Support object composition #322

rlivsey opened this Issue Jul 9, 2012 · 3 comments


None yet
3 participants

rlivsey commented Jul 9, 2012

Say we have some JSON as follows for a Person with an Address

    "id": "123456789",
    "firstname": "Charlotte",
    "lastname": "Harris",
    "address": {
        "street": "22 Acacia Avenue",
        "postcode": "N1 3CD"

There doesn't seem to be a way of modelling this as the address is part of the person, not a model in its own right.

Eg the following won't work as the address doesn't have an ID and only exists as part of the person:

App.Person = DS.Model.extend({
    firstname: DS.attr("string"),
    lastname: DS.attr("string"),
    address: DS.belongsTo("App.Address", {embedded: true})

App.Address = DS.Model.extend({
    street: DS.attr("string"),
    postcode: DS.attr("string")

My first thought was to create a custom type:

App.Address = Em.Object.extend({
    street: null,
    postcode: null

DS.attr.transforms.address = {
    from: function(serialized) {
        return App.Address.create(serialized);
    to: function(deserialized) {
        return deserialized.getProperties("street", "postcode");

App.Person = DS.Model.extend({
    firstname: DS.attr("string"),
    lastname: DS.attr("string"),
    address: DS.attr("address")   

That works to the extent that loading the data correctly instantiates the right objects, but it no longer works with dirty tracking and serializing. Changes to the address won't make the record dirty and don't make any difference to toJSON as these both use DataProxy.

Doing it this way also loses the ability to use namingConventions etc... as you have to manually serialize and deserialize to/from JSON.

It seems there's a need for something like DS.composedOf and/or a Model which knows it only exists as part of a parent model and cannot be directly saved and doesn't have an ID. There's probably some overlap here with #259


wagenet commented Jul 9, 2012

As I understand it, the reason why this isn't supported is because it's very difficult to do property dirty tracking with it. However, I do agree that this would be a useful feature.


joliss commented Aug 27, 2012

I think this is the same as #53, right? Maybe you can close this.

rlivsey commented Aug 27, 2012

Yep, looks to be the same thing. Cheers.

@rlivsey rlivsey closed this Aug 27, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment