简体   繁体   English

如何从此特定的IIFE JS结构公开公共api?

[英]How can I expose a public api from this specific IIFE JS structure?

In AspNetZero/ABP, the default index.js for an entity looks like the following: 在AspNetZero / ABP中,实体的默认index.js如下所示:

(function () {
    $(function () {

        var _$formulationFrequenciesTable = $('#FormulationFrequenciesTable');
        var _formulationFrequenciesService = abp.services.app.formulationFrequencies;

        function additionalFilter() {
            return {
                nameFilter: // something
                prop2: // something else
            };
        }

        // more code

  });
})();

We are using Telerik's AspNetCore Kendo on the Razor, so we define the grid like so: 我们在Razor上使用Telerik的AspNetCore Kendo,因此我们如下定义网格:

Html.Kendo().Grid<PatientManagement.Formulations.Dtos.GetFormulationFrequencyForViewDto>()
.Name("gridFormulationFrequencies")
.DataSource(d =>
{
    d.Custom()
     .Type("aspnetmvc-ajax")
     .ServerFiltering(true)
     .ServerPaging(true)
     .ServerSorting(true)
     .Schema(s => s.Aggregates("aggregateResults").Data("data").Errors("errors").Total("total").Model(model => model.Id(m => m.Id)))
     .Transport(t => t.Read(read => read.Action("FormulationFrequency_Read", "FormulationFrequencies", new { area = "App" }).Data("additionalData").Type(HttpVerbs.Get)));
})
.Deferred(true).Render();

How can I make additionalData "public" so that I can use it in the grid definition? 如何使AdditionalData为“ public”,以便可以在网格定义中使用它? I understand that the first line of the js is IIFE, and the second is short hand js for jQuery(document).ready() . 我知道js的第一行是IIFE,第二行是jQuery(document).ready()简写js。

I've been having issues trying to define a public API because everything is defined within the scope of the document ready, and IIFE examples I've seen don't include this curveball. 我一直在尝试定义公共API时遇到问题,因为所有内容都已在准备好文档的范围内定义,并且我所见过的IIFE示例不包含此曲线。

One way would be to assign a reference to the function to a window property. 一种方法是将对函数的引用分配给window属性。

If you have to do this often you could have a global object of your own in window namespace and assign as a property to that object instead 如果必须经常执行此操作,则可以在窗口名称空间中拥有自己的全局对象,然后将其分配为该对象的属性

(function () {
    $(function () {

        var _$formulationFrequenciesTable = $('#FormulationFrequenciesTable');
        var _formulationFrequenciesService = abp.services.app.formulationFrequencies;

        function additionalFilter() {
            return {
                nameFilter: // something
                prop2: // something else
            };
        }

        // add to global window namespace
        window.additionalFilter = additionalFilter;


        // more code

  });
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM