Question

I may add actions within handlebars templates for emberjs with

<li>{{action "SomeAction" this}} {{this.name}}</li>

the SomeAction is called.

How may I add an action within an custom helper (this is only an example, I have more code in there):

Ember.Handlebars.registerHelper('foo', function(property, options) {
  var bar = Ember.Handlebars.get(this, property, options);

  var args = Array.prototype.slice.call(arguments, 1);
  args.unshift("someAction");
  args.unshift(bar);
  var action = Ember.Handlebars.helpers.action.apply(this, args);

  return new Ember.Handlebars.SafeString("<li "+new Ember.Handlebars.SafeString(action)+">"+bar.get("name")+"</li>");
});

This creates the same li tag, but it is not working, with this error when i click on the li tag (error message is from discourse app):

Uncaught Error: Nothing handled the action ''. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble.

Was it helpful?

Solution

The error indicates that no controller or route of the current context handles the specified action i.e. does not contain an actions property with a function specified in foo helper.

Example, http://emberjs.jsbin.com/zudiqewo/1/edit

js

Ember.Handlebars.registerHelper('foo', function(property, options) {

  var action = Ember.Handlebars.helpers.action.apply(this, arguments);

  return new Ember.Handlebars.SafeString("<li "+new Ember.Handlebars.SafeString(action)+">click here</li>");
});

if in hbs template the test is renamed from {{foo "test"}} to {{foo "teest"}} the aforementioned error will be thrown.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top