Backbone 如何在Model做数据处理
RT,我生成Model为new xModel({name:'xx',age:19}),我想要在生成这个model的时候,if(age>18),让生成的model变为{name:'xx',age:19,adult:true}
backbone backbone.js JavaScript mvc
总理D军道杀拳
9 years, 3 months ago
Answers
Backbone不是很精,简单写了一个可以完成题主目的的实现。
var xModel = Backbone.Model.extend({
defaults: {
name : '',
age : '',
adult: false
},
initialize: function () {
// 实例化新增子类model时,根据age设定adult
this.isAdult();
// 若age改变,则相应变更adult
this.on('change:age', this.isAdult);
},
isAdult: function () {
this.set({adult: parseInt(this.get('age')) > 18});
}
});
sinfi
answered 9 years, 3 months ago