I have a simple app where you can login it looks like this:
{{#view App.mainV}}
{{#if logged}}
Hey!
{{else}}
<!-- Login form here when clicked: App.mainC.login()-->
{{/if}}
{{/view}}
Here is sample controller:
App.mainC = Ember.ArrayController.create({
login: function(){
if(ok)//Everything went fine
App.mainV.logged = true;
}
});
Here is mainV:
App.mainV = Ember.View.extend({
logged: false,
test: function()
{
console.log('JO');
}
});
I have two questions regarding this app:
- Why when I change logged to true view doesn’t change?
-
If I call App.mainV.test() I get an error. Why?
TypeError: ‘App.mainV.test’ is not a function
If you want to access a view’s property from a template, you have to prefix with “view.” In your example:
{{#if view.logged}}.You can’t do
App.mainV.test()becauseApp.mainVis a class, not an instance. You could doApp.mainV.create().test().And you should rename
App.mainVtoApp.MainVto fit Ember.js conventions (class names should be capitalized, see http://www.emberist.com/2012/04/09/naming-conventions.html)Edit:
There’s another problem in your example: the controller tries to modify a value in the view. In the Ember way, you should bind a view’s property to the controller. Any change in the controller would be propagated to the view: