GithubHelp home page GithubHelp logo

xenovyzarz / backbone.unclassified.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from willurd/backbone.unclassified.js

0.0 0.0 0.0 501 KB

Declarative child elements for Backbone Views

License: MIT License

backbone.unclassified.js's Introduction

Backbone Unclassified

Declarative child elements for Backbone Views.

Why?

Backbone doesn't come with a way of managing a View's child elements. If you want to use all of Backbone's great features, like its event declaration syntax, you either have to write selectors multiple times or save the selectors elsewhere for later use. Or your can forgo using Backbone's niceties altogether and do everything the hard way (manually). But where's the fun in that?

Selector engines

Backbone Unclassified supports:

Installation

All you need for Backbone Unclassified is backbone.unclassified.js. You can install it manually or use one of the following package managers.

Bower

$ bower install backbone.unclassified

JamJS

$ jam install backbone.unclassified

Include

Backbone Unclassified depends on Backbone and whatever selector library you're using for it, which you'll already have installed.

<!-- Backbone stuff -->
<script src="/path/to/your-favorite-selector-library.js"></script>
<script src="/path/to/backbone.js"></script>

<!-- Backbone Unclassified -->
<script src="/path/to/backbone.unclassified.js"></script>

How do I use it?

Once you've included backbone.unclassified.js on your page, you can use Backbone Unclassified by specifying a collection of child elements inside a view:

var YourView = Backbone.View.extend({
    el: "#your-element-id",

    // The selectors here will be used to create references to your child
    // elements when your view is instantiated and when your view's main
    // element changes. Notice, also, that this set of child elements can
    // be hierarchical. You can access this.ui.button.add in code, and
    // button.add in your `events` specification.
    ui: {
        title: "header > h2",
        list: "ul, ol",
        people: "li.person",
        odd: "li:nth-child(odd)",
        even: "li:nth-child(even)",
        admins: "li.person[data-role=admin]",
        button: {
            add: ".btn.add",
            remove: ".btn.delete",
            secret: "button[data-contains-secrets]"
        }
    },

    // You can use your child element names in place of selectors here.
    events: {
        "click button.add": "add",
        "click button.remove": "remove",
        "click button.secret": "abort",
        "click people": "select",
        "mouseenter list": "over",
        "mouseleave list": "out"
    },

    initialize: function() {
        // You can use your child elements programmatically as early as in
        // your initialize method.
        this.ui.title.text("People (" + this.ui.people.length + ")");
        this.ui.button.secret.hide();
    },

    add: function () { ... },
    remove: function () { ... },
    select: function () { ... },
    abort: function() { ... },
    over: function () { ... },
    out: function () { ... }
});

Refer to example/example.js for a complete working example.

Caveats

Dynamically created elements

Elements that are created after your view is initialzed won't be included in the query results (your this.ui.whatever objects), because jQuery objects don't update automatically. Because of this, Backbone Unclassified provides a simple way of refreshing queries.

To refresh all queries (usually at the end of your render method):

var YourView = Backbone.View.extend({
    // ...

    render: function() {
        // ...
        this.refreshUi();
    }

    // ...
});

To refresh a sub-object of queries:

this.refreshUi(this.ui.button); // An object containing multiple queries.

To refresh a single query:

this.ui.admins.refresh();

Refreshes happen in-place, meaning they modify the query object itself (as opposed to returning a new object). refresh() also returns the object, so you can chain methods as normal:

this.ui.title.refresh().text("Hello, Backbone Unclassified");

Refer to ListView.render in examples/collection/example.js for an example of how this is used in practice.

License

MIT licensed. See LICENSE.txt for the full text.

backbone.unclassified.js's People

Contributors

willurd 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.