Maris
Maris

Reputation: 4776

How to use fetch with with sending the Id

Hi I have the next model in backboneJS:

var Type = Backbone.Model.extend({
     url : "/api/SomeRoute"
});
var model = new Type({id:"MyAlias"});
model.fetch();

On the rest part In SomeRoute I have 2 actions which returns my Model. First which recieves the Id and returns the Model by Id and second without parameters which return the collection. But when I call fetch() in JS, it doesn't send the Id in the request. What I'm doing wrong?

Upvotes: 1

Views: 58

Answers (2)

Konza
Konza

Reputation: 2173

If there is an id attribute specified in a model. Then the fetch request will be like /api/SomeROute/id

Instead of url give urlRoot. If urlRoot is specified then the models id will be appended.

var Type = Backbone.Model.extend({
    urlRoot : "/api/SomeRoute"
});

Upvotes: 3

Farkhat Mikhalko
Farkhat Mikhalko

Reputation: 3655

Rewrite your url to next:

var type = Backbone.Model.extend({
  url: function(){
   return "/api/SomeRoute/" + this.get("id");
  }
});
var model = new Type({id: "SomeId"});
model.fetch();

Upvotes: 2

Related Questions