在主干上填充集合
populate collection on Backbone
Tutorial.Views.Layout = Backbone.Layout.extend({
model: Tutorial.Model,
});
initialize: function () {
_.bindAll(this);
this.model = new Tutorial.Model({id : $("#data").data('user') });
this.collection = new Tutorial.Collection();
var success1 = function(){
console.log('OK');
};
this.collection.fetch({success : success1});
},
showTuto: function(){
step = this.collection.first(1);
console.log(step);
},
我的问题是集合是空的。我的模型有4个项目,但是我一个都没看到。
Thanks in advance
我们需要看到更多的代码来做出更接近的建议,但希望这个解释将帮助您。你应该将你的模型直接传递给集合,或者在获取中处理它。
//simplified view
YourView = Backbone.View.extend({
initialize : function(){
var testModel = new Tutorial.Model({id : $("#data").data('user') });
this.collection = new Tutorial.Collection();
this.collection.add(testModel);
}
});
在这种情况下,您将直接将该模型添加到您的集合中。如果您想异步调用并从fetch获取数据,然后传递回调,您可以这样做:
//simplified view
YourView = Backbone.View.extend({
initialize : function(){
this.collection = new Tutorial.Collection();
this.collection.fetch(function(){
console.log('okay');
});
}
});
Tutorial.Collection = Backbone.Collection.extend({
fetch : function(callback){
// grab a ref to your collection
var thisCollection = this;
// call your data function which has the ajax call
getYourDataFunction(function(data){
// if your data returned is an array of objects
thisCollection.add(data);
if (typeof callback === "function"){
//if you included a callback, call it with a reference to the collection
callback.call(thisCollection);
}
});
});
});
相关文章:
- 将主干集合填充到表中
- 主干从外部json填充集合
- Backbone.js在填充集合后调用render
- 如何用图形填充流星集合并单独显示它们
- 使用 jQuery 填充“从集合中选择框”
- Backbonejs 集合未填充,但获取工作
- 主干 - 从 json 填充集合并更新视图
- 在 Meteor 中,如何使表单加载从先前从 MongoDB 集合中提取的插入对象预填充
- 如何使用主干填充子集合
- 尝试使用 填充将两个子集合关联到父集合
- Backbone Marionette.js reqres框架没有'不要等待集合被填充
- 无法从backbone.js教程中填充主干集合
- 主干持久性从服务器检索数据;t填充集合和模型
- 主干集合不会填充
- Meteor 0.9.1.1-从json端点填充到服务器端集合
- 主干-如何填充和显示模型中的集合项
- 用实例方法的结果填充集合中每个项目的属性
- 分别填充集合
- 主干JS: self填充集合
- 在主干上填充集合