2015-02-05 44 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

相關問題