0

我正在使用 reuqireJS 并且正在努力调用一个函数,该函数位于我需要的 js 文件中。我的主要app.js“控制器”需要(plugin)app.js,它处理所有插件配置和插件相关功能。

这是从 app.js

define([], function(){
    var start = function() {
        require(['jquery', 'overrides', 'jqm', 'multiview', 'respond'],function() {

            // globals
            var 
            // PROBLEM attempt at an external plugin function object
            dataTablesExt = {},
            ...;

            // call for (plugin)app.js
            enhanceDataTables = 
                function( page, from ) {
                    var datatable = page.find('.table-wrapper table');                  
                    if ( datatable.length > 0 && datatable.jqmData('bound') != true ) {
                        datatable.not(':jqmData(bound="true")')
                            .each( function() {
                                var that = $(this),
                                tblstyle = that.jqmData("table-style");

                                that.jqmData('bound', true);
                                require(['services/datatables/app'], function (App) {
                                // this calls (plugin)app.js
                                App.render({style: tblstyle, table: that });
                                });
                            });
                        }
                }; 

        // PROBLEM - try to call function "Hello" inside datatables.app
       anotherFunc= 
                function( page, from ) {
                    dataTablesExt.sayHello("john");
                };

我想我的问题是如何设置全局变量dataTablesExt,所以我可以用全局调用的函数“填充”它。这是我在里面尝试的(plugin)app.js

define(['services/datatables/app', 'services/datatables/datatables.min'], function( app, datatables ) {
    function render(parameters) {
    ...
    // the function I want to call
    function helloName( name ){
        alert( name );
        };

    // I'm trying to add this function to the global "dataTablesExt"
    dataTablesExt.sayHello = helloName; 
    }

但是……不行。我总是得到:

  dataTablesExt.sayHello is not a function

问题:
有人可以指出我做错了什么吗?如果这是不可能的,那将是一个替代方案。

我想触发一个自定义事件,但我必须设置一个对象来传递事件,我不知道该怎么做。

感谢帮助!

4

1 回答 1

0

知道了。我需要将dataTablesExt附加到我正在使用的全局变量中,而不是将其声明为变量。所以像这样:

 // globals
        var 
        ...;

        $.dataTablesExt = {};

然后我可以为它分配功能并调用它们。

于 2012-07-30T07:12:29.170 回答