如何使用coffeescript在Framer Studio(Framer.js)中创建和触发自定义事件?

时间:2017-01-19 08:03:08

标签: javascript coffeescript eventtrigger custom-events framerjs

var event = new Event('build');

// Listen for the event.
elem.addEventListener('build', function (e) { ... }, false);

// Dispatch the event.
elem.dispatchEvent(event);

enter link description here 本文演示如何创建和分派DOM事件。此类事件通常称为合成事件,而不是浏览器本身触发的事件。

2 个答案:

答案 0 :(得分:3)

我不确定你要做什么,但可能你想在Framer Layer对象上进行自定义事件,对吗?

Layer对象有一些很好的速记函数:on()emit()。你像这样使用它们:

layer = new Layer

customEventName = "something"

layer.on customEventName, (argument, layer) ->
    print "Custom event", argument, layer

layer.emit customEventName, 1

layer.emit customEventName, 2

这打印出以下内容:

» "Custom event", 1, <Layer layer id:1 (0, 0) 200x200>
» "Custom event", 2, <Layer layer id:1 (0, 0) 200x200>

答案 1 :(得分:1)

Got this link from Framer group at FB.

这是一个适合我需要的简单cs示例

# npm install events
EventsEmitter = require('events')

class customEvent extends EventsEmitter
    constructor: ->
        @on "startScan", -> startAppsScan()
        @on "cancelScan", -> cancelScan()

startScanButton = new Layer
    x: Align.center
    y: Align.center(-150)
    backgroundColor: "yellow"

cancelScanButton = new Layer
    x: Align.center
    y: Align.center(150)
    backgroundColor: "red"

startScanButton.onClick ->
    evt = new customEvent
    evt.emit "startScan"

cancelScanButton.onClick ->
    evt = new customEvent
    evt.emit "cancelScan"

startAppsScan = ->
    print "starting scan...."

cancelScan = ->
    print "scan is cancelled !"