Reputation: 3502
I'm having an issue getting Backbone to send a POST request for a newly created model for which I specified an id parameter.
For those wondering why would I need that, what happens actually in the API is that a tag that already exists in the, say http://api.example.com/tags/tag_id is beeing referenced for a given content type, eg. http://api.example.com/conent/content_id/tags. What I'm therefore trying to get Backbone to do is to make a POST request to http://api.example.com/conent/content_id/tags/tag_id when a model with that id is being created in the tags collection.
Upvotes: 0
Views: 173
Reputation: 1018
To be more RESTfull you should send POST http://api.example.com/conent/content_id/tags
because POST http://api.example.com/conent/content_id/tags/tag_id
means updating of existed resource.
But if you want to do it in this way you can specify url
of collection:
var Content = Backbone.Model.extend({
urlRoot: '/content'
});
var Tag = Backbone.Model.extend({});
var Tags = Backbone.Collection.extend({
url: function(){
return this.content.url() + '/tags';
},
initialize: function(models, options){
this.content = options.content;
}
});
var content = new Content({id: 12}),
tags = new Tags([], {content: content});
tags.create({id: 25});
// => PUT/PATCH http://api.example.com/content/12/tags/25
It is because when your model have id Backbone is thinking that is is already existed model and trying to update it. One of the solution is pass type
:
tags.create({id: 25}, {type: 'POST'});
// => POST http://api.example.com/content/12/tags/25
Upvotes: 1