创建具有骨干关系的嵌套结构



我正在尝试创建三个级联下拉列表。第一个包含项目,第二个包含所选项目的任务,最后一个包含所选任务的站点。

我想使用的骨干关系插件,但有困难的时间来创建适当的关系。这是我第一次使用这个插件。

目前代码:

App.Models.ProjectItem = Backbone.RelationalModel.extend({
    default: {
        id: 0,
        name: ''
    },
    relations: [{
        type: Backbone.HasMany,
        key: 'tasks',
        relatedModel: App.Models.TaskItem,
        //includeInJSON: Backbone.Model.prototype.idAttribute,
        collectionType: App.Collections.TasksCollection,
        reverseRelation: {
            key: 'projectId',
            //includeInJSON: Backbone.Model.prototype.idAttribute,
            type: Backbone.HasOne
        }
    }]
});
App.Collections.ProjectsCollection = Backbone.Collection.extend({
    model: App.Models.ProjectItem
});
App.Models.TaskItem = Backbone.RelationalModel.extend({
    default: {
        id: 0,
        name: ''
    },
    relations: [{
        type: Backbone.HasMany,
        key: 'sites',
        relatedModel: App.Models.SiteItem,
        includeInJSON: Backbone.Model.prototype.idAttribute,
        collectionType: App.Collections.SitesCollection,
        reverseRelation: {
            key: 'taskId',
            //includeInJSON: Backbone.Model.prototype.idAttribute,
            type: Backbone.HasOne
        }
    }]
});
App.Collections.TasksCollection = Backbone.Collection.extend({
    model: App.Models.TaskItem
});
App.Models.SiteItem = Backbone.RelationalModel.extend({
    default: {
        id: 0,
        name: ''
    }
});
App.Collections.SitesCollection = Backbone.Collection.extend({
    model: App.Models.SiteItem
});

创建单个项目:

var item = new App.Models.ProjectItem({
  id: 1,
  name: 'project',
  tasks: [
    {
      id: 1,
      name: 'task',
      sites: [
        {
          id: 1,
          name: 'site'
        }
      ]
    }
  ]
})

序列化为json的对象如下:

"{"id":1,"name":"task","tasks":[1],"sites":[{"id":1,"name":"site"}],"projectId":null}"

我的问题:

1)为什么site数组没有嵌套在tasks数组中?

2)站点集合的序列化方式与任务集合的序列化方式不同。我应该在站点模型中创建关系吗?

3)为什么为根返回投影?

经过几个小时的尝试和错误,我终于明白了。顺序很重要……工作代码:

App.Models.SiteItem = Backbone.RelationalModel.extend({
    default: {
        siteId: 0,
        name: ''
    }
});
App.Collections.SitesCollection = Backbone.Collection.extend({
    model: App.Models.SiteItem
});
App.Models.TaskItem = Backbone.RelationalModel.extend({
    default: {
        taskId: 0,
        name: ''
    },
    relations: [{
        type: Backbone.HasMany,
        key: 'sites',
        relatedModel: App.Models.SiteItem,
        //includeInJSON: Backbone.Model.prototype.idAttribute,
        collectionType: App.Collections.SitesCollection,
        reverseRelation: {
            key: 'task',
            includeInJSON: 'taskId',
            type: Backbone.HasOne
        }
    }]
});
App.Collections.TasksCollection = Backbone.Collection.extend({
    model: App.Models.TaskItem
});
App.Models.ProjectItem = Backbone.RelationalModel.extend({
    default: {
        projectId: 0,
        name: ''
    },
    relations: [{
        type: Backbone.HasMany,
        key: 'tasks',
        relatedModel: App.Models.TaskItem,
        //  includeInJSON: 'taskId',
        collectionType: App.Collections.TasksCollection,
        reverseRelation: {
            key: 'project',
            includeInJSON: 'projectId',
            type: Backbone.HasOne
        }
    }]
});
App.Collections.ProjectsCollection = Backbone.Collection.extend({
    model: App.Models.ProjectItem
});

对于示例数据:

var item = new App.Models.ProjectItem({
  projectId: 2,
  name: 'first',
  tasks: [
    {
      taskId: 1,
      name: 'task',
      sites:[{siteId:1, name: 'site'}]
    }
  ]
});

我得到这个json字符串:

"{"projectId":2,"name":"first","tasks":[{"taskId":1,"name":"task","sites":[{"siteId":1,"name":"site","task":1}],"project":2}]}"

相关内容

  • 没有找到相关文章

最新更新