如何使 javascript 模块模式与 ajax 一起工作?

发布于 2024-10-22 07:12:19 字数 2865 浏览 4 评论 0原文

我正在尝试转换一些 javascript/jquery 代码以能够处理 ajax 选项卡。

当页面加载时,我会加载所有脚本,并使用 jquery live 和 livequery 插件来帮助绑定。

每次都在部分视图(asp.net mvc 3)中,单击时转到控制器操作并呈现部分视图并将其粘贴在选项卡中。

所以 live 和 livequery 解决了除了这一问题之外的大部分问题。我正在使用 jquery datatables.net 插件,一旦它呈现,我将其存储在变量中并在地方使用它。

问题是因为我使用的是模块模式,所以代码在表格渲染之前就运行了很长时间。所以存储对象的变量是“未定义的”。我尝试用 jquery livequery 语句包装它,这使得表运行,但变量仍然是“未定义”。

我的猜测是,这与 C# 不同,它会更新引用。所以我认为它在我的全局变量(模块模式的变量)中并且永远不会更新。

var tab = (function (my, $)
{
    var myTable = my.dataTable = my.dataTable || {};

    myTable.oDataTable = {};


    myTable.init = function ()
    {
        myTable.oDataTable = _fnSetupTable();
        _fnCreateDateFilters();
    };



    myTable.delete= function (rowToDelete)
    {
        // Need to get the position of the row. Need to use index
        var pos = this.oDataTable.fnGetPosition(rowToDelete[0]);

        /* delete row from table - first param is index of row */
        this.oDataTable.fnDeleteRow(pos, null, true);
    };

    function _fnSetupTable()
    {

        /* Initialize datatable object */

        $('#table').livequery(function ()
        {
            // oDataTable gets used in alot of places later on but since it undefined
            // it will crash when used.
            var oDataTable = $(this).dataTable(
            {
                "bJQueryUI": true,
                "bFilter": true,
                "bAutoWidth": false,
                "sPaginationType": "full_numbers",
                "aaSorting": [[3, 'asc']],  //Default sorting on datetime
                "oLanguage":
                    {
                        "sZeroRecords": "No Records"
                    },
                "aoColumns":
                    [
                        { "bSortable": true, "bSearchable": false }, 
                        {"bSortable": true, "bSearchable": false }, 
                        {"bSortable": false, "bSearchable": false }, 
                        {"bSortable": false, "bSearchable": false }, 
                        {"iDataSort": 3 },
                        { "bSortable": false, "bSearchable": true },  
                        {"bSortable": false, "bSearchable": false },      
                        {"bSortable": false, "bSearchable": false}  
                    ]
            });

            return oDataTable;
        });

    }

    return my;

} (tab || {}, jQuery));

我经常使用 oDataTable。正如您所看到的,如果我会调用 myTable.Delete 我在那里使用它 oDataTable 但它会“未定义”并崩溃。

我有另一个模块类来启动整个序列(因此使上面的代码运行)

/* 
*   Document Ready
*/
$(function ()
{

    /* Initializes all plugins and events */
    tab.init();

});


var tab = (function (my, $)
{

    my.init = function ()
    {
        tab.preload();
        tab.dataTable.init();
        $('#tabs').tabs();
    };



return my;

} (tab || {}, jQuery));

I am trying to convert some javascript/jquery code to be able to handle ajax tabs.

I have all my scripts load up when the pages loads up and I use jquery live and livequery plugin to help with the binding.

Each time is in a partial view(asp.net mvc 3) and when clicked goes to a controller action and renders the partial view and sticks it in the tab.

So the live and livequery are solving most of the problems except for this one problem. I am using the jquery datatables.net plugin and once it renders that I store it in a variable and use it places.

The thing is since I am using the module pattern the code runs long before the table is even rendered. So the variable storing the object is "undefined". I tried to wrap it around with jquery livequery statement and that makes the table run but the variable is still "undefined".

My guess is that is not like C# were it would update the reference. So I am thinking that it is in my global variable(the one for the module pattern) and that never gets updated.

var tab = (function (my, $)
{
    var myTable = my.dataTable = my.dataTable || {};

    myTable.oDataTable = {};


    myTable.init = function ()
    {
        myTable.oDataTable = _fnSetupTable();
        _fnCreateDateFilters();
    };



    myTable.delete= function (rowToDelete)
    {
        // Need to get the position of the row. Need to use index
        var pos = this.oDataTable.fnGetPosition(rowToDelete[0]);

        /* delete row from table - first param is index of row */
        this.oDataTable.fnDeleteRow(pos, null, true);
    };

    function _fnSetupTable()
    {

        /* Initialize datatable object */

        $('#table').livequery(function ()
        {
            // oDataTable gets used in alot of places later on but since it undefined
            // it will crash when used.
            var oDataTable = $(this).dataTable(
            {
                "bJQueryUI": true,
                "bFilter": true,
                "bAutoWidth": false,
                "sPaginationType": "full_numbers",
                "aaSorting": [[3, 'asc']],  //Default sorting on datetime
                "oLanguage":
                    {
                        "sZeroRecords": "No Records"
                    },
                "aoColumns":
                    [
                        { "bSortable": true, "bSearchable": false }, 
                        {"bSortable": true, "bSearchable": false }, 
                        {"bSortable": false, "bSearchable": false }, 
                        {"bSortable": false, "bSearchable": false }, 
                        {"iDataSort": 3 },
                        { "bSortable": false, "bSearchable": true },  
                        {"bSortable": false, "bSearchable": false },      
                        {"bSortable": false, "bSearchable": false}  
                    ]
            });

            return oDataTable;
        });

    }

    return my;

} (tab || {}, jQuery));

I use oDataTable quite a bit. As you can see if I would call up myTable.Delete I use it oDataTable there but it will be "undefined" and crash.

I have another module class that starts the whole sequence(so makes the above code run)

/* 
*   Document Ready
*/
$(function ()
{

    /* Initializes all plugins and events */
    tab.init();

});


var tab = (function (my, $)
{

    my.init = function ()
    {
        tab.preload();
        tab.dataTable.init();
        $('#tabs').tabs();
    };



return my;

} (tab || {}, jQuery));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-10-29 07:12:19

您似乎正在调用许多东西oDataTable,并且它们都以不兼容的方式分配/访问。

我看到的第一个问题是,您尝试从 _fnSetupTable 返回的 oDataTable 值实际上是从 inner 函数返回的,因此以下赋值不执行任何操作:

myTable.oDataTable = _fnSetupTable();

您尝试使用 _fnSetupTable 的结果,但 _fnSetupTable 实际上不会返回任何内容,其内部函数会返回任何内容。

也许更大的问题是如何使用模块模式。我将尝试创建一个小示例来演示该问题:

var input = {
    dataTable: "tab table"
}

var tab = (function (my, $)
{
    // this var is local
    var myTable = my.dataTable = my.dataTable || {};

    // creating a property on a local variable
    myTable.oDataTable = {};

    // this is what's returned, but it has no access to myTable
    return my;

} (input));

console.log(tab) // does not have an oTableData property

如您的示例中所示,创建了一个名为 myTable 的局部变量并给出了表属性,但返回的是 my >,而不是 myTable。如果您希望可以从返回的 my 对象访问这些属性,则需要将它们分配给 my。或者也许您想返回myTable

There seem to be a number of things you're calling oDataTable and they are all assigned/accessed in incompatible ways.

The first problem I see is that the oDataTable value you attempt to return from _fnSetupTable is actually scoped to and returned from the inner function, so the following assignment does nothing:

myTable.oDataTable = _fnSetupTable();

You're trying to use the result of _fnSetupTable but _fnSetupTable doesn't actually return anything, its inner function does.

Perhaps a larger problem is how you're using the module pattern. I'll try to create a small example to demonstrate the problem:

var input = {
    dataTable: "tab table"
}

var tab = (function (my, $)
{
    // this var is local
    var myTable = my.dataTable = my.dataTable || {};

    // creating a property on a local variable
    myTable.oDataTable = {};

    // this is what's returned, but it has no access to myTable
    return my;

} (input));

console.log(tab) // does not have an oTableData property

As in your example a local variable called myTable is created and given the table properties, but what's being returned is my, not myTable. If you want those properties to be accessible from the my object that's returned, then you need to assign them to my. Or maybe you meant to return myTable?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文