Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js Handlebars.helpers.collection not working

I'm new to ember and I'm trying to play with the code a bit so I downloaded the starters kit from the website as a starting template.

I'm trying to use the ember handlebars helper for CollectionView, I copy paste the sample code but all I see is "Hi" 3 times without the name:

http://docs.emberjs.com/symbols/Handlebars.helpers.html#method=.collection

what am I doing something wrong?

<script type="text/x-handlebars">
    {{#collection contentBinding="App.items"}}
        Hi {{content.name}}
    {{/collection}}
</script>


<script>
App = Ember.Application.create();

App.items = [
    Ember.Object.create({name: 'Dave'}),
    Ember.Object.create({name: 'Mary'}),
   Ember.Object.create({name: 'Sara'})
]
</script>

Thanks!

like image 774
Roman Krom Avatar asked Jan 30 '26 15:01

Roman Krom


1 Answers

It's a problem about the way context is managed in the latest version of ember that is explained at:

content.name returns empty for collection

In summary, you need to do this in your template instead:

<script type="text/x-handlebars">
    {{#collection contentBinding="App.items" }}
        Hi {{view.content.name}} 
    {{/collection}}
</script>​

Fiddle showing it: http://jsfiddle.net/XdHRS/

For the same purpose you could just use each:

<script type="text/x-handlebars">
    {{#each App.items }}
        Hi {{name}} 
    {{/each}}
</script>​

Fiddle showing it: http://jsfiddle.net/e3UTt/13/

like image 192
txominpelu Avatar answered Feb 01 '26 05:02

txominpelu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!