问题描述
我有一个很好的工作程序与Requirejs和Backbonejs写作,但它确实有时...放缓例如,当谈到做一些运算的工作!我试图使用Web工人做这样该算法的工作:
I have a well working app writing with Requirejs and Backbonejs, but it's really slowing sometimes... For example when it comes to make some arithmetic work! I tried to use a Web Worker to do this arithmetic work like this :
我的模块(traffic.js):
define(["jquery", "use!underscore", "use!backbone", "namespace" ],
function ($, _, Backbone, globals) {
.....
var worker = new Worker("arithmetic.js");
worker.addEventListener('message', function(e) {
console.log(e.data);
}, false);
worker.postMessage(globals.data); // Send data to our worker.
});
arithmetic.js:
define(["use!underscore", "use!backbone" ],
function ($, _) {
//Here die Operations
});
但我有错误的定义没有定义!
我试了一下像的太多,但没有成功!
I tried it like this too but no success!!
如何使用Web工作到requirejs或backbonejs ??
How to use Web Worker into requirejs or with backbonejs??
谢谢!
推荐答案
您可以使用requireJS从网络工作者:请参见获取更多信息。
You can use requireJS from web workers: see the API docs for more info.
唯一的要求是在网络工作者开始与 importScripts(...)来加载requireJS
。一旦被加载,可以使用定义
并使用requireJS正常人一样。
The only requirement is to load requireJS at the start of the web worker with importScripts(…)
. Once that is loaded, you can use define
and use requireJS like normal.
对我来说,关键部件时,我就得到工作是确保您还加载相同的引导配置(例如 config.js
或<$ C在您使用在应用其余部分的网络工作者$ C> main.js )。这是商务部在谈论当它说:
The key part for me when I was getting it to work was to make sure that you are also loading the same bootstrap configuration (e.g. config.js
or main.js
) in the web worker that you are using in the rest of your app. This is what the doc is talking about when it says:
您可能需要在配置的baseUrl选项设置,以确保需要()可以找到脚本加载。
另一件事是,你可以从装载工人的 traffic.js
与模块ID(而不是硬编码的脚本路径)使用的
Another thing is that you can load the worker from your traffic.js
file with a module ID (instead of hardcoding the script path) utilizing this requireJS plugin.
这篇关于如何使用网络工作者成模块构建与Requirejs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!