backbone icon indicating copy to clipboard operation
backbone copied to clipboard

The attribute Id has been updated, does the attribute nameId also need to be updated?

Open usernameisregistered opened this issue 5 years ago • 2 comments

https://github.com/jashkenas/backbone/blob/0b1c2e3fce3156fcb9819b8bbf8885456e4e1055/backbone.js#L519

var hacker = new Backbone.Model({
  name: "name",
 idAttribute: 'nameId',
});
if (this.idAttribute in attrs) this.id = this.get(this.idAttribute); 

The attribute Id has been updated, does the attribute nameId also need to be updated? I don't think it is necessary to update the attribute id, just the nameId

I wonder if you can understand Chinese, but my English is poor

usernameisregistered avatar Apr 06 '20 06:04 usernameisregistered

The attribute Id had been updated, because the idAttribute was updated. Not the other way around.

yangzhichina avatar May 20 '20 03:05 yangzhichina

@usernameisregistered If you console.log(hacker), you will find an object that looks like this:

{
    id: undefined,
    // idAttribute inherited from Backbone.Model.prototype, default 'id'
    attributes: {
        name: 'name',
        idAttribute: 'nameId'
    }
    // more properties
}

You probably meant to do this:

// Define a new type of model which obtains its `id` from the `nameId` attribute.
var Hacker = Backbone.Model.extend({
    idAttribute: 'nameId'
});

// Create a new instance of the above model type.
var hacker = new Hacker({
    nameId: 'name'
});

Now console.log(hacker) will output the following instead:

{
    id 'name',
    idAttribute: 'nameId',
    attributes: {
        nameId: 'name'
    }
    // other properties
}

Does this answer your question?

jgonggrijp avatar Jan 07 '22 15:01 jgonggrijp