2014-12-03 68 views
0

我想與其他queryParams一起使用Backbone.PageableCollection,但不起作用。Backbone.PageableCollection定義的搜索

我試過_.extend queryParams和狀態,但產生值=值查詢結果。例如,如果我將{gender:'male'}添加到queryParams和state,那麼查詢字符串是...?male = male ...我需要知道如何在服務器端解釋這個。我嘗試使用下劃線的反轉來有一個值=密鑰對,但這並沒有幫助。

的CoffeeScript:

class Entities.UserCollection extends Backbone.PageableCollection 
     initialize: (searchCriteriaModel) ->     
      _.extend(@queryParams, searchCriteriaModel) 
      _.extend(@state, searchCriteriaModel)   

     model: Entities.UserModel 
     url: '/api/ra/users' 
     state: 
      firstPage: 1 
      currentPage: 1 
      pageSize: 6 
      totalRecords: 200 

任何幫助,將不勝感激

回答

0

我找到了答案。 '初始化'期間我無法應用'狀態'數據。我只是在集合獲取之前添加了_.extend。我也發現我可以用同樣的方式操縱狀態。

更新的代碼:

class Entities.UserModel extends Backbone.Model 

     class Entities.UserCollection extends Backbone.PageableCollection 
      model: Entities.UserModel 
      url: '/api/ra/users' 
      state: 
       firstPage: 1 
       currentPage: 1 

      queryParams: 
       currentPage: "current_page" 
       pageSize: "page_size" 

       offset:() -> (@state.currentPage-1) * @state.pageSize 


     API = 
      getUsers: (state, searchCriteriaModel) -> 
       items = new Entities.UserCollection 
       _.extend items.state, state 
       _.extend items.queryParams, searchCriteriaModel 

       items.fetch 
        async: false 
        reset: true     
        success: (collection, response, options) -> 
         console.log "success:", collection 

        error: (collection, response, options) -> 
         console.log "error:", collection 
       items