vendredi 4 septembre 2015

Accessing Ember Controller Properties within the same controller

I'm very new to EmberJS 2.0 and trying to slowly understand it by building my own website with it. Anyways, I've managed to get Firebase integrated with Ember and my controller is able to authenticate correctly. However, I'd like to understand why when I execute:

this.send('toggleModal');

inside the authenticate action property function (.then()) it doesn't work but if I execute it outside then everything works fine.

1) Is the 'this' keyword getting confused with something other than the Ember controller?

Here is the sample:

// /app/controllers/application.js
import Ember from 'ember';

export default Ember.Controller.extend({
  isShowingModal: false,
  actions: {
    toggleModal: function() {
      this.toggleProperty('isShowingModal');
    },
    authenticate: function(username, pass) {
      this.get('session').open('firebase', {
        provider: "password",
        email: username,
        password: pass
      }).then(function (data) {
        console.log(data.currentUser);
        console.log(session.isAuthenticated); //Why is 'session' not defined?
        this.send('toggleModal'); //This doesn't work. Throws an error.
      });

      this.send('toggleModal'); //This works.
    },
    logOut: function() {
      this.get('session').close();
    }
  }
});

2) Also, I've noticed that when using Emberfire I'm able to use the property 'session.isAuthenticated' within the template application.hbs however, shouldn't 'session' be an object that is injected to all routes and controllers using Torii? Why is that property inaccessible/undefined within the application.js controller? I'm using http://ift.tt/1N999QT as a reference.

3) In the guide above the actions for authentication are put inside the route. However, according to this quora post the route should only handle template rendering and model interfacing. Is this post incorrect? The authentication logic should reside in the application.js controller correct? http://ift.tt/1NfQ28e

Aucun commentaire:

Enregistrer un commentaire