Tôi đang sử dụng Ember, Ember Data và Handlebars để hiển thị dòng thời gian với một số loại mô hình khác nhau. Thực hiện hiện tại của tôi, mặc dù hoạt động đúng, có vẻ như nó có thể được cải thiện đáng kể với một quy ước và một người trợ giúp. Tuy nhiên, tôi không thể tìm ra cách sử dụng các mẫu đã được xác định.Tự động chọn chế độ xem khi chạy với Ember + Handlebars
Đây là những gì tôi có:
{{#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}}
... cùng với một helper ...
Handlebars.registerHelper('is', function(options) {
if (this.constructor == options.hash["constructor"]) {
return options.fn(this);
}
});
Tôi thà dựa vào một số ước để tìm ra những gì xem để render. Ví dụ:
<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>
Có thể sử dụng thuộc tính mô hình dữ liệu để xác định cách đối tượng được hiển thị.
{{#view App.SelectedAccountView contentBinding="App.selectedAccountController.everythingSorted"}}
{{#with content}}
<ol class="timeline">
{{#each this}}
{{viewish this}}
{{/each}}
</ol>
{{/with}}
{{/view}}
Than ôi, tôi không thể tìm ra cách truy cập các mẫu từ trình trợ giúp.
Handlebars.registerHelper('viewish', function(options) {
// Were I able to access the templates this question
// would be unnecessary.
// Handlebars.TEMPLATES is undefined...
});
Ngoài ra, đây có phải là điều tôi nên làm với Handlebars không?