Git Product home page Git Product logo

backbone-viewmanager.js's Introduction

backbone-viewmanager.js

Manage creation, destruction, and persistence of backbone.js Views.

Why would I use a ViewManager?

If your application has Views that are only displayed for certain routes, one approach is to create a new instance of the View each time corresponding route is visited. But this approach is the source of some common problems. If you don't remember to manually close the view when the route changes, it will lead to a memory leak as mutliple copies of the View are created. Similarly, when creating a new view each time, the state of DOM elements will always be reset. State such as expanded/hidden elements and partially-entered data will be lost. Finally, you may know which views you want to show, but without knowing which views are already displayed, there is no simple way to know which views need to be removed/added. ViewManager is a simple solution to these problems.

Dependencies

  • jQuery
  • underscore.js
  • backbone.js

Usage

Begin by defining some backbone Views.

var FooView = Backbone.View.extend({ ... });
var BarView = Backbone.View.extend({ ... });

Create the ViewManager and specify the CSS selector for the element you want to contain the Views (the default is .content).

var vm = new ViewManager({ "selector": "#my-content" });

To get an instance of a given View, use getView(type, options, reuse). If this is the first time getView has been called for the View type (or if reuse) is true, a new View will be created by calling the constructor type with the given options. Otherwise, the previously created View will be returned (and options ignored).

var foo = vm.getView(FooView, {}).cid;
// < "view75"
var bar = vm.getView(BarView, {}).cid;
// < "view76"
var foo1 = vm.getView(FooView, {}, true).cid;
// < "view75"
var foo2 = vm.getView(FooView, {});
// < "view77"

To append a view to the DOM element associated with the ViewManager, without removing existing views, use addView().

// No views visible 
vm.addView(foo)
// foo visible
vm.addView(bar)
// foo and bar visible

To show a specific set of views, and close any others that are currently displayed, use showViews().

// No views visible
vm.showViews([foo, bar]);
// foo and bar visible
vm.showViews([foo2]);
// Only foo2 visible and remove() called for foo and bar

Finally, showView(v) is a shorthand for showViews([v]).

backbone-viewmanager.js's People

Contributors

elplatt avatar

Watchers

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