Ember.js 如何访问控制器计算属性内的模型属性
我是新来的。我有以下情况:Ember.js 如何访问控制器计算属性内的模型属性,ember.js,Ember.js,我是新来的。我有以下情况: App.Message = DS.Model.extend value: DS.attr('string') App.MessagesController = Ember.ArrayController.extend isAvailable: null isShowButton: (-> #I want to check a property of each obj in the template's each loop below
App.Message = DS.Model.extend
value: DS.attr('string')
App.MessagesController = Ember.ArrayController.extend
isAvailable: null
isShowButton: (->
#I want to check a property of each obj in the template's each loop below
console.log @get('value') #but << this outputs `undefined`
if @get('value') == 'test' and isAvailable
return true
else
return false
).property('isAvailable', 'model.value')
我错过了什么?任何帮助都将不胜感激。您需要迭代数组,值在集合中不存在,它存在于集合中的每个项目上,有一个很好的帮助程序,
getEach
返回数组中每个项目的值,它是这样使用的(请原谅我的咖啡脚本)
更新
如果试图为控制器中的每个项目创建计算属性,则需要使用itemController
,它指定用于阵列中每个项目的控制器
App.MessagesController = Ember.ArrayController.extend
itemController: 'foo'
isAvailable: null
App.FooController = Em.ObjectController.extend
needs: ['messages']
isShowButton: (->
return @get('value') === 'test' && @get('controllers.messages.isAvailable')
).property('controllers.messages.isAvailable', 'value')
请看下面我的答案,您需要迭代。好的。。我来试试:)嗯,按钮现在显示在每个对象中。但我现在可以访问这些值了,虽然它是“所有值”,而不是“把手每个循环”中的“每个对象”(将按钮移到每个循环的外部:)您只是想根据每个项目的值在每个项目中显示它,还是基于所有值的组项目?我不确定可使用的内容或位置,所以我写了它,就好像它存在于messages控制器上一样,它可能需要存在于item控制器上,但您可以根据需要更改它Yes。我希望按钮仅在每次值==测试时显示。是isAvailable是messagesController的布尔属性。好的,我将尝试使用此itemController:)
App.MessagesRoute = Ember.Route.extend
setupController: (controller, model)->
controller.set('model', @store.find('message'))
App.MessagesController = Ember.ArrayController.extend
isAvailable: null
isShowButton: (->
#I want to check a property of each obj in the template's each loop below
//console.log @get('value') #but << this outputs `undefined`
values = @getEach('value')
console.log values
outerValue = false
values.forEach(function(value){
if value == 'test' and isAvailable
outerValue = true
//else
// I'm not sure if they all have to be test
});
return outerValue
).property('isAvailable', 'model.@each.value')
CcChat.MessagesRoute = Ember.Route.extend
model: (params)->
return @store.find('message')
App.MessagesController = Ember.ArrayController.extend
itemController: 'foo'
isAvailable: null
App.FooController = Em.ObjectController.extend
needs: ['messages']
isShowButton: (->
return @get('value') === 'test' && @get('controllers.messages.isAvailable')
).property('controllers.messages.isAvailable', 'value')