我遇到了骨干问题,虽然所有脚本都已加载,但模型似乎对骨干不明确。 (我正在使用require来加载主干和其他JavaScript文件)。Backbone targetModel = undefined
所以每当我做一个collection.fetch我得到这个错误在Firebug:
TypeError: targetModel is undefined
当我运行它拥有在这一点上脚本:
if (attrs instanceof Model) {
id = model = attrs;
} else {
id = attrs[targetModel.prototype.idAttribute];
}
当我和我的鼠标悬停
它说:undefined 它现在似乎不工作,我做的唯一的事情就是改变我的html模板,它只能在collection.fetch后加载。
你能帮我解决吗?
这里是我的模型:
var OF = OF || {};
OF.UsersMdl = Backbone.Model.extend({
default: {
username: '',
mailinglist: '',
email: ''
},
initialize: function() {
//
},
result: {
success: false,
message: ''
},
validate: function(att) {
}
});
这里是收集:
var OF = OF || {};
OF.UsersCollection = Backbone.Collection.extend({
initialize: function() {
//
},
parse: function(data){
return data["all-users"];
},
model: OF.UsersMdl,
url: 'php/api/users'
});
最后但并非最不重要的路由器与要求部分:
goToUsers: function() {
require(['./models/users', './views/users_view', './collections/user_collection'], function(UsersMdl, UsersView, UsersCollection) {
OF.usersMdl = new OF.UsersMdl;
OF.usersCollection = new OF.UsersCollection;
OF.usersView = new OF.UsersView;
//when the collection is fetched
$.when(OF.usersCollection.fetch({
data: {
"admin": OF.login.attributes.admin,
"session": OF.login.attributes.session
},
success: function(){
//console.log(OF.usersCollection.length);
}
//then render the view
})).then(function(){
OF.usersView.render();
}, 300);
});
},
这里是JSON将被取回:
{ “所有用户”: [ { “用户名”: “测试仪”, “邮件列表”: “1”, “电子邮件”: “[email protected]” },{ “用户名”: “tester2”, “邮件列表”: “1”, “电子邮件”: “[email protected]” },{ “用户名”: “tester3”, “邮件列表”:“0 “, ”email“:”[email protected]“ } ] }
在此先感谢
定义了哪个targetModel? – fbynite
targetModel是一个主干变量。所以我没有定义这个。除非你的意思是集合中的“model:OF.UsersMdl”部分? – BonifatiusK
我个人从来没有听说过'targetModel'的默认变量,你可以发布一个链接到文档,它在哪里被描述? – drax