简体   繁体   English

加载后让用户使用已编译的RequireJS小部件

[英]Letting a user use a compiled RequireJS Widget after it has loaded

I'm writing a JS Widget using RequireJS. 我正在使用RequireJS编写JS Widget。 After finishing the widget I'm compiling it with r.js and Almond. 完成小部件后,我将使用r.js和Almond对其进行编译。 All goes well - but I couldn't find an easy way to let the user use the widget without using RequireJS himself - as the widget code loads async (RequireJS uses AMD). 一切顺利-但是我找不到一种简单的方法让用户不用自己使用RequireJS就使用小部件-因为小部件代码会异步加载(RequireJS使用AMD)。

What I'm doing now is busy waiting for the widget code to load and using it only after detecting it has loaded. 我现在正在做的事情是等待窗口小部件代码加载,并仅在检测到已加载后才使用它。 This is not very user-friendly. 这不是非常用户友好。

Is there a way to let just do something like this? 有没有办法让自己做这样的事情?

var widget = new Widget();

instead of doing busy wait like: 而不是像这样忙着等待:

count = 0;

function loadWidget() {
    if (typeof Widget != 'undefined') {
        var p1 = new Widget();
        p1.render();
    } else {
        if (count > 10) {
            console.log('Failed to load the Widget');
            return false;
        }
        setTimeout(loadWidget, 50);
        count++;
    }
}

$(document).ready(function() {
    loadWidget();
});

Thanks! 谢谢!

EDIT: 编辑:

my build.js 我的build.js

({
    name: './lib/almond.js',
    out: './deploy/sdk.min.js',
    baseUrl: '.',
    optimize: 'uglify2',
    mainConfigFile: 'sdk.js',
    include: ['sdk'],
    wrap: true
})

Code on the web page (assume no other script tags on page): 网页上的代码(假设页面上没有其他脚本标签):

<script src="mywidget.js" data-main="scripts/sdk" id="mywidget"></script>

No sure if the 'data-main' is really required as the js is compiled. 不确定在编译js时是否真的需要“ data-main”。

You need to follow the instructions provided with Almond . 您需要遵循Almond随附的说明。 To summarize the essential points what is in the doc there, what you need in your build config the following configuration: 总结要点,这里的文档是什么,构建配置中需要以下配置:

wrap: {
    startFile: 'path/to/start.frag',
    endFile: 'path/to/end.frag'
}

And the start.frag should be: 并且start.frag应该是:

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define([], factory);
    } else {
        root.Widget = factory();
    }
}(this, function () {

and the end.frag : end.frag

    return require('main');
}));

The end fragment calls require in its synchronous form, which is really synchronous in this case (and not the pseudo-synchronous sugar that can be used by RequrieJS itself). 结束片段调用require采用其同步形式,在这种情况下,它实际上是同步的(而不是RequrieJS本身可以使用的伪同步糖)。

I've tried this before. 我以前尝试过 It works. 有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 表单加载后要取消异步操作吗? - Cancel an async operation after the form has loaded? 网页加载完成后,CDN上的CSS已加载 - CSS on CDN loaded after web page has finished loading 如何在加载完所有内容后将javascript添加到JBrowse - How to add javascript to JBrowse after everything has loaded 如何在执行异步任务时显示小部件,然后在 flutter 中执行后显示已完成的小部件 - How to show a widget while a asyncronous task is executing and then show a done widget after it has executed in flutter 窗口本身已加载后,如何触发加载事件? - How do I trigger a loading event after the window itself has been loaded? 如何在尚未加载的异步 Javascript 中使用函数? (作为 Google Analytics 的 ga 函数) - How to use a function within an asynchronous Javascript that has not loaded yet? (As a ga function from Google Analytics) 在用户离开页面后运行去抖动的 function - Running a debounced function after user has navigated away from page 用户允许共享和找到位置后继续操作 - Continue process after user has allowed sharing and location found Android-在用户访问PreferenceActivity后确定首选项是否已更改 - Android - Determine if a Preference has changed, after user accesses PreferenceActivity RequireJS异步加载会导致用户体验不佳(UX)? - RequireJS async loading leads to poor user experience (UX)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM