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

Saving models with "new" relations #451

Closed
Rejjn opened this issue Apr 10, 2014 · 1 comment
Closed

Saving models with "new" relations #451

Rejjn opened this issue Apr 10, 2014 · 1 comment
Labels

Comments

@Rejjn
Copy link

Rejjn commented Apr 10, 2014

My goal is to use a single request to save (create) multiple models. The hope is to be able to solve this within the scope of Backbone.Relational and/or Backbone, without having to write custom setters and parsers.

I'm quite willing to code the solution into Backbone.Relational myself if that's what's needed, I just want to make sure that's it's not already there today and/or deemed unfeasible.

In detail, what I want to do is take this model definition

MyModel = Backbone.RelationalModel.extend({

    ...

    relations: [
        {
            type: Backbone.HasOne,
            key: 'other',
            relatedModel: 'OtherModel',
            collectionType: 'OtherModelCollection'
        },
        {
            type: Backbone.HasMany,
            key: 'yet_others',
            relatedModel: 'YetAnotherModel',
            collectionType: 'YetAnotherModelModelCollection'
        },
        ...
    ],

    ...
}

populate it with this data (JSONish representation)

# note the lack of ids
{
    attribute_one: 'value',
    attribute_two: 'value'

    other: {
        other_attr_one: 1,
        other_attr_two: 'value',
    },

    yet_others: [
        {
            yet_another_attr_one: 'headline one',
            yet_another_attr_two: [1, 2, 3, 4],
        },
        {
            yet_another_attr_one: 'headline two',
            yet_another_attr_two: [1, 2, 3, 4],
        },
    ]
}

meaning the OtherModels and YetAnotherModels will be accessible with

enity.get('other')
// and
enity.get('yet_others').models

Now I want to be able to save the MyModel object to the server/API, get a response like this

{
    id: 1,

    other: {
        id: 1
    },

    yet_others: [
        {
            id: 1
        },
        {
            id: 2
        },
    ]
}

and have it processed by the client in such a way that what I end up with in my MyModel object is this (again, JSONish representation)

{
    id: 1,
    attribute_one: 'value',
    attribute_two: 'value'

    other: {
        id: 1,
        other_attr_one: 1,
        other_attr_two: 'value',
    },

    yet_others: [
        {
            id: 1,
            yet_another_attr_one: 'headline one',
            yet_another_attr_two: [1, 2, 3, 4],
        },
        {
            id: 2,
            yet_another_attr_one: 'headline two',
            yet_another_attr_two: [1, 2, 3, 4],
        },
    ]
}

Does that make sense?

Is this possible today, only that I'm doing it wrong? If it is not possible today, are there some fundamental properties of Backbones Model or Backbone.Relationals RelationalModel that makes this impossible, or at least unfeasible?

@Rejjn
Copy link
Author

Rejjn commented Apr 15, 2014

Update: I did find a solution for this problem via findModel. It turns out it's even mentioned in the documentation http://backbonerelational.org/#RelationalModel-findModel (oops =)

But, with 0.8.8, and by solving issue #411, the suggested solution from the documentation no longer works.

As @philfreo says in #411

Just probably need to think about the cases where a new model gets an ID later.

Did you take those cases into account, and if so, what is the new suggested solution?

For now I think I'm just going to hack the code directly and remove the if statement around

Backbone.Relational.store.register( this );

and hope that solves the problem for now as I really want some of the other fixes in 0.8.8 =/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants