JSFiddle - React, Tailwind, and code Playground
by craigm
HTML
<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone.js"></script>
JavaScript
Backbone.Factory = function (options) {
var defaults = {
all: {},
collection: {},
model: {},
router: {},
view: {}
},
config = $.extend(true, {}, options, defaults);
var collection = Backbone.Collection.extend(config.all).extend(config.collection),
model = Backbone.Model.extend(config.all).extend(config.model),
router = Backbone.Router.extend(config.all).extend(config.router),
view = Backbone.View.extend(config.all).extend(config.view);
return {
Collection: collection,
Model: model,
Router: router,
View: view
};
};
//test code
var factory = Backbone.Factory({
all: {
foo: 'bar'
},
model: {
foo: 'baz',
free: 'beer'
},
view: {
color: 'black'
},
router: {
free: 'willy'
}
});
var init = function() {
console.log('foo', this.foo);
console.log('free', this.free);
console.log('color', this.color);
};
var AModel = factory.Model.extend({
defaults: {
name: 'Unknown',
age: null
},
initialize: init
});
var AView = factory.View.extend({
initialize: init
});
var ACollection = factory.Collection.extend({
initialize: init
});
var ARouter = factory.Router.extend({
initialize: init
});
console.log('Collection');
var collection = new ACollection();
console.log('');
console.log('Model');
var model = new AModel({name: 'Joe'});
console.log('');
console.log('Router');
var router = new ARouter();
console.log('');
console.log('View');
var view = new AView();