问题
I'm starting with ember.js and ran into trouble trying out things in the ember.js docs (http://docs.emberjs.com/symbols/Handlebars.helpers.html#.collection).
This application code:
App = Ember.Application.create()
App.items = [
Ember.Object.create({name: 'Dave'}),
Ember.Object.create({name: 'Mary'}),
Ember.Object.create({name: 'Sara'})
]
with this template code:
{{#collection contentBinding="App.items"}}
Hi {{content.name}}
{{/collection}}
Results in this:
<div id="ember122" class="ember-view">
<div id="ember142" class="ember-view">
<div id="ember149" class="ember-view"> Hi
<script id="metamorph-0-start" type="text/x-placeholder">
</script>
<script id="metamorph-0-end" type="text/x-placeholder">
</script>
</div>
<div id="ember150" class="ember-view"> Hi
<script id="metamorph-1-start" type="text/x-placeholder">
</script>
<script id="metamorph-1-end" type="text/x-placeholder">
</script>
</div>
<div id="ember151" class="ember-view"> Hi
<script id="metamorph-2-start" type="text/x-placeholder">
</script>
<script id="metamorph-2-end" type="text/x-placeholder">
</script>
</div>
</div>
</div>
So, it effectively iterates through the objects, but fails to return the name values for each. Why is this? Are the docs outdated, perhaps? Is there a better way to achieve the desired result (iterating through the items)? Thanks for your answers.
回答1:
I think you are using ember-latest, or 1.0-pre, in this case, you have to prefix content.name
with view
. Since 1.0 pre, the retrieval of the context change. see: Ember.js: Handlebars displays nothing for furhter explanations.
That beeing said, the documentation is not up to date. Could you post a pull request with the fix?
回答2:
This example work well: http://jsfiddle.net/secretlm/qD6az/3/
Hope this helps.
来源:https://stackoverflow.com/questions/12028209/content-name-returns-empty-for-collection