我正在建立餐厅管理应用程序。我有两个不同的控制器。处理弹出编辑器的人:
App.DishPopupController = Em.ObjectController.extend
dishId: -1
dish: null
isHidden: false
# always triggers when dishId is updated from DishController
fetchModel: (->
@set('dish', @store.find('dish', @get('dishId')))
).observes('dishId').on('init')
# always triggers when isHidden is updated from DishController
showPopup: (->
console.log 'Updated!'
).observes('isHidden').on('init')
actions:
hidePopup: ->
@set('isHidden', true)
和许多处理菜肴的控制器
App.DishController = Em.ObjectController.extend
needs: ['dishPopup']
isEditing: false
actions:
acceptChanges: ->
$(document.activeElement).blur()
@set('name', @get('name').trim())
@get('model').save()
removeDish: ->
if confirm 'Remove dish?'
dish = @get('model')
dish.deleteRecord()
dish.save()
showDishPopup: ->
@set('controllers.dishPopup.isHidden', false)
@set('controllers.dishPopup.dishId', @get('model.id'))
@set('isEditing', true)
acceptEdits: ->
@set('isEditing', false)
@get('model').save()
这是模板(用Hamlbars编写):
// Dish popup
%script(type="text/x-handlebars" data-template-name="dish-popup")
%div{:_action => 'hidePopup on="click"', :bind => {:class => ":dish-popup isHidden:hidden"}}
=hb 'dish.name'
%br
=hb 'dish.price'
%br
=hb 'dish.weight'
当我点击弹出窗口时,它隐藏了它应该和模板更新,但是当我在showDishPopup
中触发DishController
时,它不会更新模板而不会删除hidden
来自dish-popup
的课程,虽然它会触发观察者showPopup
。这是为什么?我可以更新其他控制器的属性,或者根本不应该这样做吗?