【发布时间】:2012-08-30 10:49:28
【问题描述】:
问题:当存在需要以特定方式存储的属性时,初始化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;
},
});
【问题讨论】:
-
你在考虑
parse吗? -
身份证。我是吗?有没有更好/更容易/更合适的方法来做我在这个例子中所做的事情?
-
@Jack:
parse如果你指定了{parse:true}选项,也会被构造函数调用,但这并没有记录在案。 -
@muistooshort 我不知道,谢谢。
标签: backbone.js