Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 736
tunny Avatar asked May 02 '26 02:05

tunny


1 Answers

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

  • Backbone 1.1.0

    isNew: function() {
      return this.id == null;
    },
    
  • Backbone 1.1.2

    isNew: function() {
      return !this.has(this.idAttribute);
    },
    

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);
like image 64
nikoshr Avatar answered May 05 '26 13:05

nikoshr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!