1 Access url params in a view
Scott Erickson edited this page 2015-12-18 14:25:36 -08:00

Problem

You want to access parts of a URL for your view, for example to view a specific document in the db.

Solution

For path parameters, use Backbone Router routes in the url you add to Router.coffee, and access the values in your view's initialize function. For GET parameters, use core/utils.coffee's getQueryVariable function.

Example

In Router.coffee

routes:
  '/doodads/:doodadID': go('DoodadView')
  ...

DoodadView.coffee

utils = require 'core/utils'

class DoodadView extends RootView
  initialize: (options, doodadID) ->
    @doodad = new Doodad({_id: doodadID})
    @showTab = utils.getQueryVariable('show-tab')

So for example when navigating to the url /doodad/sprocket?show-tab=change-history, doodadID will be "sprocket" and @showTab will be "change-history".

Resources