dzm*_*dzm 0 javascript json backbone.js underscore.js
我有一个视图,它正在对集合执行fetch()并从服务器返回一些模型.
ProductsView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
this.collection = new ProductCollection();
this.collection.fetch({data: {limit : this.options.limit}});
console.log(this.collection);
this.render();
},
render: function() {
var template = _.template( $("#product-template").html(), this );
$(this.el).html( template );
return this;
}
});
Run Code Online (Sandbox Code Playgroud)
在上面的console.log中,我看到这样的对象:
products.view.js:13
d
_byCid: Object
_byId: Object
length: 7
models: Array[7]
__proto__: x
Run Code Online (Sandbox Code Playgroud)
该models是存在的,但是当我做console.log(this.collection.models)它只显示[],该机型里面,是这样的对象数组:
models: Array[7]
0: d
1: d
2: d
3: d
4: d
5: d
6: d
Run Code Online (Sandbox Code Playgroud)
其中每个都attributes具有返回的值.
任何想法为什么模型在我使用this.collection.models或使用时都不会显示get()也不起作用.
非常感谢!
一般this.collection.fetch({data: {limit : this.options.limit}})是异步操作,所以你下一行不一定要打印正确的内容collection.
相反,你应该使用success和error回调fetch方法提供作为其部分options参数(或收听收集的change事件),如下所示:
this.collection.fetch(
{
data: { limit : this.options.limit },
success : function(collection, rawresponse) {
// do something with the data, like calling render
}
}
);
Run Code Online (Sandbox Code Playgroud)
为了完整起见:
this.collection.on('change', function(){ // some handling of the data };
this.collection.fetch();
Run Code Online (Sandbox Code Playgroud)
是基于事件的方法.
| 归档时间: |
|
| 查看次数: |
1355 次 |
| 最近记录: |