【发布时间】:2012-01-11 18:31:23
【问题描述】:
我正在使用 Ember、Ember Data 和 Handlebars 来显示具有多种不同类型模型的时间线。我当前的实现虽然运行正常,但似乎可以通过约定和帮助程序大大改进。但是,我不知道如何使用已定义的模板。
这就是我所拥有的:
{{#view App.AccountSelectedView contentBinding="App.selectedAccountController.everythingSorted"}}
{{#with content}}
<ol class="timeline">
{{#each this}}
{{#is constructor="App.Design"}}
... stuff about the design
{{/is}}
{{#is constructor="App.Order"}}
... stuff about the order
{{/is}}
{{#is constructor="App.Message"}}
... stuff about the message
{{/is}}
{{/each}}
</ol>
{{/with}}
{{/view}}
...还有一个助手...
Handlebars.registerHelper('is', function(options) {
if (this.constructor == options.hash["constructor"]) {
return options.fn(this);
}
});
我宁愿依靠一些约定来确定要呈现的视图。例如:
<script type="text/x-handlebars-template" data-model="App.Design" id="design-view">
... stuff about the design
</script>
<script type="text/x-handlebars-template" data-model="App.Order" id="order-view">
... stuff about the order
</script>
也许 data-model 属性可用于确定对象的呈现方式。
{{#view App.SelectedAccountView contentBinding="App.selectedAccountController.everythingSorted"}}
{{#with content}}
<ol class="timeline">
{{#each this}}
{{viewish this}}
{{/each}}
</ol>
{{/with}}
{{/view}}
唉,我不知道如何从帮助程序访问模板。
Handlebars.registerHelper('viewish', function(options) {
// Were I able to access the templates this question
// would be unnecessary.
// Handlebars.TEMPLATES is undefined...
});
另外,这是我应该对 Handlebars 做的事情吗?
【问题讨论】: