使用骨干关系

时间:2016-10-12 09:42:52

标签: backbone.js backbone-relational

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

我想使用Backbone-Relational插件,但很难创建正确的关系。这是我第一次使用这个插件。

到目前为止

代码:

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)为什么sites数组没有嵌套在任务1中?

2)网站集不像任务一样被序列化。我是否应该在网站模型中创建关系?

3)为什么要为root返回projectId?

1 个答案:

答案 0 :(得分:0)

经过数小时的试验和错误后,我终于明白了。订单很重要.. 工作代码:

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}]}"