jeudi 25 février 2016

Ember - How to pass a property from Routes file to a Handlebar template with 401 returned

The situation is I have a login screen which works except when there is a failed login (401 not authorized). Basically it is very simple right now, the handlebar (template) is trying to access a property from the route to determine if the call to the back end (rails) failed.

{{#if loginFailed}}
  <div class="alert">Invalid username or password.</div>
{{/if}}

The Route file looks something like this, I have omitted any sensitive code or working code that is not needed:

import Ember from 'ember';
import ajax from 'ic-ajax';

export default Ember.Route.extend({

loginFailed: false,
isProcessing: false,

beforeModel: function(){
    //some stuff 
},

actions: {
login: function() {
    this.setProperties({
      loginFailed: false,
      isProcessing: true
    });
    var _this = this;

    ajax({
        url: //something,
        type: 'post',
        crossDomain: true,
        data: //some stuff,
  }).then(
            function(result) {
                //logging in logic all works
            },
            function(error){
                if (error.jqXHR.status === 401)
                {

                    _this.set('isProcessing', false);
                    _this.set("loginFailed", true);
                }
            }
        );


  },
},

reset: function() {
  this.set('isProcessing', false);
  this.controller.set('password', '');
  this.controller.set('username', '');
}
});

I've done some debugging and it does indeed hit the error block for the ajax promise however it just seems to crap out. I believe it craps out because it's 401, limited resources have led me to this conclusion.

You can see that loginFailed is the property I am trying to change and when 401 error happens, and on the template this is the property I am trying to access.

I am very new to Ember just working on it for little over a week now so any help would be amazing.

Here are the versions I am using when running ember -v on my project:

version: 1.13.8
node: 0.12.7
npm: 2.13.4
os: win32 x64




Aucun commentaire:

Enregistrer un commentaire