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
Collections 使用主干.Collection.get(id)restfully_Collections_Backbone.js - Fatal编程技术网

Collections 使用主干.Collection.get(id)restfully

Collections 使用主干.Collection.get(id)restfully,collections,backbone.js,Collections,Backbone.js,似乎在我调用backbone.js集合上的get时,它通过cookie传递id,而不是restfully传递到我的get方法中。在请求头中,它如下所示: Cookie:posts=ag5kZXZ-c29jcmVuY2h1c3IOCxIIUG9zdExpc3QYAQw; dev_appserver_登录=”test100@example.com:False:114323764255192059842“ 这就是我所拥有的: 获取呼叫: postCollection.get(id) 以及get方法:

似乎在我调用backbone.js集合上的get时,它通过cookie传递id,而不是restfully传递到我的get方法中。在请求头中,它如下所示:

Cookie:posts=ag5kZXZ-c29jcmVuY2h1c3IOCxIIUG9zdExpc3QYAQw; dev_appserver_登录=”test100@example.com:False:114323764255192059842“

这就是我所拥有的:

获取呼叫:

postCollection.get(id)
以及get方法:

def get(self, id):

我希望在get方法中使用id,而不必使用cookie。

实现这一点的最佳方法可能是如下所示

var model = collection.get(id);
// If the model is not present locally..
if (!model) {
  // Add empty model with id.
  model = collection.add([{id: id}]);
  // Populate model attributes from server.
  model.fetch({success: successCallback, error: errorCallback });
}

collection.get(id)
不应向后端发出请求

实现这一点的最佳方法可能是如下所示

var model = collection.get(id);
// If the model is not present locally..
if (!model) {
  // Add empty model with id.
  model = collection.add([{id: id}]);
  // Populate model attributes from server.
  model.fetch({success: successCallback, error: errorCallback });
}

collection.get(id)
不应向后端发出请求

这是另一种看法。您不必创建一个大部分为空的模型,然后在获取后从服务器向其添加属性,而可以执行我在下面粘贴的操作。在上面的示例中需要考虑的一件事是,如果您创建了一个模型,然后尝试从服务器获取该ID,但该ID不存在,则必须将其清除。下面的代码将为您保存该步骤

myModel = Backbone.Model.extend({
  url : function() {
     /*
     create _ POST   /model
     read _ GET   /model[/id]
     update _ PUT   /model/id
     delete _ DELETE   /model/id
     */
     return this.id ? '/model/' + this.id : '/model';
   },
});


myCollection = Backbone.Collection.extend({
  model: myModel,
  url: function() {
    return '/model';
  },
  comparator: function(model) {
    return model.get("foo");
  },
  getOrFetch: function(id) {
    var model = this.get(id) || this.getByCid(id);
    if (model) return model;
      var url = this.url() +"/"+ id
      return new this.model().fetch({url:url});
  }
});



var mc = new myCollection(new myModel({foo:"bar"}));
mc.getOrFetch(1)

这是另一种看法。您不必创建一个大部分为空的模型,然后在获取后从服务器向其添加属性,而可以执行我在下面粘贴的操作。在上面的示例中需要考虑的一件事是,如果您创建了一个模型,然后尝试从服务器获取该ID,但该ID不存在,则必须将其清除。下面的代码将为您保存该步骤

myModel = Backbone.Model.extend({
  url : function() {
     /*
     create _ POST   /model
     read _ GET   /model[/id]
     update _ PUT   /model/id
     delete _ DELETE   /model/id
     */
     return this.id ? '/model/' + this.id : '/model';
   },
});


myCollection = Backbone.Collection.extend({
  model: myModel,
  url: function() {
    return '/model';
  },
  comparator: function(model) {
    return model.get("foo");
  },
  getOrFetch: function(id) {
    var model = this.get(id) || this.getByCid(id);
    if (model) return model;
      var url = this.url() +"/"+ id
      return new this.model().fetch({url:url});
  }
});



var mc = new myCollection(new myModel({foo:"bar"}));
mc.getOrFetch(1)

您希望使用
获取
从远程服务器检索项目
get
返回集合中已存在的特定模型。它会通过URL自动完成-你不必做任何事情。我想得到一个特定的模型,如果它不存在,我想我的数据库中添加该模型。我只是不知道如何获取id,以便将其从客户端正确发送到服务器。您想使用
fetch
从远程服务器检索项目
get
返回集合中已存在的特定模型。它会通过URL自动完成-你不必做任何事情。我想得到一个特定的模型,如果它不存在,我想我的数据库中添加该模型。我只是不知道如何让id正确地从我的客户端发送到我的服务器。如果它不在集合中,则必须检索它,因此需要某种回调来实际查看模型(如果失败,还需要一个回调)。或者,如果您使用jQuery,您可以让该方法返回一个promise对象,该对象最终通过模型解析或被拒绝可以处理这个问题…
model.fetch
支持回调。我已经更新了示例以包含它们
collection.add
使用传递给集合的属性向集合中添加新模型。在这种情况下,
id
是唯一的属性,其他属性都在服务器上。
id
必须存在,以便
model.fetch
知道如何执行HTTP
GET/resource/id
请求。是否要将模型添加到集合中?仔细想想,如果服务器上不存在该id怎么办?清理…您还可以切换顺序。创建一个ID为的新模型,如果成功,从服务器获取详细信息,将该模型添加到集合中。如果该模型不在集合中,则必须检索该模型,因此需要某种回调来实际查看该模型(如果失败,还需要一个回调)。或者,如果您使用jQuery,您可以让该方法返回一个promise对象,该对象最终通过模型解析或被拒绝可以处理这个问题…
model.fetch
支持回调。我已经更新了示例以包含它们
collection.add
使用传递给集合的属性向集合中添加新模型。在这种情况下,
id
是唯一的属性,其他属性都在服务器上。
id
必须存在,以便
model.fetch
知道如何执行HTTP
GET/resource/id
请求。是否要将模型添加到集合中?仔细想想,如果服务器上不存在该id怎么办?清理…您还可以切换顺序。创建一个ID为的新模型,从服务器获取详细信息,如果成功,将模型添加到集合中。