Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 626
kunerd Avatar asked Nov 07 '13 12:11

kunerd


People also ask

How is metadata used?

Simply defined, metadata is the summary and the description about your data that is used to classify, organize, label, and understand data, making sorting and searching for data much easier. Without it, companies can't manage the huge amounts of data created and collected across an enterprise.

What is Adapter in Ember JS?

In Ember Data, an Adapter determines how data is persisted to a backend data store. Things such as the backend host, URL format and headers used to talk to a REST API can all be configured in an adapter. Ember Data's default Adapter has some built-in assumptions about how a REST API should look.


2 Answers

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();
  }
});
like image 171
Kingpin2k Avatar answered Sep 28 '22 15:09

Kingpin2k


Creating a custom serializer and overriding serialize as @Kingpin2k works if you're OK with the meta data being part of your user payload. Example:

{
  "user":{
    "firstName":"Jim",
    "meta":{
      "password":"my password"
    }
  }
}

But if you want 'meta' to follow the same structure as the server response, then you want to override serializeIntoHash instead:

App.UserSerializer = DS.RESTSerializer.extend({
    serializeIntoHash(data, type, record, options) {
        this._super(data, type, record, options);
        data.meta = {
            password: record.get('password')
        };
    }
});

This will send a payload that looks like this to the server:

{
  "user":{
    "firstName":"Jim"
  },
  "meta":{
    "password":"my password"
  }
}

You'll still need to set the password on the model as @Kingpin2k suggests:

model.set('password', 'my password');
like image 25
Johnny Oshika Avatar answered Sep 28 '22 14:09

Johnny Oshika