豪尔赫将采用我自己的方法,把新增加的json列入模板。
同样:
var userModel = Backbone.Model.extend({
initialize: function(){
_.bindAll(this, fullname , toFullJSON );
},
fullname: function(){
return this.get( name ) + " " + this.get( lastname );
},
toFullJSON: function(){
var json = this.toJSON();
return _.extend(json, {fullname : this.fullname()});
}
});
var user = new userModel();
u.set({name: John , lastname: Doe });
// you will see in this console log, that the toFullJSON function returns both the toJSON properties, and your added propert(y)(ies)...
console.log(u.toFullJSON());