Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was wondering if it would be possible to define a model stored into another one.

I have this kind of structure:

Model Contact
    String name
    Model Address (hasMany)
    Model Phone (hasMany)

On my backend, addresses and phones are MongoDB's embedded documents contained in a "Contact" document.

And, as long as they are embedded documents, they do not have an id. And when I am on the Emberjs/data layer, they are -well- loaded with the embedded option (cf. the end of the https://github.com/emberjs/data#one-to-one section) but stored as separate objects and this cause trouble when updating or saving...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.7k views
Welcome To Ask or Share your Answers For Others

1 Answer

You are using the RESTadapter... when you are saving you want to serialize all the relationships embedded?

When you are saving or updating your record pass in the options hash to the toJSON method with

{associations: true}

Take a look at the unit tests on ember-data for examples: https://github.com/emberjs/data/blob/master/packages/ember-data/tests/unit/to_json_test.js

deepEqual(record.toJSON({ associations: true }),
        { id: 1, name: "Chad", phone_numbers: [{
            id: 7,
            number: '123'
          },
          {
            id: 8,
            number: '345'
          },
          {
            id: 9,
            number: '789'
          }
        ]},
        "association is updated after editing associations array");
});

Hope this helps..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...