Reputation: 201
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:
If I call App.mainV.test() I get an error. Why?
TypeError: 'App.mainV.test' is not a function
Upvotes: 2
Views: 2418
Reputation: 810
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()
because App.mainV
is a class, not an instance. You could do App.mainV.create().test()
.
And you should rename App.mainV
to App.MainV
to 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:
<script type="text/x-handlebars">
{{#view App.MainView}}
{{#if view.logged}}
Hey!
{{else}}
<!-- Login form here when clicked: App.mainController.login()-->
{{/if}}
{{/view}}
</script>
App = Ember.Application.create();
App.mainController = Ember.Controller.create({
logged: false,
login: function() {
this.set('logged', true);
}
});
App.MainView = Ember.View.extend({
loggedBinding: 'App.mainController.logged'
});
// change in controller will be propagated to the view
App.mainController.set('logged', true);
Upvotes: 7