在Ember控制器中使用不同的型号

时间:2014-05-23 20:51:39

标签: ember.js

我试图绕过Ember.js,但没有抓住它。

我有一个HolidaysNewRoute我要在这里显示本月的日历,您可以点击一天来选择或取消选择它。您可以将其保存以显示您将在假期中的日期。

App.HolidaysNewRoute = Ember.Route.extend({});

App.HolidaysNewController = Ember.ArrayController.extend({
  days: function() {
    return [
      new App.Day({index: 1}),
      ...
    ];
  }.property()
});

<script type="text/x-handlebars" id="holidays/new">
   {{#each day in days}}
     {{day}}
   {{/each}}
</script>

这会在模板的<App.Day:ember354:null>占位符中显示{{day}}之类的内容。现在,我想显示日期索引(本例中为1)所以:

 ...snip...
   {{day.index}}
 ...snip...

但是Ember抱怨我在未定义的对象上调用index

我做错了什么?一般结构应该是什么,还是我试图解决错误的问题?我可以将'假日'重命名为'day',但我尝试创建的资源是带有日期的假日,而不是一个日期。

1 个答案:

答案 0 :(得分:1)

我假设您使用App.Day或通过扩展Ember.Data创建了Ember.Object模型。在这种情况下,您创建的实例是错误的。而不是new App.Day({index: 1}),请尝试以下操作:App.Day.create({index: 1})

至于你是否做得对,很难说没有详细的代码审查。在你到目前为止发布的内容中,我没有看到任何危险信号。