Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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 通过收集回帖,在主干网中获得一篇帖子_Backbone.js - Fatal编程技术网

Backbone.js 通过收集回帖,在主干网中获得一篇帖子

Backbone.js 通过收集回帖,在主干网中获得一篇帖子,backbone.js,Backbone.js,我有以下收藏: AisisWriter.Collections.Posts = Backbone.Collection.extend({ model: AisisWriter.Models.Post, // Build the url based on blog id. url: function() { url = '/api/v1/blogs/' + AisisWriter.blog_id + '/posts/' return url; } }); 这使我能

我有以下收藏:

AisisWriter.Collections.Posts = Backbone.Collection.extend({
  model: AisisWriter.Models.Post,
  // Build the url based on blog id.
  url: function() {
    url = '/api/v1/blogs/' + AisisWriter.blog_id + '/posts/'
    return url;
  }
});
这使我能够做到:

var options = { reset: true };
this.writer_posts.fetch(options).then(this.postsRecieved, this.serverError);
这将返回我所有的职位,目前六个

我已经厌倦了做:

var options = { reset: true };
this.writer_posts.fetch({id: id}).then(this.postsRecieved, this.serverError);
// id is the id passed into the route, in this case it's #=> {id: 6}
但是这仍然会返回我所有的六篇文章,我已经看到了,但是我不认为我应该在代码中间扩展模型来添加一个ID,我使用模型来发布、放置和删除操作,同时使用集合来获取数据


那么如何返回一篇帖子呢?

一般来说,要从集合中获取一些模型,您应该先获取集合,然后按id获取所需的模型,例如:

this.writer_posts.fetch();
收集完后会把你带走

或者,您可以尝试通过在中传递所需id来获取特定模型:

或者类似的

this.writer_posts.get(id);
this.writer_posts.fetch({
    data: {
        id: id
    }
});