I use Backbone.js and Chaplin.js on CoffeeScript. I have a challenge. How to get url params in a routed view?
routes.coffee:
define ->
'use strict'
(match) ->
match "account/albums/:page", "accounts#albums"
define [
'chaplin'
'collections/albums'
'views/form_view'
'views/inline/album'
'text!template/account/_form_image.html'
'text!template/account/list_albums.html'
], (chaplin,
Albums,
FormView,
AlbumView,
formTemplate,
template
) ->
'use strict'
class AccountAlbums extends chaplin.CollectionView
collection: new Albums
itemView: AlbumView
template: template
containerMethod: 'html'
listSelector: '[data-placeholder=albums-tile]'
# TODO: understand how to get router arguments in the view
initialize: (options) ->
super
@on 'addedToDOM', => @collection.fetch()
# Need to something like this.
# But I do not know how to get it.
#@collection.fetch
# data:
# page: page
This is an old question, but for completeness here's an answer:
Your route is sending the data to the albums
method inside a controller (accounts
) which isn't shown in your code above.
That method should look something like this:
albums: (params, route, options) ->
@collection = new Albums()
@view = new AccountAlbums collection: @collection
@collection.fetch
data:
page: params.page
The view shouldn't bother retrieving the data, it's the controller's job to tell the collection to get the data. Then the view automagically renders it.