从Ember中的控制器访问模型 [英] Accessing a model from a controller in Ember
问题描述
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屋!