Implementing custom ajax calls when loading data into a model

In my Emberjs application, I have an Employee model that I have to load through a REST Get API call, where I must first authenticate the API for the token and then start loading the data, I know how to do it with jQuery, but I don’t know how I can implement this in EmberJS, so I will be very grateful if anyone can teach me how to do this.

Below is the jQuery code that I use for authentication, retrieving employee data, as well as EmberJS model code

thank

Authentication:

 $.ajax
  ({
    type: "POST",
    url: "http://portal.domainname.com/auth",
    dataType: 'json',
    async: false,
    data: JSON.stringify({ 
        Login: "logmein@email.com", 
        Password : "test"
    }),
    success: function(data) {
        console.log(data); //Here I get the token needed for further calls...
    },
    error: function(xhr, error){
        console.debug(xhr); console.debug(error);
    } 
});

Allows you to download employee data:

$.ajax   ({
    type: "GET",
    url: "http://portal.domainname.com/employees",
    dataType: 'json',
    async: false,
    beforeSend: function (xhr) {
        xhr.setRequestHeader ("Token", "0000000-0000-0000-0000-00000000");
    },
    success: function(data) {
        console.log(data);
    },
    error: function(xhr, error){
        console.debug(xhr); console.debug(error);
    }  });

Model EmberJS

App.Store = DS.Store.extend({
  revision: 11
});

App.Employee = DS.Model.extend({
  employeeID:             DS.attr('string'),
  employeeName:        DS.attr('string')
});

App.Store.reopen({
 adapter: 'DS.RESTAdapter'
});
+3
source share
4 answers

You can add headers to all Ember AJAX requests as follows:

App.Store = DS.Store.extend({
  revision: 13,
  adapter: DS.RESTAdapter.extend({
    ajax: function(url, type, hash) {
      if (!hash) {
        hash = {};
      }
      hash.beforeSend = function(xhr) {
        xhr.setRequestHeader("Authorization", "Token " + window.sessionToken);
      };
      return this._super(url, type, hash);
    }
  })
});

.

+2

- EmberData ajax , . Discourse ( Ember Ember Data):

http://eviltrout.com/2013/03/23/ember-without-data.html

+1
0

- :

App.ApplicationAdapter = DS.RESTAdapter.extend({
  setHeaders: function() {
    this.set('headers', { "Token": "0000000-0000-0000-0000-00000000" });
  }.on('init');
});

, ember-data-1.0.0-beta.x .

0

All Articles