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"},
]