Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-persistent attributes in EmberJS

Tags:

ember.js

Does anyone know of a way to specify for an Ember model an attribute which is not persisted?

Basically, we're loading some metadata related to each model and sending that data to Ember via the RESTAdapter within the model. This metadata can be changed in our app, but is done via using an AJAX call. Once the call succeeds, I want to be able to update this value within the model without Ember sticking its nose in this business by changing the model to the uncommitted and doing whatever it does with transactions behind the scenes.

I also have the problem that this metadata, which is not data from the model's database record, is passed by the RESTAdapter back to the server, which doesn't expect these values. I am using a RoR backend, so the server errors out trying to mass-assign protected attributes which aren't meant to be attributes at all. I know I can scrub the data received on the server, but I would prefer the client to be able to distinguish between persistent data and auxiliary data.

So, to the original question: is there any alternative to Ember-Data's DS.attr('...') which will specify a non-persistent attribute?

like image 235
thy_stack_overfloweth Avatar asked May 16 '13 13:05

thy_stack_overfloweth


1 Answers

The other answers to this question work with Ember data versions up to 0.13, and no longer work. For Ember data 1.0 beta 3 you can do:

App.ApplicationSerializer = DS.RESTSerializer.extend({
  serializeAttribute: function(record, json, key, attribute) {
    if (attribute.options.transient) {
      return;
    }
    return this._super(record, json, key, attribute);
  }
});

Now you can use transient attributes:

App.User = DS.Model.extend({
  name: DS.attr('string', {transient: true})
});

These attributes won't be sent to the server when saving records.

like image 58
Yossi Shasho Avatar answered Oct 02 '22 12:10

Yossi Shasho