2013-06-11 4 views
4

일치하지 않는값은 내가 POST 방법이라고 업데이 트와 자원 공장이 서버 출력

내가 예상대로 서버에 데이터를 게시합니다 메서드 호출 할 때 :

$rootScope.toggleStar = function (post, feedname) { 
    var updated = Feed.update(post); 
    this.child.StarId = updated.StarId; 
} 

그리고 서버가 올바른 값을 반환 (이 JSON에서 StarId 통지) : 그러나

{"Name":"13 Ways to Act Like A Business Owner","ItemDate":"June 6, 2013","Url":"/post/13-Ways-to-Act-Like-A-Business-Owner-Stop-Acting-Like-an-Advisor-All-the-Time-(6-min-03-sec).aspx","StarImg":"bulletstar-on.png","StarId":1324,"StarDate":"0001-01-01T00:00:00","FeedCount":0,"FeedId":19,"SourceIcon":null,"IsBroken":false,"ItemId":"01"} 

StarId에 대한 var 업데이트 된 반환 값을 보면 "0"인지 알 수 있습니다.

enter image description here enter image description here

누군가가 왜 그런지 설명하고이 상황에서 반환 값을 얻을 수 있습니까?

답변

3

귀하의 var updated = Feed.update(post);은 서버에 비동기 호출을하고 immedaitly를 반환하고 서버가 데이터를 반환하자마자 updated 객체가 업데이트됩니다. 그래서 나는 당신이 업데이트 된 것을 액세스하려고 시도했다고 생각합니다. 너무 일찍. angular doc에서 :

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data. This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data. This means ththeat in most case one never has to write a callback function for the action methods.

같은 것을보십시오 :

$rootScope.toggleStar = function (post, feedname) { 
    var updated = Feed.update(post, function(f) { 
    this.child.StarId = f.StarId; 
    });  
} 
의미가
+0

, 나는이 문서에서 읽었습니다,하지만 난 값을 얻을 수있는 구문을 찾을 수 없습니다 마침내 돌아 왔을 때. 그 생각? – codethrift

+0

내 대답을 가능한 해결책으로 업데이트했습니다. –

+0

가까이에 있습니다. this.child.StarId = f.StarId - f.StarId가 이제 반환 된 값을 보유하지만 this.child.StarId가 오류를 throw합니다. 정의되지 않은 'StarId'속성을 설정할 수 없습니다. – codethrift