Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 使用Backgrid和PageableCollection获取方法触发两次_Backbone.js_Pagination_Backbone.js Collections_Backgrid - Fatal编程技术网

Backbone.js 使用Backgrid和PageableCollection获取方法触发两次

Backbone.js 使用Backgrid和PageableCollection获取方法触发两次,backbone.js,pagination,backbone.js-collections,backgrid,Backbone.js,Pagination,Backbone.js Collections,Backgrid,我使用Backgrid和Pageablecollection进行分页,每次我发出请求时,我都能在ChromeDeveloper的网络选项卡上看到请求被触发了两次,我不知道为什么 我的代码如下所示: RecipeManager.module('Entities', function (Entities, RecipeManager, Backbone, Marionette, $, _) { Entities.Recipe = Backbone.NestedModel.extend(

我使用Backgrid和Pageablecollection进行分页,每次我发出请求时,我都能在ChromeDeveloper的网络选项卡上看到请求被触发了两次,我不知道为什么

我的代码如下所示:

RecipeManager.module('Entities', function (Entities, RecipeManager, Backbone, Marionette, $, _) {    
  Entities.Recipe = Backbone.NestedModel.extend({
     urlRoot: 'recipes,
     parse: function (response) {
       return response.results[0]
      }
    }

  Entities.RecipeCollection = Backbone.PageableCollection.extend({
     model: Entities.Recipe,
     sync: function (method, model, options)
       { // custom headers in here},
     state: {
        firstPage: 1
        }
     queryParams: {
      currentPage: 'page',
      pageSize: 'per_page'
    },
    parseState: function (resp, queryParams, state, options) {
      return {totalRecords: resp.meta.total}
    },
    parseRecords: function (response, options) {
      return response.results
    }      
  })

  var API = {
     getRecipeEntities: function () {
        var recipes = new Entities.RecipeCollection()
        var defer = $.Deferred()
        recipes.fetch({
          reset: true,
          url: 'https://myurl/forgetting/data',
          success: function (data) {
             defer.resolve(data)
            }
          })
          var promise = defer.promise()
          $.when(promise).done(function (fetchedRecipes) {
          })
          return promise
        }
      }

  RecipeManager.reqres.setHandler('recipe:entities', function () {
    return API.getRecipeEntities()
 })
然后在Recipes.ListController中,我有以下内容:

RecipeManager.module('RecipesApp.List', function (List, RecipeManager, Backbone, Marionette, $, _) {
  List.Controller = {
    var recipesListLayout = new List.Layout()
    listRecipes: function () {
     var fetchingRecipes = RecipeManager.request('recipe:entities')
      $.when(fetchingRecipes).done(function (recipes) {
        var recipesListView = new List.Recipes({
        collection: recipes
       })  
     })
   var columns = [{ // load of columns defined here }]
   recipesListLayout.on('show', function(){
     var paginator = new Backgrid.Extension.Paginator({
        collection: recipes,
        render: function () {
         this.$el.find('ul').addClass('pagination')
         return this
         }
    )}
    recipesListLayout.$el.append(paginator.render().el)
   }
  }
RecipeManager.regions.main.show(recipesListLayout)
}

当有疑问时:将
调试器
添加到
fetch()
代码中,然后等待。它应该停止两次,查看堆栈跟踪,看看它从哪里来——我总是这样做,当某些东西没有按照我期望的方式工作时。推荐10/10!当有疑问时:将
调试器
添加到
fetch()
代码中,然后等待。它应该停止两次,查看堆栈跟踪,看看它从哪里来——我总是这样做,当某些东西没有按照我期望的方式工作时。推荐10/10!