Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

id duplication workaround #369

Open
inf3rno opened this Issue · 4 comments

2 participants

Jánszky László Lajos Phil Freo
Jánszky László Lajos

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?

Phil Freo
Collaborator

try doing this instead:

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

or save the user instance in a variable and pass that

Jánszky László Lajos

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?

Jánszky László Lajos

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.