我是backbone.js 的新手,这是我的问题。
首先我设置路由:
var Router = Backbone.Router.extend({
initialize: function(){
var data = {
member: new Member.Model({ _id: 'some id' })
};
_.extend(this, data);
// Use main layout and set Views.
app.useLayout("main-layout").setViews({
".member": new Member.Views.Start(data)
}).render();
},
routes: {
"": "index"
},
index: function() {
this.member.fetch();
}
});
在索引路由上,当元素与成员类匹配时,这将简单地获取开始视图并将其添加到 DOM。这工作得很好。但是在模板中我有:
<%= model.get('username') %>
这打印
function String() { [native code] }
而不是模型属性。这是模型和视图类:
var Member = app.module();
Member.Model = Backbone.Model.extend({
urlRoot: '/Members',
idAttribute: "_id",
defaults: {
_id: null,
name: String,
email: String,
username: String,
initials: String,
provider: String,
provider_id: String,
confirmed: Boolean,
provider_avatar: String
},
initialize: function(models, options){
}
});
Member.Views.Start = Backbone.View.extend({
template: "member/start",
serialize: function() {
return { model: this.options.member };
},
initialize: function() {
}
});
对于我的项目,我使用主干样板https://github.com/backbone-boilerplate/backbone-boilerplate 模板工程:https ://github.com/backbone-boilerplate/backbone-boilerplate/wiki/Working-with-templates
为什么我得到这个文本而不是模型值,以及如何解决这个问题?