【发布时间】:2014-10-03 22:04:37
【问题描述】:
我有一个从 REST 端点获取的集合,它接收 JSON。
所以要完全清楚:
var Products = Backbone.Collection.extend({
model: Product,
url : 'restendpoint',
customFilter: function(f){
var results = this.where(f);
return new TestCollection(results);
}
});
var products = new Products();
products.fetch();
如果我记录了这个,那么我就有了数据。但是,对象(初始)的长度为 0,但它有 6 个模型。我认为这种差异与错误有关,而我不知道实际错误是什么。
现在,如果我尝试过滤这个:
products.customFilter({title: "MyTitle"});
即使我知道有一个特定的标题,它也会返回 0。
现在是时髦的部分。如果我获取整个 JSON 并复制它,就像将其复制/粘贴到这样的代码中:
var TestCollection = Backbone.Collection.extend({
customFilter: function(f){
var results = this.where(f);
return new TestCollection(results);
}
});
var testCollectionInstance = new TestCollection(COPY PASTED HUGE JSON DATA);
testCollectionInstance.customFilter({title: "MyTitle"});
现在返回我期望的 1 模型。我记录这两个集合时的区别如下所示。 .fetch() 中是否有一些我不知道的时髦行为?
编辑 2:使用 .fetch() 我在视图中实际使用模型时没有任何问题,这也可能是有价值的。只是过滤部分很时髦。
编辑 3:添加了视图。很可能是我还没有得到流量。基本上,当我只需要 fetch() 数据并将其发送到视图时,这一切都可以正常工作,但是,fetch 被硬编码到渲染函数中,所以 this.fetch({success: send to template});这可能是错误的。
我想要做的是能够过滤集合并将任何集合发送到渲染方法,然后使用该集合渲染模板。
var ProductList = Backbone.View.extend({
el: '#page',
render: function(){
var that = this; /* save the reference to this for use in anonymous functions */
var template = _.template($('#product-list-template').html());
that.$el.html(template({ products: products.models }));
//before the fetch() call was here and then I rendered the template, however, I needed to get it out so I can update my collection and re-render with a new one (so it's not hard-coded to fetch so to speak)
},
events: {
'keyup #search' : 'search'
},
search : function (ev){
var letters = $("#search").val();
}
});
编辑:添加新图像以解决问题
【问题讨论】:
-
你如何检查长度?您的任何代码都没有实际使用它。可能,您在加载项目之前检查长度。
-
这不是我关心的。我也看到了那些用 console.log() 欺骗你的帖子。问题是过滤器(基于过滤器返回新集合)不返回任何内容。我认为长度部分只是错误的症状。为了清楚起见,如果我过滤已由 .fetch() 填充的集合,它不起作用。如果我过滤由 JSON 的复制粘贴填充的集合(相同的代码),那么它就可以工作。编辑:我会更新一张清晰的图片。
-
fetch是一个 AJAX 调用,因此在collection.fetch()之后您的集合中不会立即有任何内容,您必须等待异步操作完成。 -
但据我所知有什么?如果不能,请为它清除修复,因为我不明白。
标签: javascript json backbone.js fetch