Created
April 25, 2012 21:46
-
-
Save artemave/2493763 to your computer and use it in GitHub Desktop.
Coffee script factory pattern
This file contains 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
class MyNamespace.MyView extends Backbone.View | |
@Factory: -> | |
create: -> | |
new MyView | |
# then pass it in router initialization | |
class MyRouter extends Backbone.Router | |
initialize: (opts = {}) -> | |
myview_factory = opts.myview_factory || new MyNamespace.MyView.Factory | |
# why not just create new view in router? Answer: router becomes difficult to test as it is impossible to sensibly mock the view | |
# but this way it is dead simple: | |
describe 'MyRouter', -> | |
it 'respondes to #foo with contents of MyView', -> | |
myview = { render: @spy() } | |
myview_factory = { create: @stub().returns(myview) } | |
new MyRouter(myview_factory: myview_factory) | |
expect(myview.render).toHaveBeenCalled() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment