0

当我几天前开始使用 AngularJS 时,我发现了很多有用的东西,并且我意识到在你的 Web 应用程序中使用它比从头开始构建所有东西(有时这是必要的,但在大多数情况下不是)或使用许多库可能会使应用程序的加载时间变得混乱。

所以,我有一个用于在线商店的 php API 包装器,它有很多方法(我试图模仿 REST,但 API 只提供 POST 和 GET ),我开始为它构建一个 JS 脚本,可以用来调用无需任何 PHP 知识或无需接触任何 PHP 代码的方法。

使用 AngularJS 我想出了这个:

var Application = Application || {};

;(function($, window, document, undefined) {

"use strict";

var Envato = {};

Application.Envato = angular.module("Envato", []);

Application.Envato.constant("API", {

    Path : {
        Private : "api/envato/private/",
        Public : "api/envato/public/"
    }
});

})(jQuery, window, document);

以上将是我的模块,放置在一个modules文件夹中。接下来,将是围绕模块的工厂构建(与上述模块位于同一文件夹中):

var Application = Application || {};

;(function($, window, document, undefined) {

"use strict";

Application.Envato.factory("APIXHRService", function($http, $timeout, $q) {

    return function(method, url, data) {

        var deferred = $q.defer(),
            headers = {
                "Content-Type" : "Application/JSON"
            };

        $http({ method : method, url : url, data : data, headers : headers }).success(function(_data) {

            deferred.resolve(_data);
            deferred = $q.defer();
        }).error(function(_data) {

            deferred.resolve(_data || "Request [ Failed ]");
            deferred = $q.defer();
        });

        return deferred.promise;
    };
});

Application.Envato.factory("Envato", function(APIXHRService, API) {

    return {

        API : {

            Private : {

                User : {

                    Account : function(user){
                        return APIXHRService("POST", API.Path.Private + "get-account-information.php", { user : user });
                    },

                    Statement : function(user) {
                        return APIXHRService("POST", API.Path.Private + "get-statement.php", { user : user });
                    },

                    Sales : function(user) {
                        return APIXHRService("POST", API.Path.Private + "get-recent-sales.php", { user : user });
                    },

                    Transactions : function(user) {
                        return APIXHRService("POST", API.Path.Private + "get-earnings-and-sales-by-month.php", { user : user });
                    },

                    Vitals : function(user) {
                        return APIXHRService("POST", API.Path.Private + "get-user-vitals.php", { user : user });
                    }
                },

                Purchases : {

                    Download : function(user, token) {
                        return APIXHRService("POST", API.Path.Private + "get-item-download-url.php", { user : user, token : token });
                    },

                    Information : function(user, token) {
                        return APIXHRService("POST", API.Path.Private + "get-purchase-information.php", { user : user, token : token });
                    }
                }
            },

            Public : {

                API : {

                    Releases : function() {
                        return APIXHRService("GET", API.Path.Public + "get-api-releases.php");
                    }
                },

                Blog : {

                    Posts : function(marketplaces) {
                        return APIXHRService("POST", API.Path.Public + "get-blog-posts.php", { marketplaces : marketplaces });
                    },

                    Threads : {

                        Active : function(marketplaces) {
                            return APIXHRService("POST", API.Path.Public + "get-active-threads.php", { marketplaces : marketplaces });
                        },

                        Status : function(threads) {
                            return APIXHRService("POST", API.Path.Public + "get-thread-status.php", { threads : threads });
                        }
                    }
                },

                Collections : function(collections) {
                    return APIXHRService("POST", API.Path.Public + "get-collections.php", { collections : collections });
                },

                Items : {

                    Count : function(marketplaces) {
                        return APIXHRService("POST", API.Path.Public + "get-items-count.php", { marketplaces : marketplaces });
                    },

                    Featured : function(marketplaces) {
                        return APIXHRService("POST", API.Path.Public + "get-featured-items.php", { marketplaces : marketplaces });
                    },

                    Information : function(items) {
                        return APIXHRService("POST", API.Path.Public + "get-item-information.php", { items : items });
                    },

                    Latest : {

                        Marketplace : function(marketplaces) {
                            return APIXHRService("POST", API.Path.Public + "get-new-items-from-market.php", { marketplaces : marketplaces });
                        },

                        Random : function(marketplaces) {
                            return APIXHRService("POST", API.Path.Public + "get-random-new-items.php", { marketplaces : marketplaces });
                        },

                        User : function(users) {
                            return APIXHRService("POST", API.Path.Public + "get-new-items-from-user.php", { users : users });
                        }
                    },

                    Popular : function(marketplaces) {
                        return APIXHRService("POST", API.Path.Public + "get-popular-items.php", { marketplaces : marketplaces });
                    },

                    Price : function(items) {
                        return APIXHRService("POST", API.Path.Public + "get-item-price.php", { items : items });
                    }
                },

                Users : {

                    Information : function(users) {
                        return APIXHRService("POST", API.Path.Public + "get-user-information.php", { users : users });
                    },

                    Items : {

                        Count : function(users) {
                            return APIXHRService("POST", API.Path.Public + "get-user-items-by-site.php", { users : users });
                        }
                    },

                    Total : function() {
                        return APIXHRService("GET", API.Path.Public + "get-total-users-count.php");
                    },
                },

                Search : function(expression, marketplace, categories) {
                    return APIXHRService("POST", API.Path.Public + "get-search-results.php", { expression : expression, marketplace : ( marketplace === undefined ? "" : marketplace ), categories : ( categories === undefined ? "" : categories ) });
                }
            }
        }
    };
});

})(jQuery, window, document);

最后,如果需要,我有一个控制器,可以让我检查方法是否正常工作:

var Application = Application || {};

;(function($, window, document, undefined) {

"use strict";

Application.controller("EnvatoAPIController", ["$scope", "_debounce", "Envato", function($scope, _debounce, Envato) {

    $scope.getData = _debounce(function($event) {

        $event.preventDefault();

        return Envato.API.Private.Purchases.Download("chaoscod3r", "52dfdscb1-dada-4ff8-bfcb-d3fdsbn55mda").then(function(data) {
            $scope.data = JSON.stringify(data, undefined, 4);
        });

    }, 250, false);

}]);

})(jQuery, window, document);

我发布这篇文章的原因是我正在寻找一些关于如何改进我的代码的见解,因为在我看来,那里发生的事情有点多。也许使用 Angular 有更好的方法?我没有机会阅读所有 AngularJS API 文档,所以我想这里肯定有很多开发人员在使用 Angular 并且可以给我一些提示 :)

编辑:问题是我怎样才能折射Envato工厂?哪种方式最好?

4

1 回答 1

1

如果省略全局 app var 声明并将所有 angular 模块附加到 angular 对象,则可以节省一些行。(我不确定在哪里放置“使用严格”;虽然声明。)

还使用括号表示法进行依赖注入,以便稍后启用 js 代码缩小。

前:

var Application = Application || {};

;(function($, window, document, undefined) {

    Application.Envato.factory("APIXHRService", function($http, $timeout, $q) {
       return function (
       //something
    });

    Application.Envato.factory("Envato", function(APIXHRService, API) {
        return {
            //api
        };
    });

})(jQuery, window, document);

后:

angular.factory("APIXHRService", ["$http", "$timeout", "$q", function($http, $timeout, $q) {
   return function (
   //something
}])

.factory("Envato", ["APIXHRService", "API", function(APIXHRService, API) {
    return {
        //api
    };
}]);
于 2013-02-12T19:28:42.147 回答