将功能添加到控制器内的工厂(单件)

时间:2016-07-27 02:05:39

标签: angularjs angular-ui-router

我有一个名为search的工厂。

我有很多控制器叫SearchCampaignControllerSearchQuotaController等等。

工厂search是这些控制器向其添加函数实现的单例对象。

问题是,名为SearchController的父控制器必须调用尚未实现的工厂search内的函数,因为子控制器在父控制器执行后执行。

好像我做错了。

angular
    .module('app.search')
    .factory('search', search);

search.$inject = ['$http', '$window', '$state', 'CONF', '$mdToast', 'USER_ROLES', 'USER_MODULES'];

function search($http, $window, $state, CONF, $mdToast, USER_ROLES, USER_MODULES) {

    var searchInfo;

    function updateQueryString(params) {
        $state.go(

            'search',

            {
                searchType: params.searchType,
                quotaId: params.quotaId,
                campaignName: params.campaignName,
                templateName: params.templateName,
                passId: params.passId,
                certId: params.certId
            },

            {notify: false}

        )
    }

    function getQuotaById(params) {
        var reqPath = CONF.apiPath + 'core/quotas/' + params.quotaId;
        return $http.get(reqPath);
    }

    function getCampaignById(params) {
        var reqPath = CONF.apiPath + 'core/quotas/' + params.quotaId + '/campaigns/' + params.campaignName;
        return $http.get(reqPath);
    }

    function queryCampaigns(params) {
        var reqPath = CONF.apiPath + 'core/quotas/' + params.quotaId + '/campaigns';
        return $http.get(reqPath);
    }

    function getTemplateById(params) {
        var reqPath = CONF.apiPath + 'core/quotas/' + params.quotaId + '/campaigns/' + params.templateName;
        return $http.get(reqPath);
    }

    function queryTemplates(params) {
        var reqPath = CONF.apiPath + 'core/campaigns/' + params.campaignName + '/templates';
        return $http.get(reqPath);
    }

    function getPassById(params) {
        var reqPath = CONF.apiPath + 'core/passes/' + params.passId;
        return $http.get(reqPath);
    }

    function getPassbookById(params) {
        var reqPath = CONF.apiPath + 'core/passbookCert/' + params.certId;
        return $http.get(reqPath);
    }

    function queryPassbookCerts(params) {
        var reqPath = CONF.apiPath + 'core/quotas/' + params.quotaId + '/passbookCerts';
        return $http.get(reqPath);
    }

    //Global search logic
    //NEED TO RE-FACTOR
    function searchMasterFunction(params, obj) {

        if(params.searchType){
            search.changeSearchType(params.searchType);
        }

        updateQueryString(params);

        if(params.quotaId){
            search.getQuotaAndDisplayResult(params);
            // search.getPassbookCertsAndDisplayResult(params);

            search.updateQuotaIdInTemplateTab(params); //special - needs re-visit
        }

        if(params.quotaId && !params.campaignName){
            search.getCampaignsAndDisplayResult(params);
        }

        if(params.quotaId && params.campaignName && params.templateName){
            search.getCampaignAndDisplayResult(params);
            search.getTemplateAndDisplayResult(params);
        }else if(params.quotaId && params.campaignName){
            search.getCampaignAndDisplayResult(params);
            search.getTemplatesAndDisplayResult(params);
        }else if(params.quotaId && params.templateName){
            search.getTemplateAndDisplayResult(params);
        }

        if(params.campaignName){
            search.getTemplatesAndDisplayResult(params);
        }

        if(params.passId){
            search.getPassAndDisplayResult(params);
        }

        //getPassbookById

    }

    var search = {

        searchInfo: searchInfo,

        searchMasterFunction: searchMasterFunction,

        getQuotaById: getQuotaById,

        getCampaignById: getCampaignById,
        queryCampaigns: queryCampaigns,

        getTemplateById: getTemplateById,
        queryTemplates: queryTemplates,

        getPassById: getPassById,

        getPassbookById: getPassbookById,
        queryPassbookCerts: queryPassbookCerts
    };

    return search;


}

这是我的父控制器,它应该在工厂search内调用searchMasterFunction,这样当查询字符串中有值时,它会根据search内的逻辑自动填充任何搜索结果。工厂。

angular
    .module('app.search')
    .controller('SearchController', SearchController);

SearchController.$inject = ['$state', 'search', '$scope', '$log'];

function SearchController($state, search, $scope, $log){

    $log.warn("Executing SearchController");

    var vm = this;

    vm.searchType = $state.params.searchType; //re-visit
    vm.tabs = ['quota', 'campaign', 'template', 'pass', 'cert'];

    vm.changeSearchTypeOnTabClick = changeSearchTypeOnTabClick;

    search.changeSearchType = changeSearchType;

    function changeSearchTypeOnTabClick(searchType) {
        $state.go('search', {searchType: searchType}, {notify: false});
    }

    function changeSearchType(searchType) {
        vm.searchType = searchType;
    }

    // this function call is what is causing the problem
    // search.searchMasterFunction($state.params);

}

以下是我的一个子控制器,它实现了search.getQuotaAndDisplayResult等功能。

角         .module( 'app.search')         .controller('SearchQuotaController',SearchQuotaController);

SearchQuotaController.$inject = ['search', '$scope', '$log'];

function SearchQuotaController(search, $scope, $log){

    $log.info("Executing SearchQuotaController");

    var vm = this;

    vm.searchInfo;
    vm.quota;

    vm.searchBtnClick = searchBtnClick;

    search.getQuotaAndDisplayResult = getQuotaAndDisplayResult; //THIS LINE IS WHAT NEEDS ATTENTION. I'm adding a function into the `search` factory.

    function searchBtnClick(params){

        search.searchMasterFunction(params);

    };

    function getQuotaAndDisplayResult(params) {
        vm.searchInfo = params; //update fields in the Quota view

        search.getQuotaById(params).then(function(quota){
            vm.quota = quota.data; //update the quota object in the view
        });
    };

}

所以问题是SearchQuotaController在SearchController之后运行,因此如果我尝试在SearchController中调用search.searchMasterFunction,它将无法正常执行,因为search.searchMasterFunction依赖于子控制器来执行以便执行函数完成。

非常感谢任何帮助。我已经考虑过$ broadcast方法,但它看起来像是黑客而不是真正的解决方案。

PS。我将子控制器中的函数添加到search工厂的原因是因为子控制器可以访问其本地$scope

0 个答案:

没有答案