Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded records in ember-data 1.0 beta

My records are not flat. They have this structure:

{
    'type' : 'node',
    'properties' : {
        'name' : 'sfddsadfsd',
        'xxx'  : 'sadfdsf',
    },
    'outputs' : {
        'fghdf' : 'sadfdsf',
        'xxxx'  : 'sdfsd',
    }
}

You get the idea. Those fields (properties and outputs) do not refer to sideloaded records; instead, they are part of my record (in my CouchDb database). I did this (before learning that this is a sin by ember-data standards) because it is a handy way of organizing lots of properties in a document - the term that CouchDb uses for records. This name also suggest why you want to have structure in your record: because a document can get quite big, and thus you need some organizing structure to make your life easier (or so I thought, before bumping into ember-data).

I was happily modelling these records using embedded properties with the previous version of ember-data. Now, it seems that ember-data has dropped support for embedded records. There is a suggestion of implementing extractSingle and do some funky stuff with mapProperty('id');

Well: since they are part of my record, the embedded properties/outputs have no record id. There is simply no concept of property or output outside the node. They are not independent data with IDs: they are just part of the node.

Previously I had the following model definition:

SettingsApp.NodeProperties = DS.Model.extend({
    name : DS.attr('string'),
});

DS.RESTAdapter.map('SettingsApp.NodeProperties', {
    name : {key: 'name'},
});

SettingsApp.Node = DS.Model.extend(SettingsApp.NodeMixin, {
    properties : DS.belongsTo('nodeProperties')
});

DS.RESTAdapter.map('SettingsApp.Node', {
    nodeType:   {key: 'type'},
    outputs:    {embedded: 'always'},
    properties: {embedded: 'always'}
});

(outputs is part of the NodeMixin)

What are my options of modelling this with ember-data 1.0 beta? I have no idea what to do with these models, and I have around a dozen of them. It was hard enough to shove my record structure into ember-data, and now ... puff, effort gone, it just does not work anymore.

like image 594
blueFast Avatar asked Sep 05 '13 09:09

blueFast


Video Answer


1 Answers

If you want to just use properties and outputs as raw JSON data you can declare them as non-typed DS.attr and they will be passed through as is.

SettingsApp.Node = DS.Model.extend(SettingsApp.NodeMixin, {
    properties : DS.attr(),
    outputs : DS.attr()
});
like image 88
Jeremy Green Avatar answered Nov 01 '22 12:11

Jeremy Green