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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top