实现与骨干,关系一个多到多关系 [英] Implementing a Many-to-Many relationship with Backbone-Relational

查看:102
本文介绍了实现与骨干,关系一个多到多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,它定义了两个班,一个 PersonGroup ,其中还有许多一对多到位的关系。一个人可以没有组,或分配到各组,并在之间的任何东西。

I have a simple application which defines two classes, a Person and a PersonGroup, wherein there is a many-to-many relationship in place. A Person can have no group, or be assigned to all groups, and anything in between.

在backbonerelational.org的例子表明使用许多一对多的关系,两者之间的模式,但我不能让这种模式与获取(反序列化)和保存工作(序列)。

The example on backbonerelational.org suggests using an in-between model for many-to-many relationships, however I can't get this pattern to work with fetching (deserializing) and saving (serializing).

我想要做的是使用主干反序列化类似下面这样的JSON:

What I want to do is use Backbone to deserialize a JSON similar to the following:

{
    People:
    [
        {
            "ID": 1,
            "Name": "John"
        },
        {
            "ID": 2,
            "Name": "Bob"
        },
        {
            "ID": 3,
            "Name": "Tim"
        },
    ],
    PeopleGroups:
    [
        {
            "ID": 1,
            "Name": "Owners",
            "People":
            [
                1,
                2
            ],
        },
        {
            "ID": 2,
            "Name": "Everyone",
            "People":
            [
                1,
                2,
                3
            ],
        },
    ]
}

我使用击退/击倒数据绑定的,所以这个问题是我需要能够通过引用访问的关系。一组ID不我好,除非我可以创建一个Knockback.CollectionObservable包装的收集和解析ID来引用。

I'm using Knockback/Knockout for data binding so the problem is I need to be able to access the relationships by reference. An array of IDs does not do me any good, unless I can create a Knockback.CollectionObservable to wrap the collection and resolve the IDs to references.

推荐答案

我最终得到这个工作,我想要的方式。在许多一对多关系被保持在数据库中,但该关系可以仅在我的骨干模型在一个方向进行访问。我决定这个人的模型是单机和PersonGroup车型将不得不它们链接到人身模型引用的集合。

I ended up getting this to work the way I wanted to. The Many-to-Many relationship is maintained in the database but the relationships can only be accessed in my Backbone models in one direction. I decided that the Person models would be stand-alone and the PersonGroup models would have a collection of references to the Person models they are linked to.

在这里,使一切工作的关键点是指定 includeInJSON:ID和删除 reverseRelation 。这仍然可以让你访问引用JavaScript中的车型,但它正确序列化和反序列化到JSON。该车型的人根本不具备访问导航属性来他们在组,但他们的可以的存在于多个组就好了。

The key points here that made everything work were to specify includeInJSON: "ID" and to remove the reverseRelation. This still lets you access the references to the models in JavaScript, but it correctly serializes and deserializes to JSON. The Person models simply don't have access to a navigation property to the Groups they are in, however they can exist in multiple groups just fine.

我简单地认为使用ID列表将强制铁圈跳来解析引用,但骨干关系似乎使用的全球骨干示范店,以解决由ID引用,而无需创建重复的模型。 (例如,三个不同的群体可以参考同一个人被创建只有一个型号)。

I simply assumed that using a list of IDs would mandate jumping through hoops to resolve references, but Backbone-relational seems to use the global Backbone model store to resolve references by ID without creating duplicate models. (eg. Three different groups can reference the same Person and only one model is ever created).

var Person = Backbone.RelationalModel.extend(
{
    idAttribute: "ID",
});

var PersonGroup = Backbone.RelationalModel.extend(
{
    idAttribute: "ID",

    relations:
    [
        {
            type: Backbone.HasMany,
            key: "People",
            relatedModel: "Person",
            collectionType: "PersonCollection",
            includeInJSON: "ID",
        },
    ],
});

和这里的管道的其余部分,如果有帮助anyone.You可以定义Backbone.Collections如下并且通过单独的API请求获得它们:

And here's the rest of the plumbing if it helps anyone.You can define the Backbone.Collections as follows and obtain them through separate API requests:

var PersonCollection = Backbone.Collection.extend(
{
    model: Person,

    url: "/api/person",
});

var PersonGroupCollection = Backbone.Collection.extend(
{
    model: PersonGroup,

    url: "/api/persongroup",
});

var PersonModels = new PersonCollection();

var GroupsModels = new PersonGroupCollection();

this.PersonModels.fetch();

this.GroupsModels.fetch();

this.People = kb.collectionObservable(
    this.PersonModels,
    {
        factories:
        {
            "models": PersonViewModel,
        },
    }
);

this.PersonGroups = kb.collectionObservable(
    this.GroupsModels,
    {
        factories:
        {
            "models": PersonGroupViewModel,
            "models.People.models": PersonViewModel,
        },
    }
);

我包括Knockback.js使用Knockout.js绑定特定的集合。只有这样的变化轨迹是通过整个应用程序传播1视图模型每模型创建。

I included the Knockback.js specific collections for using Knockout.js bindings. Only one ViewModel is created per model so change tracking is propagated through the entire app.

这篇关于实现与骨干,关系一个多到多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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