0
votes

I think i have a fundamental problem of understanding how to access vars in a View form other controllers.

I read the Ember-Documentation many times and "hundreds" of Blog-Entries, but i did not find a solution.

After Submitting a Loginform, i want to set the var "isLoggedIn" from Auth Controller / Model and output it in the Login View. Auth will be used later from many other Components, thats why i want to separete it from Login.

Here is a small part of my code:

Template: Login

Authenticated: {{controllers.auth.isLoggedIn}} <--- after Login this should be TRUE, but how ?
...LoginForm: here is the login form with input fields (email and password)...

Controller: Login

var LoginController = Ember.Controller.extend({
  needs: "auth",
  // LoginForm Submit-Event
  login: function() {
    App.Login.createRecord(this.getProperties("email", "password"));
    DS.defaultStore.commit(); // on server respond, id of model.login is changed, see "idObserver"
  }
});

Model: Login

var Login = DS.Model.extend({
  email     : DS.attr("string"),
  password  : DS.attr("string"),
  // Because of Ember Bug i have to use idObserver after "DS.defaultStore.commit()"
  // to get the ID responded from the server.
  idObserver: function() {
    var auth = App.Auth.create();
    auth.set("id", this.get("id"));
  }.observes("id")
});

Controller: Auth

var AuthController = Ember.Controller.extend({
  isLoggedIn: false // What should i write here to connect to isLoggedIn of Auth.Model ???
}); // Controller

Model: Auth

var Auth = Ember.Object.extend({
  isLoggedIn: DS.attr("boolean"),
  idObserver: function() {
    if(this.get("id")) this.set("isLoggedIn", true);
    else this.set("isLoggedIn", false);
  }.observes("id")
});

How can i output "controllers.auth.isLoggedIn" in the Login Template ?

1

1 Answers

1
votes

Normally the instance of the auth model would be set on the content property of your auth controller (typically handled by the router). Once the content is set then your template would work with no modifications.

This gist might help you out as it as a login example using the router:

https://gist.github.com/machty/5647589