Problem using custom REST adapter for one of the app models


#1

I am trying to build a small emberjs app that will first get auth key from the server (using REST Api - Post), then use this auth key in pulling data from server using REST Api - Get. So far the application authenticate and return auth key from server, but that as far as it can go. When trying to pull data for a specific model nothing is returned at all. So I was wondering if someone can please help me by telling me what I am missing here and how to fix it? Thanks in advance

App = Ember.Application.create({
    LOG_TRANSITIONS: true, 
    LOG_TRANSITIONS_INTERNAL: true,
    RAISE_ON_DEPRECATION : true,
    LOG_STACKTRACE_ON_DEPRECATION : true
});

App.Router.map(function() {
  this.resource('employees', { path: '/employees'} , function() {
  });
});

App.IndexRoute = Ember.Route.extend({
  redirect: function() {
    this.transitionTo('employees');
  }
});

App.EmployeesRoute = Ember.Route.extend({
  model: function() {
    return App.Employee.find();
  }
});

App.EmployeeIndexRoute = Ember.Route.extend({
  model: function() {
    return this.modelFor('employee');
  }
});

App.EmployeesController = Ember.ArrayController.extend(
);

App.EmployeeFormView = Ember.View.extend({
  templateName: 'employee/form',
  tagName: 'form',

  didInsertElement: function() {
    this.$('input:last').focus();
  },

  submit: function() {
    this.get('controller').send('saveEmployee');
    return false;
  }
});

var tokenData = 'Null';      
    $.ajax
      ({
        type: "POST",
        url: "http://demo.domainname.com/authen",
        dataType: 'json',
        async: false,
        data: JSON.stringify({ 
            Login: "email@email.com", 
            Password : "test"
        }),
        success: function(data) {
            tokenData = data;
            console.log(data);
        },
        error: function(xhr, error){
            tokenData = 'Error!';
            console.debug(xhr); console.debug(error);
        } 
    });   
var tokenString = tokenData.Token;

App.Adapter = DS.RESTAdapter.reopen({
  bulkCommit: false,
  url: 'http://demo.domainname.com/employees',  
  corsWithCredentials: true,
  headers: {
    'Accept': 'application/vnd.vendor+json;version=1',
    'Content-type': 'application/json',
    'AuthKey': '\''+tokenString+'\''
  },
  ajax: function(url, type, hash) {
    if (this.headers !== undefined) {
      var headers = this.headers;
      if (hash) {
        hash.beforeSend = function (xhr) {
          alert('In before send'); //This is never reached..
          Ember.keys(headers).forEach(function(key) {
            xhr.setRequestHeader(key, headers[key]);
          });
        };
      }
    }
    return this._super(url, type, hash);
  }
});


App.Store = DS.Store.extend({ 
    revision: 11,
    adapter: App.Adapter.create() 
}); 

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

#2

You can use jQuery to set global headers after you logged in.

or ember way.


#3

As you can see from my code I’ve tried to follow jquery way and it didn’t work. Kindly take a moment to read my code, I have posted all what’s inside app.js, may be you can see a mistake I can’t see Thanks