Sha256: 4dc2329af78d74e69c2787b5d96aa388ad8a28c4c148757d29d59363952ddfe2

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

###
 Backbone BindTo 1.0.0

 Author: Radoslav Stankov
 Project site: https://github.com/RStankov/backbone-bind-to
 Licensed under the MIT License.
###

root = @
BackboneView = root.Backbone.View

class BindToView extends BackboneView
  constructor: ->
    super

    @bindTo @model, eventName, methodName for eventName, methodName of @bindToModel if @model
    @bindTo @collection, eventName, methodName for eventName, methodName of @bindToCollection if @collection

  bindTo: (object, eventName, methodName) ->
    unless object in [@model, @collection]
      @_binded = []
      @_binded.push object unless _.include @_binded, object

    throw new Error "Method #{methodName} does not exists" unless @[methodName]
    throw new Error "#{methodName} is not a function" unless typeof @[methodName] is 'function'

    object.on eventName, @[methodName], @

  remove: ->
    @model.off null, null, @ if @model
    @collection.off null, null, @ if @collection

    _.invoke @_binded, 'off', null, null, @
    delete @_binded

    super

Backbone.BindTo =
  VERSION: '1.0.0'

  noConflict: ->
    root.Backbone.View = BackboneView
    BindToView

  View: BindToView

root.Backbone.View = Backbone.BindTo.View

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails-backbone-generator-0.0.3 lib/generators/backbone/setup_generator/templates/vendor/assets/javascripts/backbone/backbone-bind-to-1.0.0.coffee
rails-backbone-generator-0.0.1 lib/generators/backbone/templates/vendor/javascripts/backbone/backbone-bind-to-1.0.0.coffee