使用 MooTools 处理 JSON-RPC 响应的最佳方法是什么?

What is the best way to handle a JSON-RPC response using MooTools

本文关键字:最佳 方法 是什么 响应 MooTools 处理 JSON-RPC 使用      更新时间:2023-09-26

我想使用 JSON-RPC 为 Web 服务生成代码。生成的代码将提供生成的方法供客户端使用。在传统的JavaScript中,我想人们会使用分配给每个方法的回调,但我不确定如何以适当的MooTools方式做到这一点。

var Service = new Class({
Implements: [Options, Events],
options: {
    url: 'http://localhost/lol/JSON',
    ...
    send: function(opts) {
        var JSONrpc = new Request.JSON({
         ...
        });
    JSONrpc.send(JSON.encode({'method': opts.methodname || this.options.methodname, 
                              'params': opts.params || this.options.params}));
},
    ...
    //The methods to be generated
    loginUser: function(username, password) {       
        this.send({'methodname': 'loginUser', 'params': [username, password]});
    },
    logoffUser: function(username) {
            this.send({'methodname': 'logoffUser'});
    },
    getProfile: function(username) {
       this.send({'methodname': 'getProfile', 'params': [username]});
    },

正确处理响应的最佳方法是什么?我应该为每种方法设置一个事件吗?我应该使用 id 来识别呼叫吗?MooTools 的方法是什么?

我会扩展 Request.JSON,覆盖发送和完成/成功方法,我将在其中调度解析this.data.methodname的事件,以便您在父类服务中使用它,如果需要,您可以将其冒泡到服务实例。