Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
usernameisregistered opened this issue Apr 6, 2020 · 2 comments

Comments

@usernameisregistered
Copy link

if (this.idAttribute in attrs) this.id = this.get(this.idAttribute);

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

@yangzhichina
Copy link

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

@jgonggrijp
Copy link
Collaborator

@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 jgonggrijp added this to Fridge in Dusting off Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

3 participants