2014-06-05 2 views
1

서버 쪽 페이징, 필터링 및 정렬을 처리하는 API가 있습니다. dataSource URL을 만들기 위해 transport.read를 (described in the documentation과 같은) 함수로 설정했습니다. 이 API는 두 번 호출됩니다 : 한 번 초기 아약스 요청을하는 동안 한 번, 그리고 options.success (result)가 호출 된 후에 다시 작동합니다. 다른 사람 누구나이 문제에 빠지니? options.success()가 정확히 수행하는 작업은 이미 필요한 데이터를 가지고있을 때 다른 XHR GET 요청을 발생시키는 것입니까?검도 그리드로 중복 API 호출하기


UPDATE : 아래 릭의 의견에

감사합니다, 나는 오늘 아침에 수행하고 무슨 일이 일어날 지보고 밖으로 다른 검도 그리드 설정 코드를 주석으로 시작해야 무엇을했다. dataSource가 변경되었을 때 실행되는 함수가 두 번째 API 호출을 유발하고 있음을 나타냅니다. 그리드가 정렬 될 때 API에 전송 된 URL에 업데이트 된 정렬 요청이 포함되어 있어야합니다. 이제 기본 정렬 매개 변수를 설정할 때 초기로드에서 변경 이벤트가 발생하는 이유가 혼란 스럽습니다. 내 설정을 아래에 업데이트했습니다.

var initGrid = function (take, skip, currPage, field, direction, model, gridElement, cols) { 
    //set up dataSource 
    var dataSource = new kendo.data.DataSource({ 
     transport: { 
      read: function (options) { 
       $.ajax({ 
        url: apiUrl + '?take=' + take + '&skip=' + skip + '&page=' + currPage + '&pageSize=' + take + '&sort%5B0%5D%5Bfield%5D=' + field + '&sort%5B0%5D%5Bdir%5D=' + direction, 
        dataType: 'json', 
        success: function (result) { 
         // by the time this is reached, the API has already been called once 
         // the result variable contains the necessary data 
         options.success(result); // the API is called again when this line executes 
        }, 
        error: function (result) { 
         options.error(result); 
        } 
       }); 
      } 
     }, 
     change: function() { 
      var currentSorting = JSON.stringify(this.sort()); 
      if (currentSorting != sorting) { 
       sorting = currentSorting; 
       this.sort(JSON.parse(sorting)); 
      } 
      field = JSON.parse(sorting)[0].field; 
      direction = JSON.parse(sorting)[0].dir; 
     }, 
     sort: { field: field, dir: direction }, //default sort 

     .... 

    } 
} 
+1

이 문제의 원인은 다른 코드 일 가능성이 큽니다. 내가 가진 것과 같은 데이터 소스의 간단한 예제를 만들 수 있으며 URL을 한 번만 호출합니다. –

+0

우수 의견. 그 결과 내 질문이 업데이트되었습니다. 중단 점을 시도하여 코드가 충돌했는지를 확인했지만 이전에는 주석을 제거하지 않았습니다. 아하. – esvendsen

답변

2

가 문제가 시도하고 업데이트했다 정렬 할 때 감지하는 데이터 소스 변경 이벤트를 사용하고 있었다 밝혀 :


여기 내는 dataSource 전송 설정입니다. transport.read 함수의 "options"매개 변수 내에있는 모든 업데이트 된 속성에 이미 액세스 할 수 있다는 것을 알지 못했습니다. 따라서 change 이벤트를 듣거나, 변수를 업데이트하거나, sort를 수동으로 호출하는 대신, 처음에 URL을 만들 때 옵션 객체를 사용했습니다. Take, skip, page, pageSize 및 sort가 모두 있습니다. 이런 식으로 검도는 모든 것을 독자적으로 처리합니다. 새 설정 :

var initGrid = function (model, gridElement, cols) { 
    //set up dataSource 
    var dataSource = new kendo.data.DataSource({ 
     transport: { 
      read: function (options) { 
       $.ajax({ 
        url: apiUrl + '?take=' + options.data.take + '&skip=' + options.data.skip + '&page=' + options.data.page + '&pageSize=' + options.data.pageSize + '&sort%5B0%5D%5Bfield%5D=' + options.data.sort[0].field + '&sort%5B0%5D%5Bdir%5D=' + options.data.sort[0].dir, 
        dataType: 'json', 
        success: function (result) { 
         options.success(result); 
        }, 
        error: function (result) { 
         options.error(result); 
        } 
       }); 
      } 
     }, 
     sort: { field: 'Id', dir: 'desc' }, //default sort 

     ....