Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 如何更改视图的控制器?_Ember.js - Fatal编程技术网

Ember.js 如何更改视图的控制器?

Ember.js 如何更改视图的控制器?,ember.js,Ember.js,我想听听应用程序路由/控制器广播的一些事件。我用eventsmixin扩展了它们 触发事件时,我希望更改视图的状态(简单的闪存消息),但无法访问路由/控制器。我尝试了context和controller属性,但是 该部分: this.get('controller').on('someEvent', handler) // or this.get('context').on('someEvent', handler) 表示“对象在上没有方法” 如何更改视图的控制器以及格式(字符串、对象) 回应

我想听听应用程序路由/控制器广播的一些事件。我用eventsmixin扩展了它们

触发事件时,我希望更改视图的状态(简单的闪存消息),但无法访问路由/控制器。我尝试了
context
controller
属性,但是 该部分:

this.get('controller').on('someEvent', handler)
// or
this.get('context').on('someEvent', handler)
表示“对象在上没有方法”

如何更改视图的控制器以及格式(字符串、对象)

回应评论

我想将逻辑与视图分离。让我来描述一下。单击按钮时,与之关联的操作会弹出到ApplicationRoute,因为它非常通用:

App.ApplicationRoute = Em.Route.extend Ember.Evented,
  events:
    someAction: ->
      @trigger 'someEvent', 'success'
我还有一条view-flash消息,我想在触发事件时更改其状态:

App.FlashView = Em.View.extend
  elementId: 'flash'
  classNames: 'alert'

  didInsertElement: ->
    $this = this.$ this

    @get('controller').on('someEvent', (status) ->
    if status is 'success'
      message = "Wow! Success!!"
    else
      message = "Oops! An error has occured!"

    $this
      .removeClass('alert-error alert-success')
      .addClass("alert-#{status}")
      .empty()
      .append(message)
      .show()
      .fadeIn()
      .delay(2000)
      .fadeOut()
  )

事实上,我不介意使用其他技术,因为我对新的可能更好的解决方案持开放态度。无论是通过Evented mixin的事件还是计算属性,我希望您详细说明这一点。

在这种情况下,更改控制器可能没有意义。相反,通过应用程序控制器的现有控制器使应用程序控制器可供您的视图使用。因此:

// From your controller:
  needs: ['application'];

// Now you can access application controller from your view:
this.get('controller.controllers.application').on('someEvent', handler)
如果不确定要更改哪个控制器,请从视图中尝试以下操作:

console.log(this.get('controller').toString())
FWIW我的经验和@Luke一样,很少从一个角度使用事件。考虑绑定到FLASH消息属性,而

更新(基于更新的问题) 好的,这里是使用控制器而不是事件版本的快速草图。抱歉,我没有测试过这段代码

首先,在模板中的某个地方使用
{{render}
辅助程序渲染flash

{{render flash}}
这将在FlashController的上下文中呈现FlashView

App.FlashController = Ember.Controller.extend({
  status: nil,
  messages: {
    'success': "Wow! Success!!",
    'error': "Oops! An error has occured!"
  },
  message: function() {
    return this.get('messages')[this.get('status')] || nil
  }.property('status')

})
现在定义FlashController

App.FlashController = Ember.Controller.extend({
  status: nil,
  messages: {
    'success': "Wow! Success!!",
    'error': "Oops! An error has occured!"
  },
  message: function() {
    return this.get('messages')[this.get('status')] || nil
  }.property('status')

})
对于视图,将警报类型classname绑定到控制器的状态,并添加一个观察者,该观察者将在消息更改时淡出/淡出。您还需要定义css类
隐藏
,以便在设置消息之前警报不可见

App.FlashView = Ember.View.extend({
  elementId: 'flash',
  classNames: 'alert hidden',
  classNameBindings: ['alertType'],
  alertType: function() {
    if (this.get('controller.status')) {
      return "alert-" + this.get('controller.status')
    }
  }.property('controller.status'),
  messageDidChange: function() {
    this.$().show().fadeIn().delay(2000).fadeOut()
  }.observes('controller.message')
})
对于
flash.hbs
模板,只需确保输出消息即可

{{message}}

你能展示更多的代码吗,特别是你试图访问控制器的部分吗?FWIW,~80%的时候我试图使用事件来影响我的视图,我发现我最好使用一个属性。我已经更新了问题。我也不确定我是否能像我那样订阅从路由触发的事件。谢谢!我没有注意到你编辑过的答案。但是如何从另一个控制器传递
status
消息呢?因此,从另一个控制器,指定
needs:['flash']
,然后调用
set('controllers.flash.status','success')
设置('controllers.flash.status','error')