嘿,我不确定为什么这个 Backbone 视图显示为参考错误,说明 UserView 未定义。User 模型显示为一个函数,因此可以正常工作。我也在 jsfiddle 上用 JSLint 运行了我的代码,而且 js 明智地没有问题,或者有吗?
下面的代码
var User = Backbone.Model.extend({
defaults: {
firstName: 'J.R.',
lastName: 'Smith',
email: 'jsmith@knicks.com',
phone: '212-424-6234',
birthday: '03/05/1982',
city: 'New York'
},
location: function(){
return this.get('firstName') + ' ' + this.get('lastName') + 'is currently in ' + this.get('city') + '.';
}
});
var UserView = Backbone.View.extend({
tagName: 'li',
initialize: function() {
this.render();
},
render: function() {
$(this.el).html( this.model.get('firstName'));
}
});
var user = new User();
var userView = new UserView({model: user});