samedi 26 août 2017

How to get a value from a promise in EmberJS with custom Adapter

I am pretty new to EmberJS, I am trying to get back a value from a promise given by a custom adapter since the backend is not rails API compliant

Here is my custom adapter:

import DS from 'ember-data';
import Ember from 'ember';

    export default DS.Adapter.extend({
      findRecord( store, type, id ) {
        return Ember.$.getJSON("http://localhost:8000/user/by-username/" + id );

  }
});

I also modified my serializer:

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  primaryKey:'alias',
});

my controller looks like this (where I am stuck):

import Ember from 'ember';

export default Ember.Controller.extend({
  username:'',
  actions: {
    searchUser : function(){
      var id = this.get('username');
      var promise = this.get('store').findRecord('roles', id);
      var self = this
      promise.then(function(value) {
        self.set('username', value.get('ID'));
      }, function(reason) {
        // on rejection
      });


    }
  }
});

Specifically on this line from above controller, I can not get it to work:

self.set('username', value.get('ID'));

The ajax is exectued succesfully since I can see it happen on chrome dev tools returning json following data:

{"ID":"73ebf44f-71bf-4b60-a755-a4bbef208ef1","mjID":34049137,"alias":"isgarbe","registration_date":"2017-07-07T20:03:11Z","information":{"payer":false,"last_payment_date":null,"show_tutorial":false,"last_game_played_game_id":null,"last_game_played_date":null,"last_application_connection_close_date":null,"social_links":null,"payment_count":0},"avatars":{"base_url":"http://ift.tt/2wPLJhh","asset_id":"0a8d02c4278000100000d0000000000580000000000849480020000048888888888800","sizes":["cabecera","normal","medio","maxi"]},"gender":"Female","economy":{"accountId":"34049137","balance":2000000000,"currency":"chips"},"experience":{"user_id":34049137,"level":1,"points":0,"loops":0,"max_points_by_level":20,"min_points_by_level":0},"payment_segment":1000,"security_key":null,"role":null}

I am not able to get the ID value to be set to the username property




Aucun commentaire:

Enregistrer un commentaire