What is the proper way to create a method with a dynamic number of parameters within an angularjs service?

StackOverflow https://stackoverflow.com/questions/23385989

  •  12-07-2023
  •  | 
  •  

質問

I'm currently using a custom AngularJS service to enable calling remote methods on my server-side. The wrapper service is intended to simplify calling these methods in my controllers, and returns a promise object.

Within the service, i'm calling a method from a provided API that takes the following arguments: 1) the name of the remote method, 2) a variable number of parameters and 3) as last parameter the callback function that is called after completion.

So this is the function that I'm wrapping with the service:
callRemoteAPI("nameOfRemoteMethod", [param1, param2, ... , paramN,] callBack() );

The callBack function returns the promise and implements the resolve() and reject() methods.

I've currently setup different functions within the service to handle different number of parameters: callNoParam, call1Param, call2Param, etc. to be able to call different remote methods that require a different number of parameters.

This works, but obviously is not the proper Object Oriented way of doing this, and I'm sure there is a better way. But so far I've been unsuccesfull of making this work in a dynamic way.

What is the best way of handling this dynamic # of params within the angular service?

Here's the code for the service:

app.factory('remoteAction', function($q) {
    return {
        callNoParams: function(method, p1) {
            var deferred = $q.defer();
            callRemoteApi(
                method,
                function(result) {
                    if (result.succes) deferred.resolve(result);
                    else deferred.reject(result);
                }
            );
            return deferred.promise;
        },
        call1Param: function(method, p1) {
            var deferred = $q.defer();
            callRemoteApi(
                method,
                p1,
                function(result) {
                    if (result.succes) deferred.resolve(result);
                    else deferred.reject(result);
                }
            );
            return deferred.promise;
        },
        call2Param: function(method, p1, p2) {
            var deferred = $q.defer();
            callRemoteApi(
                method,
                p1,p2,
                function(result) {
                    if (result.succes) deferred.resolve(result);
                    else deferred.reject(result);
                }
            );
            return deferred.promise;
        },
        call3Param: function(method, p1,p2,p3) {
            var deferred = $q.defer();
            callRemoteApi(
                method,
                p1,p2,p3,
                function(result) {
                    if (result.succes) deferred.resolve(result);
                    else deferred.reject(result);
                }
            );
            return deferred.promise;
        }
    };
// Could add more methods when needing more params, 
// but 1 dynamic function would be so much nicer...

});
役に立ちましたか?

解決

You can call the javascript function apply (MDN).

More on the arguments object on MDN

app.factory('remoteAction', function($q) {
    return {
        call: function() { //funcArgs should be an array
            var deferred = $q.defer();
            var parameters = arguments;
            parameters.push(function(result) {
                if (result.succes) deferred.resolve(result);
                else deferred.reject(result);
            });
            callRemoteApi.apply(this,parameters);
            return deferred.promise;
        }
    };
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top