我一直在我的头上撞墙在这一个几个小时.... 看着许多的一页,我觉得我有我的脚本正确,但我的收藏在fetch方法中传递的add:true不会触发add事件。我可以绑定到复位事件,但添加事件不会触发..骨干集合没有触发添加事件取得
型号:
test.Models.AppBlock = Backbone.Model.extend({
defaults: {
AppID: null,
AppName: null,
AppDescription: null
},
idAttribute: "AppID"
});
收藏:
test.Collections.AppBlock = Backbone.Collection.extend({
model: test.Models.AppBlock,
url: ApiPath + "apps/byuser",
Loading: false,
initialize: function() {
},
getAppBlocks: function() {
if (!this.Loading) {
this.Loading = true;
this.fetch({
data: JSON.stringify({
Token: test.Session.Token
}),
add: true, // OMG WTF ?!?!
type: 'POST',
dataType: 'json',
contentType: 'application/json',
cache: false,
success: this.getAppBlocksSuccess,
error: this.getAppBlocksError
});
}
},
parse: function (response) {
return response.Data;
},
getAppBlocksSuccess: function (that, response) {
},
getAppBlocksError: function (that, response) {
}
});
查看:
test.Views.DashboardLatestMain = Backbone.View.extend({
Loading: null,
initialize: function() {
this.template = _.template(test.Templates.DashboardLatestMain);
this.collection.bind('add', this.addAppBlock);
this.render();
},
render: function() {
$('#latest').prepend(this.template);
this.Loading = new test.Views.Loading({
el: '.main'
});
this.collection.getAppBlocks();
},
addAppBlock: function (appBlockModel) {
alert(appBlockModel); // no dice....
var appBlockView = new test.Views.AppBlock({
model: appBlockModel,
el: '#latest .main h3',
After: true
});
}
});
任何非常感谢帮助。
编辑
数据从API返回:
{
"Status":["1","OK"],
"Data":[{"AppID":1,"AppName":"test","AppDescription":"test"},
{"AppID":2,"AppName":"test","AppDescription":"test"}]
}
我们可以有更多关于你打电话回来的信息吗?模型添加到集合中,但不会触发“添加”事件? – Loamhoof
您使用BB 1.0吗?如果不是,则需要使用'update:true',因为在1.0之前,默认值是重置而不是更新,只触发'重置'事件并且不会触发'添加'事件。 –
@MikaelHärsjö我看不到你的观点。他从服务器获取数据,他为什么要使用set? – Loamhoof