小编Jon*_* M.的帖子

编写一个产生绑定结果的帮助器?

我有一个日期/时间格式助手,但它产生的内容在基础属性更改时不会更新.这并不奇怪,但有谁知道如何在助手中生成绑定?

我像这样调用助手......

{{timestamp created_at}}
Run Code Online (Sandbox Code Playgroud)

......这是助手本身:

Handlebars.registerHelper('timestamp', function(context, options) {
  var formatter        = options.hash['format'] ? options.hash['format'] : 'hh:mm a MM-DD-YYYY';
  var original_date    = Ember.getPath(this, context); // same as this.get(context) ?
  var parsed_date      = moment(original_date);
  var formatted_date   = parsed_date.format(formatter);

  return new Handlebars.SafeString("<time datetime=" + original_date +">" + formatted_date + "</time>");
}); 
Run Code Online (Sandbox Code Playgroud)

mustache ember.js

13
推荐指数
2
解决办法
4343
查看次数

使用Ember + Handlebars在运行时动态选择视图

我使用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}}
Run Code Online (Sandbox Code Playgroud)

......还有帮手......

Handlebars.registerHelper('is', function(options) {
  if (this.constructor == options.hash["constructor"]) {
    return options.fn(this);
  }
});
Run Code Online (Sandbox Code Playgroud)

我宁愿依靠一些约定来确定要渲染的视图.例如:

<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>
Run Code Online (Sandbox Code Playgroud)

也许数据模型属性可用于确定对象的呈现方式.

{{#view App.SelectedAccountView contentBinding="App.selectedAccountController.everythingSorted"}}
  {{#with content}} …
Run Code Online (Sandbox Code Playgroud)

handlebars.js ember.js

5
推荐指数
2
解决办法
3683
查看次数

标签 统计

ember.js ×2

handlebars.js ×1

mustache ×1