1.0.2 • Published 7 years ago

backbone.decorators v1.0.2

Weekly downloads
5
License
-
Repository
github
Last release
7 years ago

Backbone decorators

This is an extension of the Backbone library to add support for decorators.

Some good documentation about the decorator pattern can be found here: https://addyosmani.com/resources/essentialjsdesignpatterns/book/#decoratorpatternjavascript

Any pull request for optimisations and new additions are more than welcome.

Installing backbone-decorators

npm install backbone-decorators

Usage

Decorators can be defined for backbone views, models and collections.

A decorator must be a function.

var Backbone = require('backbone');
require('backbone-decorators');

var decorator = function() {
    return {
        someMethod: function(num) {
            return num + 1;
        }
    };
};

var anotherDecorator = function() {
    return {
        someMethod: function(num) {
            return num + 2;
        }
    };
};

var View = Backbone.View.extend({
    decorators: [decorator, anotherDecorator],
    someMethod: function() {
        return 1;
    }
});

var view = new View();
// view.someMethod() = 4;

Decorators can also be passed as an option to the backbone entity constructor.

var view = new Backbone.View({
    decorators: [decorator, anotherDecorator],
    someMethod: function() {
        return 1;
    }
});
// view.someMethod() = 4;

A decorator property can be a function or an object.

var decorator = function() {
    return {
        someObj: {someProperty: 'same value'}
    }
};

var anotherDecorator = function() {
    return {
        someObj: {otherProperty: 'other value'}
    }
};

var view = new Backbone.View({
    decorators: [decorator, anotherDecorator]
});
// view.someObj = {someProperty: 'same value', otherProperty: 'other value'};