Skip to content

Commit

Permalink
don't drop defined queryparams from the url definition, properly appe…
Browse files Browse the repository at this point in the history
…nd passed params
  • Loading branch information
outdooricon committed Dec 2, 2014
1 parent c96e76b commit b746391
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/backbone.manager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
managerQueue = _.extend {}, Backbone.Events
onloadUrl = window.location.href

cachedParamMatcher = /[:*]([^(:)/]+)/g
cachedParamMatcher = /[:*]([^(:?)/]+)/g
cachedOptionalMatcher = /\(.*\)/g

currentManager = null
Expand Down Expand Up @@ -137,7 +137,8 @@
# Perform the opposite of routes hash and fill in url parameters with data
url = stateOptions._urlAsTemplate paramsObject
if queryParams
url += '?'+queryParams
if url.indexOf('?') > -1 then url+= '&' else url+= '?'
url += queryParams

if not historyHasUpdated and transitionOptions.navigate
@router.navigate url
Expand Down
37 changes: 33 additions & 4 deletions test/src/backbone.manager_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ describe 'Backbone.Manager.prototype', ->

expect(manager.states.test._urlAsTemplate(obj)).to.equal 'a/1/b/2'

it 'should build a url template including query params', ->
manager = new (Backbone.Manager.extend
states:
test:
transitionMethod: 'a'
url: 'a/:a_id/b/:b_id?a=b'
)(@router)

obj =
a_id: 1
b_id: 2
expect(manager.states.test._urlAsTemplate(obj)).to.equal 'a/1/b/2?a=b'

it 'should remove optional surrounds for url template', ->
manager = new (Backbone.Manager.extend
states:
Expand Down Expand Up @@ -343,7 +356,7 @@ describe 'Backbone.Manager.prototype', ->
@managerProto = Backbone.Manager.extend
states:
test:
url: 'a/:id_1/b/:id_2/c/:id_3/d/:id_4'
url: 'a/:id_1/b/:id_2/c/:id_3/d/:id_4?f=g'
transitionMethod: 'test'
test: ->

Expand All @@ -355,7 +368,7 @@ describe 'Backbone.Manager.prototype', ->

manager._handleTransitionCallback 'test', manager.states.test, [1,2,3,4, null]

expect(navigateStub).to.have.been.calledWith 'a/1/b/2/c/3/d/4'
expect(navigateStub).to.have.been.calledWith 'a/1/b/2/c/3/d/4?f=g'

it 'should call navigate', ->
manager = new @managerProto @router
Expand All @@ -366,13 +379,29 @@ describe 'Backbone.Manager.prototype', ->

expect(navigateStub).to.have.been.called

it 'should include query params with navigate call', ->
it 'should append query params with navigate call', ->
manager = new @managerProto @router

navigateStub = @sinon.stub @router, 'navigate'

manager._handleTransitionCallback 'test', manager.states.test, [1,2,3,4, 'a=b&c=d']

expect(navigateStub).to.have.been.calledWith 'a/1/b/2/c/3/d/4?f=g&a=b&c=d'

it 'should create query params with navigate call', ->
SimpleManagerProto = Backbone.Manager.extend
states:
test:
url: 'a/:id_1/b/:id_2/c/:id_3/d/:id_4'
transitionMethod: 'test'
test: ->

manager = new SimpleManagerProto @router

navigateStub = @sinon.stub @router, 'navigate'

manager._handleTransitionCallback 'test', manager.states.test, [1,2,3,4, 'a=b&c=d']

expect(navigateStub).to.have.been.calledWith 'a/1/b/2/c/3/d/4?a=b&c=d'

it 'should not fire navigate if historyHasUpdated', ->
Expand Down Expand Up @@ -426,7 +455,7 @@ describe 'Backbone.Manager.prototype', ->

manager._handleTransitionCallback 'test', manager.states.test, @paramsObj

expect(navigateStub).to.have.been.calledWith 'a/1/b/2/c/3/d/4'
expect(navigateStub).to.have.been.calledWith 'a/1/b/2/c/3/d/4?f=g'

it 'should call navigate', ->
manager = new @managerProto @router
Expand Down

0 comments on commit b746391

Please sign in to comment.