Skip to content

Instantly share code, notes, and snippets.

@naganowl
Last active August 29, 2015 14:01
Show Gist options
  • Save naganowl/6fef926020b44e0b0b03 to your computer and use it in GitHub Desktop.
Save naganowl/6fef926020b44e0b0b03 to your computer and use it in GitHub Desktop.
Solution to Chaplin route params with redirectTo.
  • It's still pretty confusing if you ask me and I haven't tested it with routes with params, but as long as it's passed in effectively in the two places, it should be fine.
  • See the previous revision for some extended notes on the method flow
    • tl;dr utils.redirectTo() -> Router.route() -> Route.handler()
    • Route.handler() will only call Route.reverse() if it receives an object (needed for URL query params) which happens if you leave out the url property in the object (first argument) passed into redirectTo().
define (require, exports) ->
exports = (match) ->
match 'search', 'search#index', name: 'search'
match 'search/:query', 'search#query', name: 'query'
define (require, exports) ->
Chaplin = require 'chaplin'
exports = class SearchView extends Chaplin.View
initialize: ->
super
@delegate 'submit', 'form', (e) ->
e.preventDefault()
# `name` can also be a `path` object as specified in
# http://docs.chaplinjs.org/chaplin.router.html#route
# so doing `name = {name: 'query', query: 'foo'}` should work.
name = 'search'
term = @$('#search').val()
mode = 'email' if @$('#filter').is ':checked'
if term
Chaplin.utils.redirectTo(
{params: name, name}
null
query: {mode, term}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment