Coder Social home page Coder Social logo

backbone.viewmanager's Introduction

backbone.viewmanager

A simple little module for managing Backbone views, birthed in a JSFiddle. An even simpler version without region support is available here if you're interested in seeing how this evolved.

This point is to be a small, lightweight library to help people learn.

Usage

You're given a new Backbone.ViewManager which does all the cool stuff. And a Backbone.ViewManager.BaseView for you to extend from. See a demo of the below code in action.

var app = window.app = { views: {} };

// these views inherit the render() method from the base
// so you only need to write that function if it does something special
app.views.pageOne = Backbone.ViewManager.BaseView.extend({
    template: '#pageOne',

    initialize: function() {
        // set some data which will be passed to the template
        // you could add your model or collection here
        // 
        // NOTE: the base view's render() method is only good
        // for the simplest of views, write your own on a view
        // if you have more advanced needs
        this.templateData.person = {
            name: 'Dan Harper'
        };
    }
});

app.views.pageTwo = Backbone.ViewManager.BaseView.extend({
    template: '#pageTwo',

    unsetView: function() {
        // this method is run just before the view is removed,
        // you may want to turn off any bindings here..
        console.log('pageTwo custom unsetView');
    }
});

app.views.sidebarOne = Backbone.ViewManager.BaseView.extend({
    template: '#sidebarOne'
});

app.views.sidebarTwo = Backbone.ViewManager.BaseView.extend({
    template: '#sidebarTwo'
});



app.router = Backbone.Router.extend({
    routes: {
        '': 'pageOne',
        'foo': 'pageTwo'
    },

    initialize: function() {
        // register the regions
        Backbone.ViewManager.Core.addRegion('panel', '#panel');
        Backbone.ViewManager.Core.addRegion('sidebar', '#sidebar');
    },

    pageOne: function() {
        // render our views!
        Backbone.ViewManager.Core.swap('panel', new app.views.pageOne());
        Backbone.ViewManager.Core.swap('sidebar', new app.views.sidebarOne());
    },

    pageTwo: function() {
        Backbone.ViewManager.Core.swap('panel', new app.views.pageTwo());
        Backbone.ViewManager.Core.swap('sidebar', new app.views.sidebarTwo());
    }
});

new app.router();
Backbone.history.start();

backbone.viewmanager's People

Contributors

danharper avatar

Watchers

 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.