content.name returns empty for collection

只谈情不闲聊 提交于 2019-12-24 12:24:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!