Steering Assistant inside {{#each}}

I am trying to call a registered helper helper inside the loop {{#each}} . Unfortunately, Ember.js complains because it tries to allow the helper as a property of the controller, not the helper.

 Handlebars.registerHelper('testHelper', function(name) { return 'foo: ' + name }); 

(names and contents are just dummy values ​​to show an example)

 {{#each entry in App.testController}} <div>{{{testHelper entry.name}}}</div> {{/each}} 

The error Ember.js is printing:

 Uncaught Error: Handlebars error: Could not find property 'testHelper' on object <App.testController:ember254>. 

How do I need to call a registered assistant so that it is recognized?

+8
source share
3 answers

If it works, or is it a solution ,

Javascript

 Handlebars.registerHelper('testHelper', function(property, options) { return 'foo: ' + Ember.get(options.data.view.content, property); }); 

Handlebars Pattern

 <script type="text/x-handlebars" data-template-name='app-view'> <ul> {{#each entry in content}} <li>{{testHelper name}}</li> {{/each}} </ul> </script>​ 

Or better yet, with this:

Javascript

 Handlebars.registerHelper('testHelper', function(property) { return 'foo: ' + Ember.get(this, property); }); 

Handlebars Pattern

 <script type="text/x-handlebars" data-template-name='app-view'> <ul> {{#each entry in content}} {{#with entry}} <li>{{testHelper name}}</li> {{/with}} {{/each}} </ul> </script>​ 
+9
source share

My helpers are written in separate files, so I changed @MikeAski's answer to be as follows.

In helpers/my-helper.js :

 var MyHelper = function(value) { return moment(value).format("MMMM Do, YYYY"); }; export default MyHelper; 

At the top of app.js :

 // import modules import myHelper from 'appkit/helpers/my-helper'; // register custom helpers Ember.Handlebars.registerBoundHelper('myHelper', myHelper); 

Then you don’t even need {{#with}} in descriptors, just use it as a regular helper.

 {{#each thing}} {{myHelper thing.foo}} {{/each}} 
0
source share

If you do not want to use the global assistant, you can use the "corrected request":

 {{#each entry in App.testController}} <div>{{{../testHelper entry.name}}}</div> {{/each}} 

../ is the syntax for the corrected request. This forces you to go through the level 1 level tree and access personal data from the parent area. To go up 2 levels, you can do the following ../../ . This is useful if you have nested for-loops.

0
source share

All Articles