2014-04-12 2 views
3

Restriction을 사용하여 백엔드에서 JSON 객체를 검색하려고합니다.Restriction을 사용한 오류 처리

1)이 잘 작동,하지만 난 콜백/약속 기능이 없기 때문에 나는 오류를 처리 할 수있는 방법을 알고하지 않습니다

meals = restAngular.all('meal').getList({date: selectedDate}).$object; 

다음은 약속 다음은 작동하지 않습니다

restAngular.all('meal').getList({date: selectedDate}).then(function(newMeals){ 
    console.log(newMeals); 
    meals = newMeals; 
}); 

내 Json 객체 대신 "newMeals"객체에 Restangular 객체가 있습니다. 뭔가 같은 :

addRestangularMethod: function(){var g=arguments,h=a?u:this; 
all: function() { [native code] } 
allUrl: function() { [native code] } 
clone: function() { [native code] } 
customDELETE: function() { [native code] } 
etc.. 

가 어떻게 약속을 사용하고 잠재적 인 오류를 처리 할 수있는, 내 JSON "식사"를받을 수 있나요?

2) 추가 질문 : Angularjs 1.2와 더 나은 새로운 $ 자원이 구현되었으므로 Restangular은 여전히 ​​좋은 선택입니까?

덕분에 많은

답변

7

이 같은 수행 할 수 있습니다

restAngular.all('meal').getList({date: selectedDate}).then(function(newMeals){ 
    console.log(newMeals); 
    meals = newMeals; 
}, function(response) { 
    console.log("Error with status code", response.status); 
}); 
+0

정답입니다. 그것을 시도했다. 그것은 효과가 있었다. –

1

을 얻으려면 일반 응답 객체를

// using .plain() method 
restAngular.all('meal').getList({date: selectedDate}).then(function(newMeals) { 
    console.log(newMeals); // prints restangular object, ie, your response object with additional methods like get, post, put, remove, clone and many others 

    // To get plain response 
    meals = newMeals.plain(); 
    console.log(meals); // prints plain response object 
}) 
.catch(function(response) { 
    console.log("Error with status code", response.status); 
}); 

달리

// you can configure this globally in restangular config by adding below line of code 
// so that you will get plain response data by default 
restAngular.setPlainByDefault(true); 

// and now 
restAngular.all('meal').getList({date: selectedDate}).then(function(newMeals) { 
    console.log(newMeals); // prints plain object since setPlainByDefault is set to true in restangular config 
    meals = newMeals; 
}) 
.catch(function(response) { 
    console.log("Error with status code", response.status); 
}); 

자세히 알아 보려면 documentation을 참조하십시오.

관련 문제