Skip to content

Instantly share code, notes, and snippets.

@fearphage
Forked from thurloat/BackboneViewFactory.js
Created July 9, 2012 03:45
Show Gist options
  • Save fearphage/3074120 to your computer and use it in GitHub Desktop.
Save fearphage/3074120 to your computer and use it in GitHub Desktop.
Backbone ViewFactory
var ViewFactory = (function() {
function ViewFactory(pubSub) {
this.pubSub = pubSub;
this.registry = {};
this.registry['factory'] = this;
}
ViewFactory.prototype.register = function(key, value) {
return this.registry[key] = value;
};
ViewFactory.prototype.create = function(ViewClass, options) {
var klass, passedOptions;
options = options || {};
passedOptions = _.extend(options, this.registry, {
pubSub: this.pubSub
});
klass = ViewClass;
klass.prototype.pubSub = this.pubSub;
return new klass(passedOptions);
};
return ViewFactory;
})();
var myFactory = new ViewFactory(_.extend({}, Backbone.Events);
var viewInstance = myFactory.create(WhateverViewClass);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment