Skip to content

Instantly share code, notes, and snippets.

@renemonroy
Forked from julik/telegraph.coffee
Last active August 29, 2015 14:22
Show Gist options
  • Save renemonroy/4ebcee2e9baf98d9d4b3 to your computer and use it in GitHub Desktop.
Save renemonroy/4ebcee2e9baf98d9d4b3 to your computer and use it in GitHub Desktop.

Revisions

  1. @julik julik revised this gist Jan 28, 2014. 1 changed file with 16 additions and 11 deletions.
    27 changes: 16 additions & 11 deletions telegraph.coffee
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,34 @@
    # A module that allows us to send Backbone events up the tree of components
    ReactTelegraph =
    componentDidMount: ->
    componentWillMount: ->
    # Make BB events available to the component
    _.extend @, Backbone.Events

    componentDidUnmount: ->

    componentWillUnmount: ->
    # Remove all Backbone event listeners
    @off null, null, null

    triggerWithBubbling: (ar...)->
    return unless @trigger
    # Shortcut for triggerWithBubbling
    upstream: (ar...)->
    @triggerWithBubbling(ar...)

    # Trigger on myself using partial application
    triggerWithBubbling: (ar...)->
    # Trigger the event on myself using partial application
    @trigger.bind(@, ar...)()

    # And telegraph the event up the component tree
    parent = @props.__owner__
    return unless parent

    # Only do this if the owner component responds to "triggerWithBubbling"
    # - the extension indicates that the component participates in the
    # Telegraph
    if parent.triggerWithBubbling
    # Telegraph system and has the mixin
    if parent and parent.triggerWithBubbling
    parent.triggerWithBubbling(ar...)

    # Return true
    true
    # Return self
    @

    window.ReactTelegraph = ReactTelegraph

    MyButton = React.createClass
    mixins: [ReactTelegraph]
  2. @julik julik renamed this gist Jan 18, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @julik julik revised this gist Jan 18, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions exts.coffee
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,7 @@
    ReactTelegraph =
    componentDidMount: ->
    _.extend @, Backbone.Events
    _.extend @, ReactTelegraph


    componentDidUnmount: ->
    @off null, null, null

  4. @julik julik revised this gist Jan 18, 2014. 1 changed file with 10 additions and 11 deletions.
    21 changes: 10 additions & 11 deletions exts.coffee
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,12 @@
    # A module that allows us to send Backbone events up the tree of components
    ReactTelegraph =
    componentDidMount: ->
    _.extend @, Backbone.Events
    _.extend @, ReactTelegraph

    componentDidUnmount: ->
    @off null, null, null

    triggerWithBubbling: (ar...)->
    return unless @trigger

    @@ -20,32 +27,24 @@ ReactTelegraph =
    true

    MyButton = React.createClass
    mixins: [ReactTelegraph]
    upvoteClicked: ->
    @triggerWithBubbling 'upvote', @props.commentId

    render: ->
    (button {onClick: @upvoteClicked}, "Upvote comment #{@props.commentId}!")

    componentDidMount: ->
    _.extend @, Backbone.Events
    _.extend @, ReactTelegraph

    MyComponent = React.createClass
    mixins: [ReactTelegraph]

    render: ->
    (div {}, (MyButton {commentId: 123}), (MyButton {commentId: 234}))

    componentDidMount: ->
    _.extend @, Backbone.Events
    _.extend @, ReactTelegraph
    # Listen to the "upvote"
    # event via BB events, it's going to be
    # sent from one of the children
    @on 'upvote', (cid) ->
    # Do whatever we have to do
    console.debug "Comment #{cid} upvoted"

    componentDidUnmount: ->
    # Remove BB events
    @off(null, null, null)


  5. @julik julik revised this gist Jan 18, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion exts.coffee
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,11 @@ MyButton = React.createClass

    render: ->
    (button {onClick: @upvoteClicked}, "Upvote comment #{@props.commentId}!")


    componentDidMount: ->
    _.extend @, Backbone.Events
    _.extend @, ReactTelegraph

    MyComponent = React.createClass
    render: ->
    (div {}, (MyButton {commentId: 123}), (MyButton {commentId: 234}))
  6. @julik julik revised this gist Jan 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion exts.coffee
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ MyButton = React.createClass

    MyComponent = React.createClass
    render: ->
    (div {})
    (div {}, (MyButton {commentId: 123}), (MyButton {commentId: 234}))

    componentDidMount: ->
    _.extend @, Backbone.Events
  7. @julik julik created this gist Jan 18, 2014.
    47 changes: 47 additions & 0 deletions exts.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    # A module that allows us to send Backbone events up the tree of components
    ReactTelegraph =
    triggerWithBubbling: (ar...)->
    return unless @trigger

    # Trigger on myself using partial application
    @trigger.bind(@, ar...)()

    # And telegraph the event up the component tree
    parent = @props.__owner__
    return unless parent

    # Only do this if the owner component responds to "triggerWithBubbling"
    # - the extension indicates that the component participates in the
    # Telegraph
    if parent.triggerWithBubbling
    parent.triggerWithBubbling(ar...)

    # Return true
    true

    MyButton = React.createClass
    upvoteClicked: ->
    @triggerWithBubbling 'upvote', @props.commentId

    render: ->
    (button {onClick: @upvoteClicked}, "Upvote comment #{@props.commentId}!")

    MyComponent = React.createClass
    render: ->
    (div {})

    componentDidMount: ->
    _.extend @, Backbone.Events
    _.extend @, ReactTelegraph
    # Listen to the "upvote"
    # event via BB events, it's going to be
    # sent from one of the children
    @on 'upvote', (cid) ->
    # Do whatever we have to do
    console.debug "Comment #{cid} upvoted"

    componentDidUnmount: ->
    # Remove BB events
    @off(null, null, null)