1

I have the following method in a controller:

 $scope.showProductDetails = function (size,product) {

$scope.showLoader('Loading the details of the product. Please wait...');

    var modalInstance = $modal.open({
          templateUrl: 'productDetail.html',
          controller: 'ProductDetailController',
          size: size,
          resolve:{
            productDetails: function (){return productsFactoryHelper.ProductDetail.query({productid:product.id},function()
            {
            $scope.hideLoader();
            },function(error)
            {
                    commonFactory.Pop('error','This product is not available at this moment. Please try again later. If the problem persists contact a system administrator');
                    $scope.hideLoader();
            });}
          }

        });



};

As you can see, I'm opening a modal panel, and using 'resolve' to pass an object that I retrieve from the server. This call to the server can launch an exception, so I manage this case in the callback function for error, but I cannot avoid the modal panel to open if an error happens. I would like to know if there is a way to do that without changing the structure of the code. I mean I could achieve that just doing the call out of the resolve, before opening the modal panel, and depending if the call is successful or not , open the modal panel, but I would like to know if there is a way to do it inside the resolve, without changing the structure of the method.

Thanks

2 Answers 2

1

I think your idea of doing the call before opening the modal makes most sense in this case. In other words, only open the modal when it has the data.

Your other option is to display an error in the modal and change the contents/buttons accordingly.

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

Comments

1

Inside error callback function, return error again. You need to return a promise, so, if the returned promise is an error, it shouldn't open.

  resolve:{
            productDetails: function (){return productsFactoryHelper.ProductDetail.query({productid:product.id},function()
            {
            $scope.hideLoader();
            },function(error)
            {
                    commonFactory.Pop('error','This product is not available at this moment. Please try again later. If the problem persists contact a system administrator');
                    $scope.hideLoader();
                    return error
            });}
          }

2 Comments

I don't have three functions assigned to a variable, the other two functions are the callback functions, so once the main function finish, if it was succesful it will call the success callback function, otherwise it will call the error callback function. I need to do some work as hidding the load bar once the function returns but before the modal panel is opened, so this is the way I do it.
@fgonzalez ah ok, I got it, I think what is happening. Let me update my answer.

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.