• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 11 years ago
  • Updated about 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Functional mixins for Backbone based on Advice by Angus Croll

Backbone.Advice

Use requireJS to import -- or whatever you want.

Based on the Advice functional mixin library by Angus Croll. Adds functional mixin abilities for Backbone objects.

There can be issues when extending classes that already have mixins and you may overrwrite needed functionality that has been mixed in. To get around this we use Backbone.AdviceFactory.

Features

Gives a convenient way to add functionality as needed and reuse components together.

Installation

Bower

To install Backbone.Advice via Bower:

bower install backbone.advice

From source

Download the latest zip file or clone the repository from GitHub:

git clone https://github.com/rhysbrettbowen/Backbone.Advice

Usage

Advice and Mixins are provided for use with requirejs, if you'd just like to add them in with script tags then just remove the define function call.

// add the mixin capability (may already be done for you)
Backbone.Advice.addMixin(Backbone.View)

// define a mixin
var namer = function(options) {
	// options an object that may be passed in

	// any functions under clobber will be replaced
	this.clobber({
		initialize: function() {
			this.spoke = options.times || 0;
		}
	});

	// these will only be set if there is no existing function
	this.setDefaults({
		name: 'frank',
		getName: function() {
			return this.name;
		},
		speak: function() {
			console.log('hello ' + this.getName());
		}
	});

	// first argument will be the original function - can also take an object of functions
	this.around('getName', function(orig) {
		return orig().split(' ')[0];
	});

	// can even extend objects - useful for adding events
	this.addToObj({
		events: {
			'greeted' :'speak'
		}
	});

	// first argument will be the original function - can also take an object of functions
	this.before('speak', function() {
		this.spoke++;
	});

	// first argument will be the original function - can also take an object of functions
	this.after('speak', function() {
		console.log('for the ' + this.spoke + 'th time');
	});

}


var Speaker = Backbone.View.extend({
	name: 'Bob White' // the set defaults won't override this
}).mixin([
	namer
], { // options passed in
	times: 3
});

var bob = new Speaker();
bob.speak();  // Hello Bob
              // for the 4th time

You can even call addToObj, clobber, setDefaults, after, before and around straight on the constructor rather than creating the mixin function:

var ShoutName = Speaker.extend().around('getName', function(orig) {
	return orig().toUpperCase();
});

We can also setup a sort of pseudo inheritance between mixins, say we wanted the previous example to work on it's own we could do this:

var shouter = function(options) {

	// pull in any other mixins this one depends on
	this.mixin([
		namer
	], options);

	// now we can decorate
	this.around('getName', function(orig) {
		return orig().toUpperCase();
	});
}

var ShoutName = Backbone.View.extend().mixin([
	shouter
]);

Mixins will keep a record of what has been put on, so a mixin will only be applied once (this may cause issues if you'd like to re-apply a mixin with a different set of options).

notice that we're extending before the mixin to get the right prototype chain before mixing in.

Mixins can also be objects like this:

var myMixin = {
	clobber: {
		clobbered: true;
	},
	addToObj: {
		events: {
			'click': 'onClick'
		}
	},
	after: {
		'render': function() {
			console.log('rendering done');
		}
	}
};

Be careful though, only mixins that were defined as functions are able to see if they have been applied before. To fix this you can return the object from a function:

var myMixinFn = function() {
	return myMixin;
}

or even use the return method to pass in the options object:

var myMixinFn = function(options) {
	return {
		setDefaults: {
			number: options.number
		}
	};
}

see Backbone.Advice.Mixins (mixin.js) for some useful mixins you can use today!

Tests

Install dependencies

bundle install

and open the file test.html in your browser.

TBD: re-enable testing with testacular/karma.

#Changelog

##Advice

###v1.0.0

  • initial versioning

##Mixin

###v1.0.0

  • initial versioning

More Repositories

1

PlastronJS

MVC framework for Closure Library and Closure Compiler
JavaScript
105
star
2

debounce

an improved debounce implementation
JavaScript
27
star
3

G-closure

use closure like jquery
JavaScript
27
star
4

Backbone.ComponentView

view manager based on goog.ui.component for Backbone
JavaScript
21
star
5

Aplus

an implementation of promises/A+
JavaScript
18
star
6

Backbone.ModelMorph

create a model of computed properties from another backbone model
JavaScript
18
star
7

Backbone.CollectionFilter

create filtered collection from backbone collections
JavaScript
18
star
8

Agency-Adserver

agency adserver using node.js, redis and mongodb
JavaScript
12
star
9

goog.mvc

backbone.js like mvc for google closure
JavaScript
10
star
10

Backbone.ModelRegistry

Use only single references to Backbone models across registered collections
JavaScript
10
star
11

Backbone.MiddlewareSync

allows middleware to be added to Backbone.Sync
JavaScript
9
star
12

Backbone.ModelAttrs

Provide a simpler api to get and set values
JavaScript
7
star
13

Backbone.AdviceFactory

enhanced inheritance structure to work with Backbone.Advice
JavaScript
7
star
14

closure-widgets

UI widgets for use with closure library (and G-Closure)
JavaScript
6
star
15

Underscore.Extras

Extra functionality for underscore.js
JavaScript
6
star
16

Loader

An IoC DI for Closure Tools
JavaScript
6
star
17

MouseMover

run mouse move on requestAnimationFrame
JavaScript
5
star
18

Backbone.SelectableCollection

a selectable layer on top of a collection
JavaScript
5
star
19

promise_impl

Promises/A+ v1.1 implementation
JavaScript
5
star
20

widgetboard

Logic for creating a board of widgets
HTML
4
star
21

HandlebarRivets

A node program to convert handlebar templates to RivetsJS
JavaScript
3
star
22

HTML5-chat

uses HTML5 for a rich chat program
JavaScript
3
star
23

JSDB

a simple DB in javascript
JavaScript
3
star
24

functional-closure

allows point-free style with the closure library
JavaScript
3
star
25

gss.sublime-package

sublime package for Closure Stylesheets
2
star
26

MaggieJS

Reactive data model streams
JavaScript
2
star
27

JSONycle

JSON stringify and parse for objects containing cycles
JavaScript
2
star
28

Fibonacci

JavaScript
1
star
29

JSpuzzles

JavaScript
1
star
30

tags

1
star
31

how_google_does_it

Philly ETE talk on Closure Tools
JavaScript
1
star
32

CSS-to-inline

JavaScript
1
star
33

foosball-scores

work out foosball ratings
JavaScript
1
star
34

weatherdemo

demo of plastronjs and the g
JavaScript
1
star
35

closure_from_jquery

html5 dev conf talk slides
JavaScript
1
star