从 HTML 元素(例如 onclick 处理程序)调用 RequireJs 模块中的方法 [英] Calling methods in RequireJs modules from HTML elements such as onclick handlers

查看:28
本文介绍了从 HTML 元素(例如 onclick 处理程序)调用 RequireJs 模块中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将项目从旧"浏览器样式的模块结构更改为新"浏览器--server-side-javascript 模块结构require.js.

I'm changing a project from an "old" browser-style module structure to a "new" browser-or-server-side-javascript module structure with require.js.

在客户端上,我使用的是异地托管 jQuery,因此我从他们在 "使用优先级配置" README 技术:

On the client I'm using an offsite hosted jQuery, so I started from the example they give in the "use priority config" technique of the README:

<title>My Page</title>
<script src="scripts/require.js"></script>
<script>
require({
    baseUrl: 'scripts',
    paths: {
        jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min',
        jqueryui: ...,
        ...
        ... // bunch more paths here
    },
    priority: ['jquery']
}, [ 'main' ]);
</script>

这实际上工作正常.但我想将主要功能导出到 HTML 网页本身.例如:

This is actually working all right. But I'd like to export functionality from main to the HTML webpage itself. For instance:

<a class="button" href="#" onclick="MyApi.foo();">
    <img src="foo.png" alt="foo" />Click for: <b>Foo!</b>
</a>

在适应 AMD 模块模式之前,我通过在全局空间中创建字典对象来公开各种文件中的功能:

Before fitting into the AMD module pattern, I'd exposed functionality from my various files by creating a dictionary object in the global space:

// main.js

var MyApi = {};

jQuery(document).ready(function($) {
    // ...unexported code goes here...

    // export function via MyApi
    MyApi.foo = function() {
        alert("Foo!");
    };
});

但我不知道 require.js 中的正确方法是什么.是否可以在

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆