Represent a transient association model in Ember.Data


#1

I’m currently using the ActiveRecordAdapter in my app. One of the models has a server-side generated array of values for building a sparkline. So the model looks something like:

{
  my_obj: {
    id: 1,
    name: 'My Name',
    persisted_things: [ 1, 2, 3 ],
    non_persisted_things: [
      { 
        month: 'Jan',
        values: [1, 2, 3, 1, 6, 1, 2, 3, 1, 5, 6 ]
      },
      {
        month: 'Feb',
        values: [1,2,3,1,5,1,2,3,4,1,2]
      }
    ]
  },
  persisted_things: [ { ... side loaded things ... } ]
}

The non-persisted things represent values calculated on the server side for visualization (there’s a few dozen of them with hundreds of data points in the real app, I’ve made it just two for the example.).

Currently my model in Ember looks something like:

App.MyThing = DS.Model.extend({
  name: DS.attr('string'),
  persisted_things: DS.hasMany('persisted_thing'),
  non_persisted_things: DS.attr(),
  non_persisted_things_as_objects: function() {
    var npt = this.get('non_persisted_things');
    ... create simple JS object here ...
    return temporary_objects;
  }.property('non_persisted_things')
});

The question is, is it possible to treat my non-persisted things are Model objects so that I could treat them the same as I’d treat my persisted things association (except that I would never save this association.) In other words, the model would look like:

App.MyThing = DS.Model.extend({
  name: DS.attr('string'),
  persisted_things: DS.hasMany('persisted_thing'),
  non_persisted_things: DS.hasMany('non_persisted_thing')
});    

Hope this makes sense. Thanks for all the hard work on the framework.


#2

If any interest, my solution was simple. I was returning my non_persisted_things as part of the parent. After sleeping on it, I realized they were really a transient attribute of an existing 1:N association on the server / rails side. So, I simply moved them there and it works fine.