mardi 9 avril 2019

How can I Migrating this factory from AngularJS 1.2 to AngularJS 1.3+

I would appreciate your help, someone has an idea how to write this code to work on angularjs 1.3

AngularJS

Migrating from 1.2 to 1.3

"due to 77ada4c8, You can no longer invoke .bind, .call or .apply on a function in AngularJS expressions. This is to disallow changing the behaviour of existing functions in an unforeseen fashion."

$api.post('App/Login', {q: q})



app.factory('$api', function ($deferred, $http) {
    var APIBASE = 'index.php';
    function _invoke(method, path, params) {
        var pathParts = path.split('/');

        if (typeof params == 'undefined')
            params = {};
        params.module = pathParts.shift();

        if (pathParts.length)
            params.api = pathParts.shift();
        var options = {
            method: method,
            url: APIBASE
        }
        if (params) {
            if (method == 'GET')
                options.params = params;
            else
                options.data = params;
        }
        var deferred = $deferred.create();
        $http(options)
        .then(function (data, status, headers, config) {
                    data.then ?
                            deferred.resolve(data.result, null) :
                            deferred.reject(data.error ? data.error.message : "No response");

        }, function (data, status, headers, config) {
                    deferred.reject(data ? data : "ERR: " + status);
                });
        return deferred.promise;
    }
    return {
        'get': function (path, params) {

            return _invoke.apply(null, ['GET', path, params]);
        },
        'post': function (path, params) {

            return _invoke.apply(null, ['POST', path, params]);
        },
    }

});




Aucun commentaire:

Enregistrer un commentaire