Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 使用主干.Rpc设置收集方法的参数_Backbone.js - Fatal编程技术网

Backbone.js 使用主干.Rpc设置收集方法的参数

Backbone.js 使用主干.Rpc设置收集方法的参数,backbone.js,Backbone.js,使用主干.Rpc插件[],我试图在获取集合时发送read方法上的参数。使用单个模型实例时,可以通过设置模型属性的值向方法调用添加参数 var deviceModel = Backbone.model.extend({ url: 'path/to/rpc/handler', rpc: new Backbone.Rpc(), methods: { read: ['getModelData', 'id'] } }); deviceModel.set({id: 14}); dev

使用主干.Rpc插件[],我试图在获取集合时发送read方法上的参数。使用单个模型实例时,可以通过设置模型属性的值向方法调用添加参数

var deviceModel = Backbone.model.extend({
  url: 'path/to/rpc/handler',
  rpc: new Backbone.Rpc(),
  methods: {
    read: ['getModelData', 'id']
  }
});
deviceModel.set({id: 14});
deviceModel.fetch(); // Calls 'read'

// Request created by the 'read' call
{"jsonrpc":"2.0","method":"getModelData","id":"1331724849298","params":["14"]};
据我所知,没有相应的方法在获取集合之前执行类似的操作,因为主干集合没有可用的“set”方法

var deviceCollection = Backbone.collection.extend({
  model: deviceModel,
  url: 'path/to/rpc/handler',
  rpc: new Backbone.Rpc(),
  methods: {
    read: ['getDevices', 'deviceTypeId']
  }
});
// This is not allowed, possible work arounds?
deviceCollection.set('deviceTypeId', 2);
deviceCollection.fetch();
// Request created by the 'read' call
{"jsonrpc":"2.0","method":"getDevices","id":"1331724849298","params":["2"]};
是否可以使用Backbone.Rpc将参数传递给收集方法?或者我需要在fetch方法的数据对象中传递收集过滤器吗?

Ah, 好的,现在还不包括这个,但我能理解这里的问题。 我应该能够为集合添加一个变通方法,允许RPC插件读取 集合属性

var deviceCollection = Backbone.collection.extend({
    model: deviceModel,
    url: 'path/to/rpc/handler',
    rpc: new Backbone.Rpc(),
    deviceTypeId: 2,
    methods: {
        read: ['getDevices', 'deviceTypeId']
    }
});
然后将创建此响应:

{"jsonrpc":"2.0","method":"getDevices","id":"1331724849298","params":["2"]};
今晚我去看看。

啊, 好的,现在还不包括这个,但我能理解这里的问题。 我应该能够为集合添加一个变通方法,允许RPC插件读取 集合属性

var deviceCollection = Backbone.collection.extend({
    model: deviceModel,
    url: 'path/to/rpc/handler',
    rpc: new Backbone.Rpc(),
    deviceTypeId: 2,
    methods: {
        read: ['getDevices', 'deviceTypeId']
    }
});
然后将创建此响应:

{"jsonrpc":"2.0","method":"getDevices","id":"1331724849298","params":["2"]};
今晚我将看一看。

我更新了Backbone.Rpc(v0.1.2)&现在您可以使用以下语法添加“dynamic” 调用的参数

var Devices = Backbone.Collection.extend({
    url: 'path/to/my/rpc/handler',
    namespace: 'MeNotJava',
    rpc: new Backbone.Rpc(),
    model: Device,
    arg1: 'hello',
    arg2: function () { return 'world' },
    methods: {
        read : ['getDevices', 'arg1', 'arg2', 'arg3']
    }
});

var devices = new Devices();
devices.fetch();
此调用导致以下RPC请求:

{"jsonrpc":"2.0","method":"MeNotJava/getDevices","id":"1331724850010","params":["hello", "world", "arg3"]}
我更新了Backbone.Rpc(v0.1.2)&现在您可以使用以下语法添加“dynamic” 调用的参数

var Devices = Backbone.Collection.extend({
    url: 'path/to/my/rpc/handler',
    namespace: 'MeNotJava',
    rpc: new Backbone.Rpc(),
    model: Device,
    arg1: 'hello',
    arg2: function () { return 'world' },
    methods: {
        read : ['getDevices', 'arg1', 'arg2', 'arg3']
    }
});

var devices = new Devices();
devices.fetch();
此调用导致以下RPC请求:

{"jsonrpc":"2.0","method":"MeNotJava/getDevices","id":"1331724850010","params":["hello", "world", "arg3"]}

嗨,这个问题有解决方案吗?嗨,这个问题有解决方案吗?