从 HTML 元素(例如 onclick 处理程序)调用 RequireJs 模块中的方法 [英] Calling methods in RequireJs modules from HTML elements such as onclick handlers
问题描述
我正在将项目从旧"浏览器样式的模块结构更改为新"浏览器-或-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 中的正确方法是什么.是否可以在 标签内放入更多的
require
语句,然后命名模块以便它可以在网页中使用?或者这应该总是在 main.js 内部动态完成,比如 $('#foobutton').click(...)
?
But I don't know what the right approach in require.js is. Is it okay to put in the HTML more require
statements inside of <script>
tags, and then name modules so that it can be used from within the webpage? Or should this always be done dynamically inside of main.js, like $('#foobutton').click(...)
?
推荐答案
使用 AMD 的一个好处是不再需要命名空间.尝试使用 RequireJS 再次创建它们将违背 AMD 提倡的模式.
One benefit from using AMD is to drop the need for namespaces. Trying to create them again with RequireJS will go against the patterns AMD promotes.
关于使用 main.js
,只有当你有一个单页应用程序并且你的所有代码都从一个地方引用时,这才是真正合适的.您可以根据需要随意调用其他模块来要求和加载其他模块.
With regard to using main.js
, this is only really appropriate when you have a single page app and all your code be reference from one place. You're free to make additional calls to require and load other modules as you need them.
使用上面的示例,您可以这样处理:
Using your example from above, you could approach it this way:
foo.js
define(['jquery'], function($) {
// Some set up
// code here
// Return module with methods
return {
bar: function() {
}
}
});
page.js
require(['foo'], function(foo) {
// jQuery loaded by foo module so free to use it
$('.button').on('click', function(e) {
foo.bar();
e.preventDefault();
});
});
然后在您的页面中使用 require 请求 page.js 文件.
Then in your page request the page.js file with require.
使用上面的示例:
require({
// config stuff here
}, [ 'page' ]);
或者在页面下方加载它:
Or loading it in the page further down:
<script>
require(['page']);
</script>
补充几点
使用上面的模式,page.js 可以很容易地需要许多其他加载其他页面相关功能的模块.
Using the pattern above, page.js could easily require many other modules to load other page related functionality.
在将成员附加到全局命名空间之前,您现在将该代码拆分为可以重复使用的单独模块任何页面.所有这些都不依赖全局对象.
Where before you would attach members to your global namespace, you now split that code into separate modules that can be re-used on any page. All without reliance on a global object.
使用 require 这种方式将事件附加到 DOM 元素将很可能依赖RequireJS提供的DOM Ready模块
Using require this way to attach events to your DOM elements will most likely rely on the DOM Ready module provided by RequireJS
这篇关于从 HTML 元素(例如 onclick 处理程序)调用 RequireJs 模块中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!