使用“;调解员”;作为侦听块例程中的汇点

Using "mediator" as a sink in a listen-block routine

本文关键字:例程 汇点 调解员 使用      更新时间:2023-09-26

我有一个由其他人构建的ChaplinJS项目,我没有完全了解该框架的某些细节。我发现有一点很难理解:

listen: {
  'change model': 'render',
  'home:actionvideo mediator': 'show'
},

这段代码位于其中一个视图JS文件中。我熟悉这种类型的事件处理,我的理解是第一位("home:actionvideo")是事件的名称,第二部分("mediator")是元素选择器,冒号后面的位是响应事件运行的函数的名称。

但在卓别林的世界里,我认为"中介"实际上是指卓别林核心的Chaplin.mediator对象。这是正确的吗?

当我在做的时候,第一行change model不知怎么听了Chaplin.model吗?哪个Chaplin.model

是的,这是正确的

chaplinjs能够监听以下方法:

var MyView = Chaplin.View.extend({
  events: {
    // Listen to $ DOM events
    'click button': 'methodName',
    'change select#myid': 'methodName',
    ...
  },
  listen: {
    // Listen to Chaplin events
    'onAddedToDOM': 'methodName',
    ...
    // Listen to model events
    'change:foo model': 'methodName',
    // Listen to collection events
    'reset collection': 'methodName',
    // Custom mediator events (or Chaplin events, like router:route etc.)
    'pubSubEvent mediator': 'methodName',
    // The value can also be a function.
    'eventName': function() {alert('Hello!')}
  },

使用mediator类,通过受控信道发布/或订阅直接通信:

this.publishEvent('pubSubEvent', ['Joe', 'Schmoe']);

或在您的视野之外:

require('chaplin');
Chaplin.mediator.publishEvent('pubSubEvent', ['Joe', 'Schmoe']);

您可以在此处找到事件委派的源代码:https://github.com/chaplinjs/chaplin/blob/master/src/chaplin/views/view.coffee#L299-308