是否有可能在单个工厂返回多个承诺?

时间:2013-10-04 11:16:24

标签: angularjs

angular.module('mymodule', [
])
  .controller('myController', [
      '$scope', 'myModel'
      function($scope, myModel) {
          // Some code here

      }
  ]);
  .factory('myModel', [
      '$scope',
      function($scope) {
          // Some code here

      }
  ]);

然后在您的路由文件中

$routeProvider.when('/someroute', {
    templateUrl: 'partials/someroute.html', 
    resolve: ['myModel'] //ALWAYS IN ARRAY)
});

在此代码中,myModel中有一个resolve方法。有没有办法向myModel添加第二个方法,这样你就可以使用相同的模型在另一个旋转函数中添加第二个“when”?

编辑: 我向工厂添加了一些方法(getOrder),但现在不知道如何调用specificik方法:

我尝试了这个但不起作用:

  when('/order/:id', {
                        templateUrl: 'partials/order/Order.html',
                        controller: "test.controller.order.Order",
                        resolve: {
                            getOrder: ["test.resolver.Order", function (resolveProvider) {
                                return resolveProvider().getOrder();
                            }
                            ]}

1 个答案:

答案 0 :(得分:1)

如果我理解你,那应该是这样的:

myModule.factory('ajax_post', ['$http',  function(_http) {   

    return{
        TypeOne: function(jsonData){
            var _promise= _http.post(/**/ );            
            return _promise; 
        },       
        TypeTwo: function(uploadedFile){

            var upload_promise =  _http.post(/**/);

            return upload_promise;
        },
        TypeThree: function(uploadedFile){

            var upload_promise =  _http.post(/**/);

            return upload_promise;
        }     
    }   
 }]);

在我的示例中,我使用了相同的factory,其中包含3种promise

  • 初始化
  • uploadFile_init
  • uploadFile_init_for_compare

    myModule.factory('ajax_post', ['$http',  function(_http) {   
    
    var path = 'src/php/data.ajax.php';
    
    return{
        init: function(jsonData){
            var _promise= _http.post(path, 
                jsonData
                ,{
                    headers: {
                        'SOAPActions': 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems'
                    }
                }
                );            
            return _promise; 
        },       
        uploadFile_init: function(uploadedFile){
            var fd = new FormData();
            fd.append("uploadedFile", uploadedFile);
            var upload_promise =  _http.post("src/php/data.ajax.php",
                fd,
                {
                    headers:{
                        'Content-Type':undefined
                    },
                    transformRequest:angular.identity
                });
    
            return upload_promise;
        },
        uploadFile_init_for_compare: function(uploadedFile){
            var fd = new FormData();
            fd.append("uploadedFileForCompare", uploadedFile);
            var upload_promise =  _http.post("src/php/data.ajax.php",
                fd,
                {
                    headers:{
                        'Content-Type':undefined
                    },
                    transformRequest:angular.identity
                });
    
            return upload_promise;
        }     
    

    }
    }]);

从控制器中,您可以将其命名为:

var promise_1 = ajax_post.init(someJsonData);
var promise_2 = ajax_post.uploadFile_init(someJsonData)
var promise_3 = ajax_post.uploadFile_init_for_compare(someJsonData);