2

I have created a Spine Controller and I want to trigger a custom event 'Create' from inside one of it's methods. Then I would like another Spine Controller to listen for that event. How is this possible using Spine.js's custom event methods?

Panos Spiliotis
  • 801
  • 1
  • 9
  • 18

2 Answers2

3

Custom events don't have to be global. You can can trigger/bind events per controller instance, but not per controller class. For instance

class PresenterController extends Spine.Controller
  giveSpeech: (message) ->
    @trigger("speech",message)

class App extends Spine.Controller
  addKeynote: ->
    @speaker = new PresenterController
    @speaker.bind("speech",@listen)
  listen: (message) =>
    alert("I heard " + message)

But you can't get something like PresenterController.bind("speech")

The reason is that Model uses @extend Event, which adds methods to the class, but Controller uses @include Event, which adds methods to the instance. Model achieves the appearance of having instance bindings because it defines its own implementations of trigger, bind, and unbind

deafgreatdane
  • 1,211
  • 11
  • 12
1

You can trigger events in Spine like this

Spine.trigger "Create"

In you're other Controller you can bind the event to a method like this

Spine.bind "Create", @create

You have to define a method with the name create in this Controller:

create: =>
  # Create action

Because the custom events are global I would suggest using the controllers name as a prefix. You could use ControllerName:Create for example:

Spine.trigger "ControllerName:Create"
Nina
  • 23
  • 4