从Ember中的控制器访问模型 [英] Accessing a model from a controller in Ember

查看:103
本文介绍了从Ember中的控制器访问模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的设置,我想在我的Controller中格式化日期。问题是我无法在下面的 formattedStart 函数中访问它,而我可以在 summaryRowAction 处理程序中访问它。这让我很困惑,因为这两个地方的console.logging 这个给出了相同的结果。但是由于某些原因, formattedStart this.get('model.startDate')未定义。

  App.SummaryRowController = Ember.ObjectController.extend({
formattedStart:function(){
console.log this.get('model.startDate');)
return this.get('model.startDate');
} .property(),

actions:{
summaryRowAction:function(){
console.log(this.get('model.startDate'));
}
}
});

这是我的模型和我的模板(在玉中)供参考:

  App.PricingSummary = DS.Model.extend({
startDate:DS.attr(),
endDate:DS.attr )
days:DS.hasMany('day',{async:true}),
属性:DS.belongsTo('property',{async:true})
});

脚本(type =text / x-handlebars,data-template-name =summaryRow)
.summaries__summary({{action'summaryRowAction'}})
.summaries__summary - item {{formattedStart}}& mdash; {{endDate}}


解决方案

只有)属性被评估的时间, model 实际上是 null 。您需要在属性中指定 startDate 作为依赖关系,因此Ember知道重新评估数据何时更改。此外,您不需要在对象控制器中的模型。* ,属性将自动委托给内容 / 模型



所以:

  formattedStart:function(){
console.log(this.get('startDate');)
return this.get('startDate');
} .property('startDate'),


I have a pretty basic setup where I'm trying to format a date in my Controller. The problem is I can't access it in the formattedStart function below, whereas I CAN access it in the summaryRowAction handler. This is baffling me, because console.logging this in both places gives the same result. But for some reason inside of formattedStart, this.get('model.startDate') is undefined.

App.SummaryRowController = Ember.ObjectController.extend({
    formattedStart: function(){
        console.log(this.get('model.startDate');)
        return this.get('model.startDate');
    }.property(),

    actions: {
        summaryRowAction: function(){
            console.log(this.get('model.startDate'));
        }
    }
});

Here is my model and my template (in Jade) for reference:

App.PricingSummary = DS.Model.extend({
    startDate: DS.attr(),
    endDate: DS.attr(),
    days: DS.hasMany('day', {async: true}),
    property: DS.belongsTo('property', {async: true})
});

script(type="text/x-handlebars", data-template-name="summaryRow")
    .summaries__summary("{{action 'summaryRowAction'}}")
        .summaries__summary--item{{formattedStart}} — {{endDate}}

解决方案

It's because the first (and only) time that the property is evaluated, model is actually null. You need to specify startDate as a dependency in the property so Ember knows to re-evaluate when the data changes. Also, you don't need model.* in an object controller, the properties are automatically delegated to content/model

So:

formattedStart: function(){
    console.log(this.get('startDate');)
    return this.get('startDate');
}.property('startDate'),

这篇关于从Ember中的控制器访问模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆