backbone.js structuring nested views and models

前端 未结 5 1063
感情败类
感情败类 2020-12-02 03:51

Using backbone.js:

I have a top level ModelA that contains 2 attributes and 2 nested models, ModelB and ModelC. ModelB and ModelC each have 2 attributes as follows:<

5条回答
  •  我在风中等你
    2020-12-02 04:39

    You can use some extensions, Backbone-Forms https://github.com/powmedia/backbone-forms for example. To follow your use case define a schema like:

    var ModelB = Backbone.Model.extend({
        schema: {
            attributeB1: 'Text',
            attributeB2: 'Text'
        }
    });
    
    var ModelC = Backbone.Model.extend({
        schema: {
            attributeC: 'Text',
        }
    });
    
    var ModelA = Backbone.Model.extend({
        schema: {
            attributeA1: 'Text',
            attributeA2: 'Text',
            refToModelB: { type: 'NestedModel', model: ModelB, template: 'templateB' },
            refToModelC: { type: 'NestedModel', model: ModelC, template: 'templateC' }
        }
    });
    

    Look at https://github.com/powmedia/backbone-forms#customising-templates for partial templates.

    Important parts here are type: 'NestedModel' and template: 'templateXXX'.

    This plugin has some limitations but you can look at others at https://github.com/documentcloud/backbone/wiki/Extensions%2C-Plugins%2C-Resources.

提交回复
热议问题