Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone relation: how to put in relation collection and model?
    primarykey
    data
    text
    <p>I have the following situation and I think the best way to deal with it is to use <code>Backbone relation</code>.<br> Please correct me if there is another solution.</p> <p>I have one collection (1) and one model (2).<br> The collection result looks like (3) and the model result like (4).</p> <p>Finally the view should look like this (5).</p> <p>My questions are:<br> 1) possible to use <code>Backbone relation</code> to handle this situation?<br> 2) If yes, how should I rewrite the <code>FeedsCollection</code> to take automatically the needed data from the <code>UserModel</code>? </p> <hr> <p>(1)</p> <pre><code>// FeedsCollection var FeedsCollection = Backbone.Collection.extend({ url: "http://localhost/feeds" }); </code></pre> <hr> <p>(2)</p> <pre><code>// User Model var UserModel = Backbone.Model.extend({ url: "http://localhost/user" }); </code></pre> <hr> <p>(3) feedsCollection result</p> <pre><code>//feedsCollection.toJSON(); [ {id:1, message: "something one", creator_id: 100}, {id:2, message: "something two", creator_id: 101}, ] </code></pre> <hr> <p>(4) userModel result</p> <pre><code>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"} </code></pre> <hr> <p>(5) Finally the view result should be like:</p> <pre><code>[ {message: "something one", creator_id: 100, name: "jhon"}, {message: "something two", creator_id: 101, name: "herry"}, ] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload