Modernizr load [A, B] seems to execute B before A, how is this possible?

此生再无相见时 提交于 2019-12-11 02:16:03

问题


Modernizr.load({ both: [a, b] }) seems to executes b before a but that's not how Modernizr is supposed to work? "yepnope.js [used by Modernizr] always executes things in the order they are listed"

Modernizr versions 2.5.3 and 2.6.2.

I'm loading angular.js and angular-sanitize.js like so:

Modernizr.load({
  both: [
    cdnDir + 'angular.js',
    cdnDir + 'angular-sanitize.js',
    d.i.assetsUrlPathStart + 'debiki-dashbar.js'],
  complete: bootstrapAngular
})

However, infrequently, angular-sanitize.js dies because angular does not yet exist.

But isn't Modernizr.load(both: [a, b, c]) guaranteed to execute a, b, c in order? What is happening...?

Details:

The error happens on an angular.extend(...) line in angular-sanitize.js, the last line in this excerpt: (line 148)

// Elements that you can, intentionally, leave open (and which close themselves)
// http://dev.w3.org/html5/spec/Overview.html#optional-tags
var optionalEndTagBlockElements = makeMap("colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr"),
    optionalEndTagInlineElements = makeMap("rp,rt"),
    optionalEndTagElements = angular.extend({}, optionalEndTagInlineElements, optionalEndTagBlockElements);

Here's the error message:

Uncaught TypeError: Cannot call method 'extend' of undefined ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular-sanitize.js:148

and Chrome's debugger shows that window.angular is indeed undefined — althought it's being used on the very last line in angular.js (which is supposed to be executed first of all).

Chrome (version 24.0.1312.57) says that both angular.js and angualar-sanitize.js are loaded from the browser's cache.


Update: This more explicit rewrite really should work I think. But when run, it sometimes prints "Angular absent" and dies later on (when angular-sanitize.js runs)— although Angular "must" just have been created.

Modernizr.load({
  load: cdnDir + 'angular.js', // <-- creates `angular`
  complete: function(){
    if (typeof angular == 'undefined' || angular === null) {
      console.log('Angular absent.');  // <-- printed sometimes, and everything fails
    }
    Modernizr.load({
      load: cdnDir + 'angular-sanitize.js',
      complete: ...
      }
    });
  }
});

Someone marked this question as a duplicate of: Can modernizr load scripts asychronously but execute them in order? — however, if you read this question carefully, you'll notice that this question itself actually already includes the answer to that other question (namely this: "[...] always executes things in the order they are listed" — that's the answer to the other question). Instead, this question, is about why things does not work in the way they should, according to the docs.

来源:https://stackoverflow.com/questions/14787619/modernizr-load-a-b-seems-to-execute-b-before-a-how-is-this-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!