I have the following situation, and I think the best way to deal with it is to use the Backbone relation
.
Please correct me if there is another solution.
I have one collection (1) and one model (2).
The result of the collection looks like (3), and the result of the model is like (4).
Finally, the view should look like this (5).
My questions:
1) can I use the Backbone relation
to solve this situation?
2) If so, how do I rewrite the FeedsCollection
to automatically retrieve the necessary 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) Result of userModel
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 result of the scan should look like this:
[ {message: "something one", creator_id: 100, name: "jhon"}, {message: "something two", creator_id: 101, name: "herry"}, ]