-
-
Save thurloat/1760538 to your computer and use it in GitHub Desktop.
Backbone ViewFactory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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