AngularJS最佳实践 - 采用多种方法的工厂 [英] AngularJS Best Practice - Factory with multiple methods

查看:152
本文介绍了AngularJS最佳实践 - 采用多种方法的工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工厂,提供三种不同的 $ http.get 方法。

I have a factory that serves up three different $http.get methods.

angular.module('myApp')
	.factory('getFactory', function ($http, $q) {

	return {

		methodOne: function () {
			var deferred  = $q.defer();

			$http.get('path/to/data')
				.then(function successCallback (data) {
    					deferred.resolve(data);
    				},function errorCallback (response) {
                                        console.log(response);
                                });

			return deferred.promise;
		},

		methodTwo: function (arg1, arg2) {
			var deferred  = $q.defer();

			$http.get('path/to/' + arg1 + '/some/' + arg2 + 'more/data')
				.then(function successCallback (data) {
    					deferred.resolve(data);
    				},function errorCallback (response) {
                                        console.log(response);
                                });

			return deferred.promise;
		},

		methodThree: function (arg1, arg2, arg3) {
			var deferred  = $q.defer();

			$http.get('path/to/' + arg1 + '/some/' + arg2 + '/more/' + arg3 + '/data')
				.then(function successCallback (data) {
    					deferred.resolve(data);
    				},function errorCallback (response) {
                                        console.log(response);
                                });

			return deferred.promise;
		},
	};
	});

基本上,这些方法只在路径上有所不同从中获取数据。这些方法获得的数据在控制器中处理。我一直在阅读很多 Angular最佳实践,并且一直在看DRY(不要重复自己)的提示。

Basically, these methods only differ in the path it gets the data from. The data these methods get are handled in the controller. I have been reading a lot of Angular best practices on the side and have been seeing DRY (Don't Repeat Yourself) tips.

我觉得我上面的代码太重复了。有没有更好的方法来编码 Angular方式

I feel the code I have above is too repetitive. Is there a better way of coding this the Angular way?

**注意:我使用yeoman生成器来构建项目的目录。

**Note: I used the yeoman generator to scaffold the directories of my project.

推荐答案

angular.module('myApp')
    .factory('getFactory', function ($http, $q) {

    //Using revealing Module pattern
    var exposedAPI = {
        methodOne: methodOne,
        methodTwo: methodTwo,
        methodThree: methodThree
    };

    return exposedAPI;

    //Private function, not required to be exposed
    function get(url){
        //$http itself returns a promise, so no need to explicitly create another deferred object
        return $http.get(url)
                .then(function (data) {
                    //Create deferred object only if you want to manipulate returned data
                }, function (msg, code) {                       
                    console.log(msg, code);
                });
    }

    function methodOne() {
        //DRY
        return get('path/to/data');
    }

    function methodTwo(arg1, arg2) {
        return get('path/to/' + arg1 + '/some/' + arg2 + 'more/data');
    }

    function methodThree(arg1, arg2, arg3) {
        return get('path/to/' + arg1 + '/some/' + arg2 + '/more/' + arg3 + '/data');
    }
    });

这篇关于AngularJS最佳实践 - 采用多种方法的工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆