简体   繁体   中英

Ember - registerHelper else condition always renders twice

I am attempting to create a handlebars block helper for use in an Ember project. It is partially working giving the if/else results that I expect. However, the content of the else block in the template is rendered twice in addition to the desired output in all situations.

Here is the helper

Ember.Handlebars.registerHelper 'ifIn', (a, b, options) ->
  Ember.Handlebars.bind.call options.contexts[0], a, options, true, (item) =>
    Ember.Handlebars.bind.call options.contexts[0], b, options, true, (list) =>
      if item in list
        options.fn()
      else
        options.inverse()  

And here is a JS Bin that demonstrates the problem. ---> JS Bin

After fiddling with your jsbin for a bit, I've come to the conclusion that the way you're attempting to handle this helper is sort of strange.

  • The multiple bind-call on the options.context objects isn't really needed. You just need to get the comparator property in the class (options.context[0].get('notFoundColor')) and compare it to the items in the list (options.context[0].get('colors')) .

  • Why not use a string in the helper, to make your life easier? eg; the helper would become just comparing the A to the B.list, rather than having to dive into a computed property. But if that's a requirement, just use the option in the first bullet.

  • The provided parameters a, b to your helper are just strings. You'll have to write the logic in the helper on how to handle those.

I'd suggest not having the helper do too much in this case. pass a string for the property you want to evaluate, and get() the property you want to use as a comparator, and use the helper to do the logic you want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM