我正在使用Backbone.Mutators.js js插件来覆盖setter和getter。下面是我的模型
var BuyerModel = Backbone.Model.extend({
mutators: {
fullName: {
get: function () {
return this.firstName + ' ' + this.lastName;
}
}
}
});下面是我如何设置和获得全名。
var buyerModel = new BuyerModel();
buyerModel.set({ firstName: 'Joe', lastName: 'Bloggs' });
console.log(buyerModel.get('fullName')); // returns undefined undefined
console.log(buyerModel.get('firstName')); // return Joe
console.log(buyerModel.get('lastName')); // returns Bloggs为什么fullName返回未定义的、未定义的以及如何修复它?
发布于 2014-10-05 07:49:32
我不知道变异插件的情况,但似乎你需要将你的功能改为
return this.get('firstName') + ' ' + this.get('lastName');这意味着函数的作用域是模型,而不是属性子对象。
发布于 2014-10-05 07:49:43
模型的属性存储在它的attributes属性中,这些属性不是实例的直接属性,应该使用get方法:
return this.get('firstName') + ' ' + this.get('lastName');https://stackoverflow.com/questions/26200668
复制相似问题