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) and one model (2).
The collection result looks like (3) and the model result like (4).
Finally the view should look like this (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)
// FeedsCollection
var FeedsCollection = Backbone.Collection.extend({
url: "http://localhost/feeds"
});
(2)
// User Model
var UserModel = Backbone.Model.extend({
url: "http://localhost/user"
});
(3) feedsCollection result
//feedsCollection.toJSON();
[
{id:1, message: "something one", creator_id: 100},
{id:2, message: "something two", creator_id: 101},
]
(4) userModel result
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) Finally the view result should be like:
[
{message: "something one", creator_id: 100, name: "jhon"},
{message: "something two", creator_id: 101, name: "herry"},
]