使用复杂的JSON创建Backbone.js模型

use*_*103 2 backbone.js backbone.js-collections marionette

我有如下JSON响应

{
    "results": [
        {
            "name": "FOO",
            "containerName": "Foo",
            "accounts": [
                {
                    "id": "10445570_7601",
                    "shareeAccountInfo": "",
                    "siteAccountId": "271555",
                    "siteId": "271555",
                    "refreshMode": "NORMAL",
                    "isNetIncl": "true",
                    "propertyId": null,
                    "amount": [
                        "0.0",
                        "USD"
                    ]
                },
                {
                    "id": "1070_20537601",
                    "shareeAccountInfo": "",
                    "siteAccountId": "271555",
                    "siteId": "271555",
                    "refreshMode": "NORMAL",
                    "isNetIncl": "true",
                    "propertyId": null,
                    "amount": [
                        "0.0",
                        "USD"
                    ]
                }
            ]
        },
        {
            "name": "FOO123",
            "containerName": "Foo123",
            "accounts": [
                {
                    "id": "10445570_20601",
                    "shareeAccountInfo": "",
                    "siteAccountId": "271555",
                    "siteId": "271555",
                    "refreshMode": "NORMAL",
                    "isNetIncl": "true",
                    "propertyId": null,
                    "amount": [
                        "0.0",
                        "USD"
                    ]
                },
                {
                    "id": "10445570_37601",
                    "shareeAccountInfo": "",
                    "siteAccountId": "271555",
                    "siteId": "271555",
                    "refreshMode": "NORMAL",
                    "isNetIncl": "true",
                    "propertyId": null,
                    "amount": [
                        "0.0",
                        "USD"
                    ]
                }
            ]
        },
        {
            "name": "FOO83838",
            "containerName": "Foo3232",
            "accounts": [
                {
                    "id": "1601",
                    "shareeAccountInfo": "",
                    "siteAccountId": "271555",
                    "siteId": "271555",
                    "refreshMode": "NORMAL",
                    "isNetIncl": "true",
                    "propertyId": null,
                    "amount": [
                        "0.0",
                        "USD"
                    ]
                }
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我在使用此JSON响应创建Backbone模型时遇到问题.我应该使用嵌套模型吗?我应该如何根据我的模型创建一个集合?相反,展平这个JSON结构会是一个好主意吗?有任何想法吗?

joe*_*ews 5

您的数据结构自然适合模型集合(我称之为模型Group),其中每个模型Group包含一组Account模型.此集合(以及可选的模型)应该具有对父级的引用Group.

var Account = Backbone.Model.extend({

})

var Accounts = Backbone.Collection.extend({
  model: Account, 
  initialize: function(models, options) {
    this.parent = options.parent;
  }
});

var Group = Backbone.Model.extend({
  initialize: function() {
    this.accounts = new Accounts([], { parent: this });
  }
});

var Groups = Backbone.Collection.extend({
  model: Group,

  // Assuming you make requests to `/group` to produce your result JSON
  url: 'group',

  // Construct models from the `results` attribute of the response
  parse: function(response) {
    return response.results;
  }
});
Run Code Online (Sandbox Code Playgroud)

有两个主要的实现选择:

坚持

如果单个帐户可以单独保存在父容器中,也许使用类似的端点/group/FOO83838/account/1601,则Acccount模型可以使用默认值Backbone.Model.save.该Accounts集合应覆盖url以引用父URL:

Accounts = Backbone.Collection.extend({
  // code from earlier

  url: function() {
    return this.parent.url() + '/account';
  }
});
Run Code Online (Sandbox Code Playgroud)

如果帐户只能保存为整体Group模型的一部分,则需要做两件事:

首先,覆盖Account.save委托父save方法:

Account = Backbone.Model.extend({
  // code from earlier

  save: function() {
    this.collection.parent.save();
  }
});
Run Code Online (Sandbox Code Playgroud)

其次,覆盖Group.toJSON包括子帐户:

Group = Backbone.Model.extend({
  // code from earlier

  toJSON: function() {
    var json = Backbone.Model.prototype.toJSON.call(this);
    json.accounts = this.accounts.toJSON();
    return json;
  }
});
Run Code Online (Sandbox Code Playgroud)

(在此示例中,我使用了集合的parent引用.如果您愿意,还可以在此模型上保存对父项的引用).

活动

您可以允许应用程序代码直接侦听Group.accounts事件,在这种情况下,不需要更改代码:

// Example view code
this.listenTo(group.accounts, 'change', this.onAccountChange, this);
Run Code Online (Sandbox Code Playgroud)

或者,如果您更喜欢额外的封装,则可以转发子模型更改:

Group = Backbone.Model.extend({
  // code from earlier

  initialize: function() {
    this.accounts = new Accounts([], { parent: this });
    this.listenTo(this.accounts, 'all', this.onChildEvent, this);
  }

  onChildEvent: function(eventName, model, options) {
    // write logic to whitelist the events and parameters you are interested in
    this.trigger('child:' + eventName, model, options); 
  }
});


// Example view code 
this.listenTo(group, 'child:change', this.onAccountChange, this);
Run Code Online (Sandbox Code Playgroud)

您还可以查看DeepModel(不再维护)或Relational等Backbone扩展.我通常更喜欢更好地控制自定义实现.