我创建了一个模型(RuleModel),它的属性是 other model(RuleInformationModel) 。但是在传递 json(SampleRule) 数据时,内部模型没有被填充。
当我做控制台时,我变得不确定。所以我在 cosole 中检查了 RuleInformationModel,它只有来自示例 json 的默认值值没有被添加到这个内部模型中。
var SampleRule = {
RuleId: 1,
Information: [{
RuleId:1,
RuleName: 'Diwali Offer',
MerhantId: '565cfa73-8f12-4876-b2cf-2e61a28eddca',
StartDate: '1/1/0001 12:00:00 AM',
EndDate: '1/1/0001 12:00:00 AM',
IsActive: 'True',
Description: 'Diwali Offer for Samsung products',
RuleSetId: 1 ,
Status:'False'
}],
Conditions: {},
Actions: {}
};
var RuleInformationModel = Backbone.Model.extend({
defaults: {
RuleId: '',
RuleName: '',
MerchantId: '',
StartDate: '',
EndDate: '',
IsActive: '',
Description: '',
RuleSetId: '',
Status: 'False'
},
idAttribute: "RuleId"
});
var RuleModel = Backbone.Model.extend({
initialize: function (options) {
this.RuleId = options.RuleId;
},
defaults: {
RuleId: '',
Information: new RuleInformationModel(),
Conditions: {},
Actions: {}
},
idAttribute: "RuleId",
urlRoot: function () {
return "../BackBoneApi/RuleEngine/" + this.RuleId
},
parse: function (response) {
response = SampleRule;
return response;
}
});
var NewOrEditRuleView = Backbone.View.extend({
tagName: 'div',
template: ItemTemplate,
initialize: function (options) {
var self = this;
self.model = new RuleModel({RuleId:options.RuleId});
this.model.fetch();
console.log(self.model.get('Information').get('RuleName'));
},
render: function (Purpose) {
var self = this;
var tmpl = _.template(self.template);
console.log(self.model);
self.$el.html(tmpl({ Purpose: Purpose, RuleName: self.model.get('Information').get('RuleName') }));
}
});