id duplication workaround #369

Closed
inf3rno opened this Issue Jun 25, 2013 · 4 comments

Projects

None yet

3 participants

@inf3rno

Hi!

I have a problem with id duplication, and need a workaround...

I have 2 models in my code:

var User = Backbone.RelationalModel.extend({});
var Role = Backbone.RelationalModel.extend({
    relations: [
        {
            type: Backbone.HasOne,
            key: 'user',
            relatedModel: User
        }
    ]
});

I fetch the whole User collection and a Role which can contain an User:

var users = new Backbone.Collection([
    new User({
        id: 1,
        name: "Jánszky László"
    }),
    new User({
        id: 2,
        name: "Kovács István"
    }),
    new User({
        id: 3,
        name: "Nagy Zsuzsanna"
    })
]);
var role = new Role({
    user: new User({
        id: 1       //here I got id duplication error
    })
});

After that I render the input on which I can select the User contained by the Role:

var input = new Backbone.UI.SelectOneTableView({
    width: 500,
    model: role,
    content: "user",
    alternatives: users,
    columns: [
        {
            title: "#",
            width: 50,
            content: "id"
        },
        {
            title: 'Név',
            width: 200,
            content: 'name'
        }
    ]
});
input.render();

So I really need both of the User models with the same id, but I can't have because of the id duplication error. How can I solve this problem?

@philfreo
Collaborator

try doing this instead:

var role = new Role({
user: 1
});

or save the user instance in a variable and pass that

@inf3rno

By fetch it works well. I found the code which checks the duplication, so by new instances I can fix it too:

        var role = Backbone.Relational.store.find(Role, id) || new Role({id: id});
        role.fetch();

Is there a built-in function for this? For example: Role.create or something like this?

@inf3rno

Thanks!

@bpatram bpatram added the question label Mar 26, 2016
@bpatram bpatram closed this Mar 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment