Giter VIP home page Giter VIP logo

backbone-unbindingview's Introduction

Backbone.UnbindingView

Backbone.UnbindingView improves on the default Backbone.View by making it easy to clean up event listeners that aren't set up with declarative events.

Normal Backbone.View

class Foo.Views.Main extends Backbone.View
  initialize: ->
    _.bindAll @, "render"

    @collection.on "reset", @render

  render: ->
    bar = new Foo.Views.Bar
    @$el.append bar.render().el

    @

In this version, every time a new Foo.Views.Main is instantiated, @collection will gain an additional listener — i.e., on the fifth instance, render will run five times. The problem gets even worse if Foo.Views.Bar binds to a model or collection.

Backbone.UnbindingView

class Foo.Views.Main extends Backbone.UnbindingView
  initialize: ->
    _.bindAll @, "render"

    @bindings = []
    @bindTo @collection, "reset", @render

  render: ->
    @cleanUp()

    bar = new Foo.Views.Bar
    @$el.append bar.render().el

    @

Here, @bindTo takes a slightly less direct route than .on, pushing the event binding into the @bindings Array set up by initialize. @cleanUp clears the binding from @collection, so the reset event will only fire once. Hooray!

Even better, you can add automatic subview cleanup by adding just two lines. Put @child_views = [] in initialize, and push in the subviews you create:

render: ->
  @cleanUp()

  bar = new Foo.Views.Bar
  @$el.append bar.render().el
  @child_views.push bar

  @

Important note: Using Backbone.UnbindingView for a view means that any subviews put in @child_views must be extended from UnbindingView.

Contributing

Issues and patches welcome!

backbone-unbindingview's People

Contributors

stilist avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.