Skip to content
Browse files

turn TilsRouter into a SwappingRouter

  • Loading branch information...
1 parent e15cb8a commit 29831ce2dfe04207707a68ae55f9d5c66a26b7c0 @artemave committed May 15, 2012
View
30 app/assets/javascripts/backbone/routers/tils_router.js.coffee
@@ -1,23 +1,14 @@
-class TilApp.Routers.TilsRouter extends Backbone.Router
+class TilApp.Routers.TilsRouter extends Support.SwappingRouter
@Factory: ->
- create: () ->
- new TilsRouter()
+ create: ->
+ new TilsRouter
initialize: (opts = {}) ->
navigate = opts.navigate || @navigate
-
- @createNewTilForm = ->
- if @show_til_view
- @show_til_view.leave()
-
- @new_til_view = new TilApp.Views.TilForm()
- $(TilApp.bodySelector).append(@new_til_view.render().el)
- $(@new_til_view).trigger('load_pagedown_editor')
+ @til_form_view_factory = opts.til_form_view_factory || new TilApp.Views.TilForm.Factory
+ @el = opts.el || '#note_details'
@redirectToShowLastModifiedTil = ->
- if @new_til_view
- @new_til_view.leave()
-
last_modified_til = TilApp.tilsCollection.last_modified_til()
if last_modified_til
@@ -32,13 +23,12 @@ class TilApp.Routers.TilsRouter extends Backbone.Router
@redirectToShowLastModifiedTil()
new: ->
- @createNewTilForm()
+ new_til_view = @til_form_view_factory.create()
+ @swap(new_til_view)
+ $(new_til_view).trigger('load_pagedown_editor')
show: (id) ->
- if @show_til_view
- @show_til_view.leave()
-
- @show_til_view = new TilApp.Views.ShowTil(til: TilApp.tilsCollection.get(id))
- $(TilApp.bodySelector).append(@show_til_view.render().el)
+ show_til_view = new TilApp.Views.ShowTil(til: TilApp.tilsCollection.get(id))
+ @swap(show_til_view)
View
1 app/assets/javascripts/backbone/til.js.coffee
@@ -23,6 +23,7 @@ window.TilApp =
tils_index_factory = opts.tils_index_factory || new TilApp.Views.TilsIndex.Factory
@tils_index_view = tils_index_factory.create(collection: @tilsCollection)
$(@bodySelector).append(@tils_index_view.render().el)
+ $(@bodySelector).append($('<div id="note_details">'))
history = opts.history || Backbone.history
if not history.started
View
10 spec/javascripts/init_spec.js.coffee
@@ -35,3 +35,13 @@ describe "Til", ->
TilApp.init(tils_index_factory: tils_index_factory)
expect($(TilApp.bodySelector)).toHaveText(/text/)
+
+ it 'creates til details container element', ->
+ setFixtures sandbox()
+ TilApp.bodySelector = '#sandbox'
+
+ new TilApp.Routers.TilsRouter
+
+ TilApp.init()
+ expect($(TilApp.bodySelector)).toContain('#note_details')
+

0 comments on commit 29831ce

Please sign in to comment.
Something went wrong with that request. Please try again.