Skip to content

Instantly share code, notes, and snippets.

@artemave
Created April 25, 2012 21:46
Show Gist options
  • Save artemave/2493763 to your computer and use it in GitHub Desktop.
Save artemave/2493763 to your computer and use it in GitHub Desktop.
Coffee script factory pattern
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