GithubHelp home page GithubHelp logo

th3james / backbone.viewmanager Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 2.0 113 KB

Allows management and garbage collection of backbone views which are inserted into a common DOM element

CoffeeScript 100.00%

backbone.viewmanager's Introduction

Backbone.ViewManager

The Problem

By default, Backbone.View can quite easily leak memory, even if you're taking care to use the unbind and remove methods when disposing of views. This can quickly become an issue if your application frequently creates and removes views, as zombie views remain bound to model events, causing performance to degrade at an alarming rate. For a good overview, see Derick Bailey's post: http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

The Solution

Backbone.ViewManager extends Backbone.View with new methods which monitor bindings (the main cause of view memory leakage). You can then use the Backbone.ViewManager class to handle swapping between views on a common DOM element.

Installation

Include view_manager.js (or .coffee) after Backbone.js but before your backbone application.

Usage

Backbone.View extensions

Backbone.View is extended with some new methods which allow the monitoring and disconnetion of bindings

  • bindTo - Use this method in place of Backbone.Events.on(). It behaves the same as .on(), but keeps a reference of the binding. Takes 3 argments:
    • model - The object to bind to
    • event - The event to listen for
    • callback - Callback method on event Example:
  post.on('update', callback)
  # Becomes...
  this.bindTo(post, 'update', callback)
  • close - This method removes the backbone view, unbinding from all events. This method is called by Backbone.ViewManager on old views when swapping a new one in. You can add an optional onClose method to your models, if there is anything else you want to do when a view is removed.

Backbone.ViewManager

Once you've updated your application to use the above methods, you can use the Backbone.ViewManager object to manage views which are inserted into a common DOM element.

  # Create a new Backbone.ViewManager, passing the tag name (in an options object) you want the rendered view to be inserted into and returned inside:
  # If you do not provide a tagName, ViewManager defaults to creating a <div>
  viewManager = new Backbone.ViewManager(tagName: 'div')
  
  # Show a Backbone.View inside the viewManager element
  viewManager.showView(new MyBackboneView())

  # Show a different view inside the viewManager element, cleanly removing the prior view
  viewManager.showView(new AnotherView())

The showView calls .close() on the current view before inserting the new one, allowing you to swap views in and out without worrying about bindings leaking.

Notes

This project was developed to fill a personal need, but hopefully will prove useful to others. Contributions very welcome. The plugin is heavily inspired by Derick Bailey's post: http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/ and JohnnyO http://stackoverflow.com/questions/7567404/backbone-js-repopulate-or-recreate-the-view/7607853#7607853

backbone.viewmanager's People

Contributors

th3james avatar adammulligan avatar

Stargazers

Jim Rubenstein avatar Ger Hobbelt avatar Jonathan Barratt avatar  avatar

Watchers

 avatar James Cloos avatar  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.