I added the example application.
http://jsfiddle.net/Sly7/amG56/
Js:
App = Ember.Application.create();
App.ApplicationController = Ember.ArrayController.extend({
selectedBook: null
});
App.ApplicationView = Ember.View.extend({
actions: {
selectBook: function(book) {
this.get('controller').set("selectedBook", book);
},
cancel: function(book) {
alert(book);
}
}
});
App.Book = Em.Object.extend({
name: null
});
Template:
<script type="text/x-handlebars">
{{#each book in books}}
<a {{action "selectBook" book target="view"}} href="#">select {{book.name}}</a><br />
{{/each}}
<hr />
Selected Book: {{selectedBook.name}}
<br />
<a {{action "cancel" selectedBook target="view"}} href="#">cancel selected book</a>
</script>
Select one of the books. You will see that name of the book will be displayed. But the “cancel selected book” link does not work.
I think the problem is context of the action helper does not change when a book is selected.
How do I implement an action helper which has a changing context? Or is it a bug?
The answer is in the guides
http://emberjs.com/guides/templates/actions/#toc_action-parameters
And the context is lazily evaluated, so the problem does not occur anymore
DEPRECATED ANSWER BELOW
The problem here is that the action helper is interpreted with the selectedBook context. But at this time, selectedBook is null. So when clicking on the link, even if you previously select a book, it’s too late, for the registered action, the context is still null.
As a workaround, you can enclose this with a {{with}} block.
see: http://jsfiddle.net/x82dr/17/
BTW, you can see the code of the ApplicationView, where I access the application controller, using the controller property. With Ember.js convention, the controller is injected to the view when the application initialize
UPDATE: The use of the {{with}} helper seems to be not mandatory now, see: https://github.com/emberjs/ember.js/issues/1150