2012-08-10 61 views
1

我在CoffeeScript上使用Backbone.js和Chaplin.js。我有一個挑戰。如何在路由視圖中獲取url參數?如何在視圖中獲取路徑url參數?

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           

回答

0

這是一個老問題,但爲了完整性這裏有一個答案:

你的路由發送控制器內部數據到albums方法(accounts)的ISN不會顯示在上面的代碼中。 這種方法應該是這個樣子:

albums: (params, route, options) -> 
    @collection = new Albums() 
    @view = new AccountAlbums collection: @collection 
    @collection.fetch 
     data: 
     page: params.page 

的觀點不應該去檢索數據,這是控制器的工作告訴收集來獲取數據。然後視圖自動呈現它。