简体   繁体   English

如何在AngularJS中将Modals作为服务处理响应

[英]How to Handle response from Modals as services in AngularJS

I am new to JavaScript and AngularJS. 我是JavaScript和AngularJS的新手。 There are couple things with JS that astounds me. JS有几件事令我惊讶。 For example I am trying to create a modal service and I found teh following example online. 例如,我尝试创建模式服务,并在网上以下示例中找到了它。 I wish to understand what is happening internally in the specific line where the modal is opened. 我想了解在打开模态的特定行内部发生了什么。

        $scope.checkout = function (cartObj) {
      var modalInstance = $modal.open({
      templateUrl : 'assets/menu/directives/payment-processing-modal.tmpl.html',
      controller : ["$scope", "$modalInstance", "cartObj", function($scope, $modalInstance, cartObj) {
                        $scope.submit = function () {
                            console.log("Submit");
                            //DB CALL HERE (with promise)
                            return "well";

                        };
                        $scope.cancel = function () {
                            console.log("Cancel");
                            $modalInstance.dismiss('cancel');
                            return "not so well";
                        };
      }],
      resolve : { // This fires up before controller loads and templates rendered
        cartObj : function() {
           return cartObj;
        }
      }
    });

In the Line : 在行中:

 var modalInstance = $modal.open({

What I understand is the the open method is called in the $modal service with a bunch of configurations set up. 我了解的是,在$ modal服务中调用open方法并设置了许多配置。

No within the controller I have my cartObj that is sent by the consuming view which I will then use to make certain CRUD operations. 在控制器中没有我有我的cartObj,它是由消费视图发送的,然后我将使用它进行某些CRUD操作。

My question is : I want the consuming view to know if there was a success or a failure of teh crud operation and also to return the data. 我的问题是:我希望使用的视图知道操作成功还是失败,并返回数据。 How can I implement this with a call back at the consuming view? 如何通过使用方视图的回调实现此目的? This is getting confusing because the "Submit" logic is within the Modals's controller. 这变得令人困惑,因为“提交”逻辑在Modals的控制器内。 1. When I return something from here, I am unable to access it on the consuming end. 1.当我从这里退货时,无法在消费端访问它。 How do return from within submit call? 如何在提交呼叫中返回? 2. How do I handle success and error based on this setup? 2.如何根据此设置处理成功和错误? Do I handle success and failure at modal service and return just data? 我是否在模态服务中处理成功和失败并仅返回数据? Or is there a way I can do it gracefully at consuming end, like: 还是有一种方法可以让我在消费端优雅地做到这一点,例如:

modalService.checkout(cartObj).success(function(response){
      //success handler
  }).error(function(response)){
      //failure handler
  }

As per your code, you are using AngularUI So, I will just complete your code, which is a way to solve your problem 根据您的代码,您正在使用AngularUI,所以,我将完成您的代码,这是解决您的问题的一种方法

$scope.checkout = function(cartObj) {
  var modalInstance = $modal.open({
    templateUrl: 'assets/menu/directives/payment-processing-modal.tmpl.html',
    controller: ["$scope", "$uibModalInstance", "cartObj", function($scope, $uibModalInstance, cartObj) {
      $scope.submit = function() {
        console.log("Submit");
        //DB CALL HERE (with promise)
        DB_CALL.then(function(success) {
          // It resolve and pass to success fuction of the result promise
          $uibModalInstance.close({ success: success });
        }, function(err) {
          // It rejects the result promise
          $uibModalInstance.dismiss({ error: err });
        });
        return "well";

      };
      $scope.cancel = function() {
        console.log("Cancel");
        // It rejects the result promise
        $uibModalInstance.dismiss({ error: 'cancel' });
        return "not so well";
      };
    }],
    resolve: { // This fires up before controller loads and templates rendered
      cartObj: function() {
        return cartObj;
      }
    }
  }).result;
}

first of all use $uibModalInstance instead modalInstance. 首先使用$ uibModalInstance代替modalInstance。 Read this 这个

Now what I have done is binded modalInstance with the "result" method provided by $modal.open(), which eventually returns a Promise, So now you can resolve the promise like this 现在,我完成的工作是将modalInstance与$ modal.open()提供的“结果”方法绑定在一起,该方法最终返回Promise,因此,现在您可以像这样解决Promise了

modalIntance.then(function(success) {
      /* Your success code goes here*/
    }, function(err) {
      /* Your failure code goes here*/
    });

Hope it helps. 希望能帮助到你。

I figured out an old school way to approach this issue. 我想出了一种古老的方法来解决这个问题。 I did not have to use promise at all. 我根本不必使用诺言。 From the consumer end I just return two functions in params like so at the consuming end : 从使用者端,我只返回了两个参数,例如在使用者端:

service.method(param, function(result){
      //success handler
     }, function(err){
      //error handler
     });

in the modal service, the signature changes as follows: 在模式服务中,签名更改如下:

service.method(params, succesFunc, errorFunc){
   //modal code
   }

Now I will just call successFunc or errorFunc call backs when I need to based on whether the DB call was a success or not and pass the data or error message respectively in the function's param. 现在,当我需要基于数据库调用是否成功并在函数的参数中分别传递数据或错误消息时,我将仅调用successFunc或errorFunc回调。 Like: 喜欢:

 $scope.submit = function() {
    console.log("Submit");
    //DB CALL HERE (with promise)
    DB_CALL.then(function(success) {
      // It resolve and pass to success fuction of the result promise
      successFunc(success);
    }, function(err) {
      // It rejects the result promise
      errorFunc(err);
    });
    return "well";

  };

Hope this helps someone in this kind of use case. 希望这对这种用例有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM