用主干关系创建嵌套结构 [英] creating nested structure with backbone-relational

查看:86
本文介绍了用主干关系创建嵌套结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a three cascading dropdown lists. First one constains projects, second contains tasks for selected project and the last one sites for selected task.

我想使用Backbone-Relational插件,但是很难创建适当的关系.这是我第一次使用此插件.

I want to use the Backbone-Relational plugin, but have hard time to create the proper relations. That's my first time with this plugin.

到目前为止的代码:

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的对象如下:

This object serialized to json looks like this:

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

我的问题:

1)为什么站点数组未嵌套在任务一中?

1) Why the sites array is not nested in the tasks one?

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

2) Sites collection is not serialized the same way that tasks one is. Should I create a relations in the site model too?

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

3) Why the projectId is returned for the root?

推荐答案

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

After many hours of trials and errors I finally got it. The order matters.. Working code:

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字符串:

I got this json string:

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

这篇关于用主干关系创建嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆