2014-11-13 4 views
1

PUT에 대한 응답으로 반환 된 데이터 업데이트를 유지하려면 어떻게해야합니까?각 자원 PUT 응답이 손실 됨

저는 서버에 데이터를 성공적으로 검색, 업데이트 및 기록하는 기본 각도 (vsn 1.2.26) RESTful 앱이 있습니다. 서버는 업데이트 된 레코드의 "updateTime"필드를 변경하고 (200) 응답으로 브라우저에 반환합니다. $ resource.save 콜백 함수에서 업데이트 된 값을 볼 수 있지만이를 콜백 지속 시간을 초과하는 $ 범위로 유지하여 UI에 표시되도록하는 방법을 생각할 수 없습니다.

$scope.$apply(); 

또는 당신은 당신이로 기능을 변경할 수 있습니다 :

angular.module('myResources',['ngResource']) 
    .factory('Fund',['$resource',function($resource) 
     { 
     return $resource('http://myhost/xyz/fund/:id' 
         ,{ id: '@guid' } 
         ,{ save: { method: 'PUT' } 
          ,query: {method: 'GET', isArray: false } 
         } 
         ); 
     }]) 
... 
$scope.selectRecord = function(R) 
    { 
    ... 
    $scope.record = R; 
    } 

$scope.saveRecordChanges = function() 
    { 
    ... 
    myFundResource.save($scope.record,function(response){ 
     $scope.record = angular.fromJson(response); // gets refreshed data but doesn't update UI 
     console.log("new updateTime=" + $scope.record.updateTime); // correctly displays new value in the log 
     }); 
    } 
+0

Json을 호출하기 전에 응답을 표시 할 수 있습니까? json을 javascrit 객체로 수동으로 변환 할 필요가없는 것 같습니다. –

+0

@ Tyler.z.yang 여기에 대한 응답 : 응답 헤더 : 콘텐츠 형식 : \t application/json; charset = UTF-8 콘텐츠 길이 : 본문 : { "guid": "357fad51-62d5-420c-9372-1449817192b8", "fundCode": "46", "name": "ABACAB" 시스템 ":"X17 ","updateTime ":"2014-11-13T16 : 27 : 14 "} –

+0

어, 내 응답은 myFundResource.save 콜백입니다. 저장 콜백에서 응답을 기록하기 위해 console.log()를 사용할 수 있습니까? –

답변

0

내 오류는 ng-repeat 레코드 목록이 업데이트 될 것으로 예상했지만 목록을 업데이트하지 않고 $ scope.record (현재 레코드) 포인터 만 새 개체로 리디렉션했습니다.

myFundResource.save($scope.record,function(response) 
    { 
    var vIdx = $scope.allRecords.indexOf($scope.record); // allRecords is bound to the UI table via ng-repeat 
    $scope.record = response; // ** $scope.record no longer points to the same object as $scope.allRecords[vIdx] - array is unaware of this change 
    $scope.allRecords[vIdx] = $scope.record; // replace array element with new object 
    }); 

날 올바른 방향으로 생각을 가지고 당신의 통찰력과 질문 @ Tyler.z.yang에게 감사 : 나는 레코드 목록을 업데이트하는 코드를 추가했다.

0

당신은 콜백 함수 저장 당신이 추가 할 수 있습니다, 그것은 $를 호출 각도 $ 약속에

$scope.saveRecordChanges= function(){ 
    myFundResponse.save($scope.record).$promise.then(function(response){ 
     $scope.record = response; 
    }); 
}; 

사촌 asyncEval을 success 또는 error 콜백을 호출하기 위해서는 각도 $ promise를 사용하면 뷰가 자동으로 업데이트됩니다.

+0

$ scope. $ apply()가 다이제스트 진행 중 오류를 발생 시켰습니다. –

+0

@MichaelLemaire $ promise.then을 사용할 수 있습니까? –

+0

$ promise가 실행되었지만 여전히 $ scope에 업데이트를 할당하지 않았습니다. 아마도 이것은 내가 이상한 초보자 방식으로 뭔가를하고 있다는 신호인가? :) –