Search code examples
ember.jsember-data

ember data inject meta data on save


I'm developing a Ember.js app and at the moment I try to implement user registration. Until now I found two ways to get this working, but both have some disadvantages.

One way is to have a registration controller, which does a custom ajax post with email, password and password confirmation values to the server. I don't really like this custom ajax call at this place, I would prefer to use a EmberData user model and save it, but I didn't want to store the password and password confirmation values on client side.

I found out, that there is a way to receive meta-data for models from the server, so my question is, is there also a way to save meta-data to the server?

Other solution without storing the passwords on client-side are also welcome.


Solution

  • Sure, create a custom serializer and override serialize

    App.UserSerializer = DS.RESTSerializer.extend({
      serialize: function(record, options) {
        var json = this._super(record, options);
    
        // assuming niner isn't an attr on the model definition
        // just a value added to the model that I want to include in the meta data  
        json.meta = { typedPass: record.get('typedPass') };  
    
        return json;
      }
    });
    

    BTW, you definitely shouldn't store, nor transmit the password to the client at all. I'm assuming you are writing some sort of login page though and don't intend on doing this, I'm just writing it as a CYA.

    // No password defined, it will only send up the id and the user property.

    App.User = DS.Model.extend({
      user: DS.attr()
    });
    
    App.ApplicationRoute = Em.Route.extend({
      model: function(){
        var user = this.get('store').createRecord('user', {user:'cow'});
        user.set('typedPass', 'some crazy cow password'); // the default serializer wouldn't attach this when you save
        user.save();
      }
    });