ABP之Javascript產生

來源:互聯網
上載者:User

標籤:

還是服務

在調試SimpleTaskSystem的AngularJs demo時,一開始我只看到對服務的應用。

app.controller(controllerId, [‘$scope‘, ‘abp.services.tasksystem.task‘,function($scope, taskService){}]);

  

在尋找原始碼中的所有js檔案後還是沒找到abp.services.tasksystem.task的定義,那麼現在就剩下最後一種情況。這些服務是系統產生的,這樣的話與動態WebApi的設計思路也是一致的。在layout.cshtml中有兩處js引用

<script src="~/api/AbpServiceProxies/GetAll?type=angular"></script><script src="~/AbpScripts/GetScripts" type="text/javascript"></script>

  

 

 

產生所有服務

~/api/AbpServiceProxies/GetAll?type=angular 對應的就是就是Abp對系統所有服務產生的JavaScript,現在對url進行反推我們可以在Abp.Web.Api中找到AbpServiceProxiesController,其中有一ScriptProxyManager 類型的欄位_scriptProxyManager。ScriptProxyManager就是產生所有服務的一管理者。

在AbpServiceProxiesController中的GetAll方法有一參數type。這個參數表示根據什麼js架構產生javascript,目前Abp提供了Angular與jQuery兩種支援。

 

在ScriptProxyManager中會根據不同的type調用不同的IScriptProxyGenerator產生javascript代碼。以Angular的實現AngularProxyGenerator為例。

 

 

        public string Generate()        {            var script = new StringBuilder();            script.AppendLine("(function (abp, angular) {");            script.AppendLine("");            script.AppendLine("    if (!angular) {");            script.AppendLine("        return;");            script.AppendLine("    }");            script.AppendLine("    ");            script.AppendLine("    var abpModule = angular.module(‘abp‘);");            script.AppendLine("    ");            script.AppendLine("    abpModule.factory(‘abp.services." + _controllerInfo.ServiceName.Replace("/", ".") + "‘, [");            script.AppendLine("        ‘$http‘, function ($http) {");            script.AppendLine("            return new function () {");            foreach (var methodInfo in _controllerInfo.Actions.Values)            {                var actionWriter = CreateActionScriptWriter(_controllerInfo, methodInfo);                script.AppendLine("                this." + methodInfo.ActionName.ToCamelCase() + " = function (" + GenerateJsMethodParameterList(methodInfo.Method) + ") {");                script.AppendLine("                    return $http(angular.extend({");                script.AppendLine("                        abp: true,");                script.AppendLine("                        url: abp.appPath + ‘" + actionWriter.GetUrl() + "‘,");                actionWriter.WriteTo(script);                script.AppendLine("                    }, httpParams));");                script.AppendLine("                };");                script.AppendLine("                ");            }            script.AppendLine("            };");            script.AppendLine("        }");            script.AppendLine("    ]);");            script.AppendLine();            //generate all methods            script.AppendLine();            script.AppendLine("})((abp || (abp = {})), (angular || undefined));");            return script.ToString();        }
View Code

 

 

 

AngularProxyGenerator對所有的服務與Action進行了掃描產生javascript。

不過將所有服務都返回到用戶端,好像並不怎麼安全。

另外ScriptProxyManager對產生的javascript代碼進行了緩衝。

 

 

基礎配置

~/AbpScripts/GetScripts對應的則是Abp.Web.Mvc下的AbpScriptsController,AbpScriptsController主要提供一些基礎的配置資訊到用戶端。

 

        [DisableAuditing]        public async Task<ActionResult> GetScripts()        {            var sb = new StringBuilder();            sb.AppendLine(_multiTenancyScriptManager.GetScript());            sb.AppendLine();            sb.AppendLine(_sessionScriptManager.GetScript());            sb.AppendLine();                        sb.AppendLine(_localizationScriptManager.GetScript());            sb.AppendLine();                        sb.AppendLine(await _authorizationScriptManager.GetScriptAsync());            sb.AppendLine();                        sb.AppendLine(await _navigationScriptManager.GetScriptAsync());            sb.AppendLine();                        sb.AppendLine(await _settingScriptManager.GetScriptAsync());            sb.AppendLine(GetTriggerScript());            return Content(sb.ToString(), "application/x-javascript", Encoding.UTF8);        }

  

 

這些資訊分別是:

 

介面

實現

說明

IMultiTenancyScriptManager

MultiTenancyScriptManager

多租戶配置

ISettingScriptManager

SettingScriptManager

Abp基礎配置

INavigationScriptManager

NavigationScriptManager

導航資訊

ILocalizationScriptManager

LocalizationScriptManager

本地化

IAuthorizationScriptManager

AuthorizationScriptManager

許可權

ISessionScriptManager

SessionScriptManager

Session資訊

 

ABP之Javascript產生

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.