Loading Sequence problems encountered by jquery. artdialog in requirejs

Source: Internet
Author: User

Jquery. artdialog is estimated to not support amd specifications. Therefore, when using the modular methods of requirejs and AMD, there is no problem at the beginning. Later, in the slow network environment, it was found that jquery was not fully loaded. artdialog has been loaded and an error is reported, causing you to refresh the page.

The solution provided by requirejs is called Shim, see: http://requirejs.org/docs/api.html#config-shim

I tried it multiple times and figured out how to do it:

1. a js file will be loaded on each HTML page, and require. config will be called in this JS file. All the configurations will be written in it, including Shim

For example, the login.html page contains the loginmodule. js file:

    <script data-main="../script/app/loginModule" src="../script/thirdParty/require.js"></script>

Loginmodule. js contains a configuration and a module definition:

/*global window, document */require.config({paths: {"jquery": "../thirdParty/jquery-1.8.0.min","ajaxUtility": "./ajaxUtility","jquery.artDialog": "../../plugin/artDialog4.1.6/jquery.artDialog","jqueryTool": "./jqueryTool","login": "./login"},shim: {"jquery.artDialog": {deps: ["jquery"],exports: "artDialog"}}});define("loginModule", ["jquery", "ajaxUtility", "dialog", "jqueryTool", "login"], function ($, ajaxUtility, dialog, jqueryTool, login) {'use strict';var locale = window.locale;ajaxUtility.init(locale);$(document).ready(function () {login.init(ajaxUtility, dialog, jqueryTool, locale);});});

Note that my loginmodule is a module dependent on the dialog module.

The dialog module is another module defined by myself. It depends on the jquery. artdialog module.

The jquery. artdialog module demonstrates the dependency on jquery in shim.

Now let's take a look at the module dialog. js that depends on jquery. artdialog. The following array shows the dependency, and the parameter uses the variable name exported from exports in shim.

Define ("dialog", ["jquery", "jquery. artdialog "], function ($, artdialog) {'use strict '; return {canceltext:" ", locale:" ", oktext:" ", titletext :"", // language shoshould be either 'cn' or 'en' init: function (locale) {This. locale = locale; If (locale = "cn") {This. canceltext = "cancel"; this. oktext = "OK"; this. errortitletext = "error"; this. oktitletext = "info";} else {This. canceltext = "cancel"; this. oktext = "OK"; this. errortitletext = "error"; this. oktitletext = "info" ;}}, error: function (Message) {$. dialog ({Title: This. errortitletext, icon: "error", content: Message, okval: This. canceltext, OK: function () {This. close () ;}) ;}, done: function (Message) {$. dialog ({Title: This. oktitletext, icon: "OK", content: Message, okval: This. oktext, OK: function () {This. close ();}});}};});

It takes patience to make good use of requirejs. You only need to repeat the experiment based on your actual situation by reading the document.

By the way, you only need to add require. config... To the JS file directly referenced on the top level, that is, the HTML page. Other JS files are not needed.

If there are multiple pages, it is clear that you need multiple such top level files. Someone is called Bootstrap and the name is also suitable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.