English 中文(简体)
后骨关系:如何建立关系收集和模型?
原标题:Backbone relation: how to put in relation collection and model?

I have the following situation and I think the best way to deal with it is to use Backbone relation.
Please correct me if there is another solution.

I have one collection (1) (1) and one model (2) ( (2)).
The collection result looks like (3) and the model result like (4).

最后,这种观点应当看起来象这个(5)。

My questions are:
1) possible to use Backbone relation to handle this situation?
2) If yes, how should I rewrite the FeedsCollection to take automatically the needed data from the UserModel?


(1) (1)

// FeedsCollection
var FeedsCollection = Backbone.Collection.extend({
    url: "http://localhost/feeds"
});

(2) ( (2))

// User Model
var UserModel = Backbone.Model.extend({
    url: "http://localhost/user"
});

(3) 种子集合结果

//feedsCollection.toJSON(); 

[
   {id:1, message: "something one", creator_id: 100},
   {id:2, message: "something two", creator_id: 101},
]

(4) 用户模式结果

userModel = new UserModel({id: 100});
userModel.fetch();
userModel.toJSON(); // {id:100, name: "jhon"}

userModel = new UserModel({id: 101});
userModel.fetch();
userModel.toJSON(); // {id:101, name: "herry"}

(5) 最后,观点结果应当如下:

[
    {message: "something one", creator_id: 100, name: "jhon"},
    {message: "something two", creator_id: 101, name: "herry"},
]
问题回答

Have you considered returning the user s name from the server directly. So that the url "/feeds", would return:

[
    {message: "something one", creator_id: 100, name: "jhon"},
    {message: "something two", creator_id: 101, name: "herry"},
]

如果您只是希望用户名称在消息附近显示, 这似乎是一个简单的解决方案。 我不知道您服务器的后端是什么, 但我肯定这可以高效完成 。

所以回答你的问题:

1) 利用后骨关系处理这种情况的可能性?

也许您可以使用插件来解决这种情况, 但我不建议引入插件来解决这个具体的例子。 对我来说, 查询服务器中的用户模型似乎效率不高, 只显示用户模型的名称。 但是在不同的情况下, 这样做可能是合适的 。

无论如何,如果您决定用插件或不带插件装入用户模型。 您必须提出一些问题才能弄清楚它是否值得 :

  1. How many queries will this generate? One per each user might be too much.
  2. How many user models will the server return in case you load all with one request? All users might be to much. So you need to load only needed ones.
  3. How much data will the server return? Do you really need all users s attributes?

最后,我还是要说,你直接从服务器上返回用户名, 只有在需要多于用户名时才装入特定用户型号。





相关问题
What s the appropriate granularity for Backbone.js Views?

I m adopting Backbone.js to render a small corner of an existing large web app. If this goes well, I can see Backbone.js growing to encompass the whole of the app, lending some much-needed structure ...

Rendering Layouts with Backbone.js

If you were to build a single page web application (SPWA) using Backbone.js and jQuery with--for example--two controllers that each required a unique page layouts, how would you render the layout? ...

Load html without refresh

Im unsure of how to approach this, should I have the html to be loaded hidden or load it from somewhere? I want to load a form in one page, and dynamic content on other pages. The form can be saved ...

Why are my CoffeeScript/backbone.js events not firing?

I m trying to familiarize myself with CoffeeScript and backbone.js, and I must be missing something. This CoffeeScript: MyView = Backbone.View.extend events: { "click" : "testHandler" ...

热门标签