我可以用不同的方式组织路径吗?

时间:2013-02-11 16:08:03

标签: javascript angularjs

我正在研究一个AngularJS应用程序,我是角色的新手,我正在$rootScope设置一个大对象,只要我需要它就可以从$scope获得。< / p>

此时它看起来像这样:

Application.run(['$rootScope', function($rootScope) {

    $rootScope.Envato = {

        API : {

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

            Methods : {

                Private : {

                    User : {

                        Vitals : $rootScope.Envato.API.Path.Private + "get-user-vitals.php",
                        Transactions : $rootScope.Envato.API.Path.Private + "get-earnings-and-sales-by-month.php",
                        Statement : $rootScope.Envato.API.Path.Private + "get-statement.php",
                        Sales : $rootScope.Envato.API.Path.Private + "get-recent-sales.php",
                        Account : $rootScope.Envato.API.Path.Private + "get-account-information.php"
                    },

                    Item : {

                        Purchase : {
                            Information : $rootScope.Envato.API.Path.Private + "get-purchase-information.php",
                            Download : $rootScope.Envato.API.Path.Private + "get-item-download-url.php"
                        }
                    }
                },

                Public : {

                    API : {

                        Releases : $rootScope.Envato.API.Path.Public + "get-api-releases.php"
                    },

                    Blog : {

                        Posts : $rootScope.Envato.API.Path.Public + "get-blog-posts.php",
                        Threads : {

                            Active : $rootScope.Envato.API.Path.Public + "get-active-threads.php",
                            Status : $rootScope.Envato.API.Path.Public + "get-thread-status.php"
                        },
                    },

                    Collections : $rootScope.Envato.API.Path.Public + "get-collections.php",

                    Items : {

                        Count : $rootScope.Envato.API.Path.Public + "get-number-of-items.php",
                        Popular : $rootScope.Envato.API.Path.Public + "get-popular-items.php",
                        Featured : $rootScope.Envato.API.Path.Public + "get-featured-items.php",

                        New : {

                            Marketplace : $rootScope.Envato.API.Path.Public + "get-new-items-from-market.php",
                            User : $rootScope.Envato.API.Path.Public + "get-new-items-from-user.php",
                            Random : $rootScope.Envato.API.Path.Public + "get-random-new-items.php"
                        },

                        Information : $rootScope.Envato.API.Path.Public + "get-item-information.php",
                        Prices : $rootScope.Envato.API.Path.Public + "get-item-prices.php"
                    },

                    Users : {

                        Total : $rootScope.Envato.API.Path.Public + "get-total-users-count.php",
                        Information : $rootScope.Envato.API.Path.Public + "get-user-information.php",
                        Items : {
                            Count : $rootScope.Envato.API.Path.Public + "get-user-items-by-site.php"
                        }
                    },

                    Search : $rootScope.Envato.API.Path.Public + "get-search-results.php"
                }
            }
        }
    };

    $rootScope.$safeApply = function($scope, fn) {

        fn = fn || function() {};

        if($scope.$$phase) {
            fn();
        } else {
            $scope.$apply(fn); 
        };
    };

}]);

但这是一团糟,我想知道是否有更好的方法,或者AngularJS还有一些其他很棒的东西可以帮助我做类似的事情:)

编辑:显然,像$rootScope.Envato.API.Path.Private之类的东西不起作用,但我添加了它,因为它更容易理解我正在尝试做什么。

1 个答案:

答案 0 :(得分:1)

看起来你正在寻找一项服务。查看this guide on the AngularJS site以了解如何创建它们。

Application.factory('Envato', function() {
  return {
    API: {
      Path: { ... },
    }
  };
});

Application.controller('SomeController', function($scope, Envato) {
  $scope.thing = Envato.API...;
});