简体   繁体   中英

Using whenjs with requirejs

I am unable to use requirejs with whenjs, it gives 404 errors about missing files when running the site. I'm following the instructions in the whenjs README :

Firstly, I run git submodule add https://github.com/cujojs/when when in the project root dir.

Then I do "Configure your loader with a package" in app/public/js/main.js :

requirejs.config({
  baseUrl: "js",
  packages: [
    {
      name: "when",
      path: "../../../when",
      main: "when"
    }
  ]
});

This leaves the following directory layout:

project-root/
  app/
    public/
      js/
        main.js
        helpers/
          myhelper.js
  when/

The file ( myhelper.js ) that requires whenjs has:

define(['when'], function(When) {

This produces the error

GET http://localhost:4580/js/when/when.js 404 (Not Found)

I tried copying the js files from project-root/when into app/public/js/libs/when

project/
  app/
    public/
      js/
        main.js
        helpers/
          myhelper.js
        libs/
          when/

and using the following code:

//main.js
requirejs.config({
  baseUrl: "js",
  packages: [
    {
      name: "when",
      path: "libs/when",
      main: "when"
    }
  ]
});

//myhelper.js
define(['when'], function(When) {

Produces the same error:

GET http://localhost:4580/js/when/when.js 404 (Not Found)

It appears that whatever is in myhelper.js is ignoring the packages declaration - I'm not sure if there's something else I'm supposed to do there? The whenjs instructions don't say to.

I tried this:

//main.js
requirejs.config({
  baseUrl: "js",
  packages: [
    {
      name: "when",
      path: "libs/when",
      main: "when"
    }
  ],
  paths: {
    w: "./libs/when"
  }
});

//myhelper.js
define(['w/when'], function(When) {

This doesn't produce and error - although I haven't tried using the library with it yet…

The version of Requirejs is v2.1.8, and I've read the API instructions regarding packages but I'm none the wiser . What is it that means the whenjs instructions don't work here? I've tried other combinations of settings too, like combining the paths and packages options but to no avail.

This is not a Node or javascript only project, so a drastic movement of directories is not an option.

Any help is much appreciated.

Try using the paths property of requirejs.config : If you put when.js under js/libs/when/when.js , you can try:

    requirejs.config({
        paths: {
            'when': 'libs/when/when'
        }
    });
define(['when'], function(When) {...

You don't even need the baseUrl , if your JavaScript tag contains it, but you can specify it if you want.

This should solve the library load issue, but if when.js does not support AMD, you can add a shim to the configuration:

shim: {
    'when': {
        exports: 'When'
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM