Backbone.PageableCollection 定义的搜索

Backbone.PageableCollection defined search

本文关键字:搜索 定义 PageableCollection Backbone      更新时间:2023-09-26

我正在尝试将Backbone.PageableCollection与附加的queryParams一起使用,但不起作用。

我已经尝试了 _.extend 查询参数和状态,但产生了一个值=值查询结果。例如,如果我将 {gender: 'male'} 添加到查询参数并声明,则查询字符串是 ...?男=男... 我需要知道如何在服务器端解释这一点。 我尝试使用下划线的反转来获得值=键对,但这没有帮助。

咖啡脚本:

   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

任何帮助将不胜感激

我找到了答案。 我无法在"初始化"期间应用"状态"数据。 我只是在集合获取之前添加了 _.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