2013-08-20 44 views
2

http://plnkr.co/edit/bSm4nYJ0TmUqylidxkqt?p=preview广东话使用requirejs

我试图用requirejs注入到ACE编辑器的JavaScript库的引用到ACE的参考。 嵌套ace引用的对象返回就好,但该对象的ace属性未定义。我在设置中缺少什么?

代码如下。

我使用define返回一个对象。 define函数声明了对本地ace库的依赖(控制台显示加载的ace.js资源)。

define(["./libs/ace/ace"],function(ace){ 
return { 
     ref: ace, 
     msg: "alive" 
    } 

});对于requirejs

我的数据,主要针对的是我的script.js文件,该文件是如下...

require(["js/testace"],function(testace){console.log(testace);}); 

我的文件结构如下...

enter image description here

。 ..和我的控制台输出如下

对象{ref:undefined,msg:“alive”}

回答

3

我通过使用此代码得到它: 替换/static/ace,与lib/ace

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<title>ACE in Action</title> 
<style type="text/css" media="screen"> 
    #editor { 
     position: absolute; 
     top: 0; 
     right: 0; 
     bottom: 0; 
     left: 0; 
    } 
</style> 
</head> 
<body> 

<div id="editor"> 
    function foo(items) { 
    var x = "All this is syntax highlighted"; 
    return x; 
    } 
</div> 
<script type="text/javascript" src="/static/require.js"></script> 
<script> 
    require.config({ 
     baseUrl: window.location.protocol + "//" + window.location.host 
      + window.location.pathname.split("/").slice(0, -1).join("/"), 

     paths: { 
      ace: "/static/ace" 
     } 
    }); 

    require(["ace/ace"], function (ace) { 
     var editor = ace.edit("editor"); 
     editor.setTheme("ace/theme/monokai"); 
     editor.getSession().setMode("ace/mode/javascript"); 
    }); 
</script> 
</body> 
</html> 

来源:https://github.com/ajaxorg/ace/issues/1017

结帐这个页面,如果你得到一些疯狂的错误:http://requirejs.org/docs/errors.html

这里本来回答:How to load ace editor