简体   繁体   English

通过JavaScript类调用javaScript函数

[英]Calling javaScript function by a JavaScript class

I am a newbie in the ASP.NET technology and also I did not find any answer on the forum (however if there is one please let me know) 我是ASP.NET技术的新手,也没有在论坛上找到任何答案(但是,如果有答案,请告诉我)

I am writing ASP.NET MVC app and I use a lot of Ajax there. 我正在编写ASP.NET MVC应用程序,并且在那里大量使用Ajax。 At the moment I am rewriting "free" Javascript into classes and their methods, so the code will be more readable. 目前,我正在将“免费” Javascript重写为类及其方法,因此代码将更具可读性。 And here is the problem: 这是问题所在:

For such a case: 对于这种情况:

@Ajax.ActionLink("Text", "SomeAction", "Controller", new AjaxOptions() { UpdateTargetId="content", HttpMethod="Post", OnComplete="javascriptActionStatedBelow"})

    <script>

        function javascriptActionStatedBelow() 
        {
            // do stuff here 
        }

    </script>

Everyting works perfectly fine. 一切都很好。 However when I try to input the function into the class and then call it, it does not work. 但是,当我尝试将函数输入到类中然后调用它时,它不起作用。

I am doing this in the following way: 我通过以下方式进行操作:

    <script>

    // is in seperate file and here is added reference to it
    class myClass {
        javascriptActionStatedBelow() {
            // do stuff here
        }
    }

    // this is on the view page
    var someObj = new myClass();


</script>

I initilize the object in the header and then in the body I put the following. 我在标题中初始化对象,然后在主体中放置以下内容。

@Ajax.ActionLink("Text", "SomeAction", "Controller", new AjaxOptions() { UpdateTargetId="content", HttpMethod="Post", OnComplete= "someObj.javascriptActionStatedBelow" })

The second idea does not work. 第二个想法行不通。

What am I doing wrong ? 我究竟做错了什么 ? Is there a better solution for managing javascript content in ASP.NET MVC applications while using AJAX ? 使用AJAX时,是否有更好的解决方案来管理ASP.NET MVC应用程序中的javascript内容?

I know about $.ajax type of solution but actions like OnComplete, OnBegin,.. etc worked perfectly for now. 我知道$ .ajax类型的解决方案,但是类似OnComplete,OnBegin等的操作现在可以完美地工作了。

It's important to note that there are no classes in JavaScript. 重要的是要注意,JavaScript中没有classes Functions can be used to somewhat simulate classes, but in general JavaScript is a class-less language. 函数可以用来在某种程度上模拟类,但是通常JavaScript是一种无类语言。 However from ECMAScript 2015, classes are introduced which are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. 但是,从ECMAScript 2015开始,引入了类,这些类主要是基于JavaScript的现有基于原型的继承的语法糖。 For your requirement, should defined the java script function as IIFE . 根据您的要求,应将Java脚本函数定义为IIFE If your javascript is not targeted for ECMAScript 2015 , then you can write the code as follows for IIFE: 如果您的JavaScript并非ECMAScript 2015目标,则可以为IIFE编写以下代码:

  1. Define the IIFE method as 将IIFE方法定义为

     var myClass = (function () { // Object that's returned from the IIFE. return { javascriptActionStatedBelow: function() { } }; }()); 

Now in view's script section you can use it as 现在,在视图的脚本部分中,您可以将其用作

// this is on the view page
var someObj = myClass.javascriptActionStatedBelow();

Javascript isn't strictly a “class-based” object-oriented language, hence the same patterns that apply to conventional back-end object-oriented languages don't apply there effectively. Java严格来说并不是严格的“基于类”的面向对象语言,因此,与传统的后端面向对象语言相同的模式不能在那里有效地应用。 However, there is a number of ways to make javascript much more convenient and manageable. 但是,有很多方法可以使javascript更加方便和易于管理。 One of the ways that I highly recommend is through the use of “Module / Revealing module pattern”, or simply put through making javascript “namespaces” and calling them wherever needed. 我强烈建议的一种方法是使用“模块/显示模块模式”,或者简单地通过制作javascript“命名空间”并在需要时调用它们。 This reduces the clutter in View files and mimics the design we are used to seeing. 这样可以减少View文件中的混乱情况,并模仿我们经常看到的设计。

In a separate js file (conventionally named similar to the view file), you will define your javascript as follows: 在一个单独的js文件(通常与视图文件类似的名称)中,您将按以下方式定义JavaScript:

var className = (function() {
    // private variables and functions
    var javacriptField1 = "some field"; 

    var javascriptFunction1 = function() {
        //function logic
    }

    var javascriptFunction2 = function() {
        //function logic
    }

    // public API
    return {
        javascriptField1   : javascriptField1,
        javascriptFunction1: javascriptFunction1,
        javascriptFunction2: javascriptFunction2
    };
})();

In your view you would include your js file: 在您看来,您将包括您的js文件:

<script src="~/Scripts/myfile.js"></script>

And use it within the javascript on your view as follows: 并在您视图的javascript中使用它,如下所示:

var obj = className.javascriptFunction1();

Following is a very nice tutorial you should go through: https://www.dcaric.com/blog/asp-net-mvc-and-revealing-module-pattern 以下是一个非常不错的教程,您应该阅读: https : //www.dcaric.com/blog/asp-net-mvc-and-revealing-module-pattern

Here's another example: How to use javascript namespaces correctly in a View / PartialView 这是另一个示例: 如何在View / PartialView中正确使用javascript名称空间

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

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