0

我创建了一个模型(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') }));
    }
});
4

1 回答 1

0

不,它不会。您尚未发布实例化或解析 SampleRule 对象的代码,但我想我明白了您的意思。我建议您在您的方法中创建一个方法,该方法RuleModel可以执行以下操作:

var RuleModel = Backbone.Model.extend({
   // As you were..
   // ...
    updateInformationModel: function(object) {
        // SampleRule.Information is an array, I assume this is by design? If so:
        this.get("Information").set(object.Information[0]);
    }
   // ...
});
于 2013-02-22T13:40:29.550 回答