0

I want to use a delete method for my backbone model but for some reason, backbone does not include that model ID in produced request URL. To remove my model, i need to fire a following request: DELETE /api/v1/places/12/place_users/12. Here is my code:

# place_user.model.js
var PlaceUserModel = Backbone.Model.extend({
  url: function() {
    return this.urlRoot;
  },

  initialize: function(data, options) {
    this.urlRoot = '/api/v1/places/' + options.placeId + '/place_users'
  },
});

And i'm trying to remove that using:

# PlaceUsersCollection fetches it's results from a remote api
placeUsers = new PlaceUsersCollection({placeId: 12}).fetch();

# it's obviously more complicated in the app but let's say i just want to
# remove first model
placeUsers.models[0].destroy;

And that code produces a DELETE request to /api/v1/places/12/place_users (without a model id included). I'm not sure what else can i post in here to make it easier to help me so please ask if you need anything.

Thanks in advance!

mbajur
  • 4,406
  • 5
  • 49
  • 79

1 Answers1

0

This is because you have an url function defined, which simply returns the urlroot.

If you change your model to look like this it should work:

# place_user.model.js
var PlaceUserModel = Backbone.Model.extend({
  urlRoot: function() {
    return '/api/v1/places/' + this.options.placeId + '/place_users'
  }
});

The initialize function will be invoked automatically on creating a new instance if it is defined. The options passed in are stored in this.options within the instance, thus accessing this.options.placeId in the urlRoot function will be possible.

Exinferis
  • 689
  • 7
  • 17