Git Product home page Git Product logo

minionette's Introduction

Minionette Build Status Code Climate Coverage Status

A mini Marionette for Backbone.js

Minionette provides three highly optimized View classes for you to use, Minionette.View, Minionette.ModelView, and Minionette.CollectionView. Each class is designed to ease development, from using several performance boosting techniques during rendering to placing subviews directly in templates and allowing the subviews to be easily removed.

Why?

Because Backbone doesn't get much better than this:

var NavItem = Minionette.ModelView.extend({
    tagName: 'li',
    template: _.template('<a href="<%= href %>"><%= text %></a>')
});
var Nav = Minionette.CollectionView.extend({
    ModelView: NavItem,
    tagName: 'ul',
    template: _.template('<li>First</li><li class="last">last</li>'),
    appendModelView: function(view) {
        this.$('.last').before(view.$el);
    }
});

var Main = Minionette.View.extend({
    template: _.template('<p>Some content from another view</p>')
});

var navCollection = new Backbone.Collection([
    { text: 'home', href: '/' },
    { text: 'google', href: 'http://google.com/' }
]);

var App = Minionette.View.extend({
    el: $('body'),
    template: _.template(
        '<nav><%= view("nav") %></nav>' +
        '<div id="content"></div>'
    ),
    regions: {
        nav: new Nav({collection: navCollection}),
        content: '#content'
    }
});

var app = (new App()).render();
app.nav.render();

app.content.attach(new Main()).render();

Minionette.View is the base View class, providing an easy way to listen for events on an associated model or collection, an actually useful generic rendering function, easy subviews (AKA Regions).

Minionette.Regions help manage subviews of a Minionette.View, allowing you to specify directly in a template where a subview should be attached. A view can have any number of regions, each managing their own part of the overall view.

Minionette.ModelView is nothing more than Minionette.View with two minor tweaks to easily support rendering models.

Minionette.CollectionView is an optimized Minionette.View for your Backbone collections. It quickly handles rendering using a DocumentFragment, ensuring at most three content reflows even with hundreds of models to render. The most important feature of CollectionView is the #ModelView property, from which all models will have a view instantiated from.

Other Templating Languages

Handlebars.js

Full support for Handlebars.js templating is trivial. Just use the following:

Handlebars.registerHelper('view', function(name) {
    return new Handlebars.SafeString(this.view(name));
});

This will allow for template subview insertion using the special {{view 'regionName'}} syntax.

Mustache.js

Full support for Mustache.js templating takes just a bit more effort. You must override the internal-use _serialize() method in your view with the following:

var View = Minionette.extend({
    //...
    _serialize: function() {
        var _viewHelper = this._viewHelper;
        return _.extend({view: function() { return _viewHelper; }}, this.serialize());
    }
    //...
});

This will allow for template subview insertion using the special {{#view}}regionName{{/view}} syntax. For ease of use, have all of your new View classes extend from this, and they will all be compatible with Mustache.

minionette's People

Contributors

jridgewell avatar jamesplease avatar

Stargazers

Phillip Ressler avatar

Watchers

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