0

as I understand it Angular http has 2 checks 'success and 'error'. Thats in terms of connecting to the service or not - so I have that in hand and thats my first check.

The issue I have is that the data in my JSON has a success state which informs me if the data it contains or has received from my form had any problems with it, in which case there will be an error object that I act on and display to the user.

I need to check for that value of success, but where is the best place to check for that?

Should I be doing it in the controller?

Without that data being correct theres nothing else for the page to do so it is effectively the first thing that needs to be done after the data is retrieved.

heres the basic controller layout

 app.controller("dataCtrl", function ($scope, $http) {

$http.post('/getdata').success(function (data) {

    $scope.businessData = data;

    // Should I then be checking businessData.success at this level?

}).error(function () {
    alert("Problem");
});

});
3
  • If your response header has one of many error statuses, the flow will switch to error block automatically. You don't have to check for success or failure if your API responds with errors properly. Commented May 12, 2015 at 14:39
  • This is going to end up being developer preference, but the question is what operations should happen in your controller vs in your service vs in your API. In my opinion, it should happen in a service as that is where business logic should be, your controller is for binding data to your view and shouldn't handle lots of logic. Commented May 12, 2015 at 14:39
  • Yes you can check it in the success callback and perform your action as per the result!!!! Commented May 12, 2015 at 14:44

1 Answer 1

1

You can write something like this:

$http.post('/getdata').success(function (data) {
  if (validate(data)) {
    $scope.businessData = data;
  } else {
    $scop.buisnessDataError = {msg: 'smth bad happend'};
  }
}).error(function () {..})

Otherwise, you can write your validator in Promise-like style and then just chain promises in such manner:

$http.post('/getdata').then(function (res) {
   return validator(null, res.data);
}, function (err) { 
   return validator({msg: 'error'})
}).then(function (data) {
   //proceed your data
}, function (err) {
  alert(err.msg);
});

Where validator is:

var varlidator = function (err, data) {
  return $q(function (resolve, reject) {
   if (/*data is not valid*/ || err) {
     reject(err);
   } else {
     resolve(data);
   }
  });
}

$q is a standard angulars implementation of Promises

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will try option 1 at the moment as it seems less code for a simple check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.