2015-02-05 46 views
1

我有一个由其他人构建的ChaplinJS项目,并且我没有完全了解框架的某些细节。这里有一点我很难找到:在侦听阻止例程中使用“mediator”作为接收器

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

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

但是,在卓别林世界,我认为“中介”实际上是指卓别林核心Chaplin.mediator对象。它是否正确?

虽然我在它,那么第一行change model莫名其妙听Chaplin.model?其中Chaplin.model

回答

1

是的,这是正确的。

能够听到下面的方法:通过控制渠道

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班,publish/ or subscribe to direct communication

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

或您的视野之外:

require('chaplin'); 

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

你可以鳍d这里的事件代理的源代码:https://github.com/chaplinjs/chaplin/blob/master/src/chaplin/views/view.coffee#L299-308

+0

fan-friggen-tastic回答,我喜欢你包括一个链接到源代码来回答这个问题。我很懒,但你不是,你会得分! – the0ther 2015-03-26 15:21:55

相关问题