Can't find three.js after loading through Ajax

为君一笑 提交于 2019-12-13 02:09:48

问题


Been working on a system to load document-specific scripts asynchronously into a Meteor template after it's been rendered:

Template.postPage.onRendered(function(){

    var post = Template.currentData();

    if(post.libs) post.libs.forEach(function(e){
        console.log(e);
        $.getScript(e, function(data, text, code){
            console.log(text);
        }).done(threejs);

        function threejs(){
            var scene = new THREE.Scene();
        };

    });
});

This pattern works with Chartist, Chart.js, and d3 but doesn't seem to be working with three.js—all of the other libraries are available globally, but I can't seem to find the THREE object constructed by three.js anywhere.

Am I missing something obvious, or do I just need to wrap the contents of three.js in an anonymous function to initialise? If so, could someone provide an example/documents on how best to do so?

Update: I'm stumped. By switching to a CDN, THREE loads perfectly well. For the sake of self-sufficiency I'd like to host from my own server—if anyone can recommend any additional tests, I'd love to hear them.


回答1:


Finally figured this out for my own project - the way to make this work is to edit the three.min.js source file.

Removing/commenting 'use strict'; at the start of three.min.js lets it load correctly.

This seems to be related to the following feature/issue of meteor: https://github.com/meteor/meteor/issues/1380

The CDN was likely un-minified and didn't contain 'use strict;'. To be safe, 'use strict;' should probably be placed after the global THREE namespace has been declared.



来源:https://stackoverflow.com/questions/30292027/cant-find-three-js-after-loading-through-ajax

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