我有一个简单的 Backbone 视图继承层次结构,并希望我的所有视图都有一个通用模型。只有这些视图中模型的 url 属性会有所不同。所以我在初始化模型时传递了 url。
父通用视图是:
GenericView = Backbone.View.extend({
template: "friendsList-template.html",
initialize: function(){
var that = this;
that.model.fetch({
success: function(model,response){
that.handleResponse(response);
},
error: function(){
//handle error
}
});
}
});
子视图是:
PersonView = GenericView.extend({
el:$("#container"),
personData: {},
template: "profile-template.html",
model = new GenericModel({
url:"testRouter.php"
});//here is where I'm defining the url
initialize: function(){
PersonView.__super__.initialize.apply(this);
},
render: function(){
var that = this;
$.get(this.templatesPath + this.template, function(resTemplate){
var html = Mustache.render(resTemplate, that.personData);
that.$el.html(html);
});
},
handleResponse: function(res){
this.personData = res;
this.render();
}
});
GenericModel 只是空模型(至少现在)
GenericModel = Backbone.Model.extend({
});
在控制台中,我收到以下错误:
Uncaught Error: A "url" property or function must be specified backbone.js:1559
是我的层次结构是问题还是其他?有什么想法吗?