tunny
tunny

Reputation: 493

backbone fetch id changed

Has Model fetch behaviour, specifically setting the model id, changed between 1.1.0 and 1.1.2?

I've checked the changelog and can't find anything relevant.

The following no longer works:

var Wibble = Backbone.Model.extend({
    urlRoot: 'rest/wibble',
    idAttribute: 'wibbleId'
 });

var model = new Wibble();
model.id = 1;
model.fetch()

It requests /rest/wibble rather than /rest/wibble/1 as it used to.

Examples: I've used url() rather than fetch() to demonstrate

jsbin for 1.1.0

jsbin for 1.1.2

Upvotes: 3

Views: 77

Answers (1)

nikoshr
nikoshr

Reputation: 33364

A model builds its url by appending /[id] when the model is not new :

url: function() {
  var base = _.result(this, 'urlRoot') ||
    _.result(this.collection, 'url') ||
    urlError();
  if (this.isNew()) return base;
  return base.replace(/([^\/])$/, '$1/') + encodeURIComponent(this.id);
}

but it appears that model.isNew changed between 1.1.0 and 1.1.2

The check now only considers the property described by idAttribute and no longer the id property.

Setting your idAttribute as you did in your 1.1.2 example is probably the safest bet:

model.set('wibbleId', 123);

Upvotes: 3

Related Questions