这些是我服务器的路径网址:
GET /watchlists
GET /watchlists/:id
POST /watchlists // Create a new watchlists
PUT /watchlists/:id // Change attributes of watchlists (Like name)
POST /watchlists/:id/movies // Add a movie
DELETE /watchlists/:id/movies/:trackId // Delete a movie by the trackid
DELETE /watchlists/:id // Delete the watchlists by is ID
这是我的模特:
var WatchlistModel = Backbone.Model.extend({
urlRoot: '/watchlists',
});
我想知道怎样做'添加电影'用这个网址:
/watchlists/:id/movies
和删除电话:
/watchlists/:id/movies/:trackId
对此有哪些最佳实践?
答案 0 :(得分:1)
一种解决方案是将Movies
集合创建为每个WatchlistModel
模型的子集,为其提供其父级的引用,并使用该引用构建其url
。
假设对父项的引用存储在watchlist
属性中,如
var Movies = Backbone.Collection.extend({
url: function() {
return _.result(this.watchlist, 'url') + '/movies';
}
});
var WatchlistModel = Backbone.Model.extend({
urlRoot: '/watchlists',
initialize: function() {
this.movies = new Movies();
this.movies.watchlist = this;
}
});
你会创建一个这样的新电影:
var lst = new WatchlistModel({id: 1});
var newmovie = lst.movies.create();