如何处理在Backbone.js的关系[复制](How to handle relations in

2019-07-29 17:51发布

This question already has an answer here:

  • backbone.js - handling model relationships in a RESTful way 4 answers

I'm stuck with how to design my backbone.js application regarding to my model relationships.

If I have an event model , that has a couple of relationships, say a user model can have many events and the event model in turn can have many comments and participations. A user can have many comments and the participation can have one user and one event. Wow, what a mess!

Event has many Comments
Event has many Participations
Event has one User

User has many Events
User has many Participations
User has many Comments

Comment has one Event
Comment has one User

Participation has one User
Participation has one Event

Okey, so my thought was to load a list of events when the user loads the page, and when a user clicks on an event load the rest of the information about that event (comments, participations and user).

So the question is, should I use some sort of global variable to hold all events, users and so on, and when I grab information from the server put it there (and check there before I grab something from the server), maybe in some sort of localstorage (and how do I do this using backbone-relational?).

Another idea I've had is to let every event have its own independent data, the problem with this is that I will probably send redundant data every time I click on an event.

What way do you recommend?

thanks!

Answer 1:

像@mu_is_too_short评论,骨干关系可能是一个你有兴趣寻找到。 随着骨干关系的模型和子集自动创建和事件可以在亲子关系进行管理。

我只是给你一些示例代码,这样你就可以得到它的味道。 你的示例代码部分原因可能是这个样子。

用户有许多活动:

User = Backbone.RelationalModel.extend({
    relations: [
        type: Backbone.HasMany,   // Type of relationship
        key: 'events',            // How we reference the sub-models in collection
        relatedModel: 'Event',    // The sub-model type
        collectionType: 'EventCollection',  // The sub-model collection
        reverseRelation: {
            key: 'belongsToUser'            // Key we use to refer to the parent
        }
    ],
    // Other Backbone.Model properties and functions
});

当你创建一个骨干关系模型,它会自动创建您指定的“钥匙”后,命名为您子模型的集合。 所以,你必须每个用户都会有它自己的相关事件整洁,编起来的集合。

基本上,当你创建或者获取用户,你给它引用到它所需要的相关模型。 因此,例如,用户ID = 1可能需要事件5,7,和11。(我只是用的ID)。 只要这些引用数组形式定义,那么你可以使用关系的fetchRelated方法懒洋洋地加载它们。

myUser = new User();
myUser.set({
    name: 'RayMysterio',
    age: '26',
    events: [5, 7, 11]    // Or this might just come with the User object from your server
});

myUser.fetchRelated('events');
// This will go fetch the related events for this user model from the URL you designate.

myUser.get('events');
// The collection of events are treated like an attribute of the User model.

myUser.get('events').find(function(eventModel){
    return // some find condition - or whatever you want to do on that collection
});

您可能希望某些听众绑定到子模型。

myUser.bind('add:events', function(model, collection) {
    // Whatever code you want to happen when new models are added to the user events coll
});

等等 等等

这是生产一对一,一对多,和逆关系的一个很好的方式。 这是非常关键的。 当你定义模型之间的关系,并创建模型。

例如,你创建的用户模型的新实例。

骨干关系自动生成逆链接(事件模型由反向关系的关键“belongsToUser”(或任何你能想到的定义的属性)。这使得它非常方便的上下窜动模型/子模型的层次结构。

根据您的需求关系这似乎是一个不错的选择。

如果你想多对多的,有(使用中级车型),那么这是一种迂回的方式,但我发现这是一种靠不住的,我避免它。 保罗 - Uithol已更新骨干,关系一段时间,新的功能不断地让加,学习曲线是有点难受,在第一,但一旦你开始习惯了它的强大的得心应手。

注:只是为了强调,Mosselman推荐Require.js,我也坚决同意这种说法。 它使我的代码更易于管理。 您可以鼓捣(套)的骨干关系代码,使其兼容AMD和它与要求完美的作品。

更新:骨干关系现在支持2014年4月1日,作为require.js发布0.8.8的 - 感谢肯尼斯



Answer 2:

理论上你可以加载所有单独的集合,然后使用本地过滤的集合,当你做你的渲染抢相关机型。

你必须考虑安全问题,这个当然。

我并不很清楚,至少对我来说,从你的问题你会跟你抢的信息做的,但我会尽力回答,尽我所能。

我的假设是,你有某种一些额外的数据事件,你需要显示与此相关的信息。 然后,你还需要能够做的事情一样添加注释,等等。

就像我说的,你可以考虑做以下几点:

var Events = Backbone.Collection.extend({
    url: '/events', // You might want to let the app return only an account's events or something, not just ALL events.
    initialize: function(){
        this.fetch();
    }
});

var Comments = Backbone.Collection.extend({
    url: '/comments',
    initialize: function(){
        _.bindAll(this, 'getEventComments');
        this.fetch();
    },

    getEventComments: function(event_id){
        return _.filter(this.models, function(model){ return model.get('event_id') == event_id; });
    }
});
//etc

通过使用下划线的过滤功能,你可以很快得到相关的模型每次你(在渲染时为例)需要的时候。

在某种程度上,这是因为你在你的服务器端脚本做到这一点是相同的。 一些数据库保存与记录所有的表,因为你的藏品将在这里做的,和你的代码只是要求基于一些输入相关的多个数据库。

最后,我将始终保持与骨干到的人指着组合Require.js。 可耻的是指的一个答案,我今天给,检查出来,它会让生活变得更轻松无论什么: 主干网设计方案



文章来源: How to handle relations in backbone.js [duplicate]