问题:当存在需要以特定方式存储的属性时,初始化backbone.js 模型的正确方法是什么?我是否需要映射不需要任何特殊格式的属性?我认为backbone.js 做了某种自动映射。
例子:
var MyModel = Backbone.Model.extend({
initialize: function (options) {
// These attributes need to be stored in a different format
// Dates
this.startYear = new Date(options.startTime).getFullYear();
// Rounding numbers
this.wholeNumber = Math.Round(options.numberWithDecimals);
// Storing empty strings as nulls
if (options.fullName == null || options.fullName == "") {
this.fullName == null;
} else {
this.fullName = options.fullName;
}
// These are fine as they are
this.fieldA = options.fieldA;
this.fieldB = options.fieldB;
this.fieldC = options.fieldC;
},
});