简体   繁体   English

我应该如何编写我的定义以使用curl.js?

[英]how should I write my define to work with curl.js?

I'm reading Addy Osmani's excellent blog post about writing AMD modules. 我正在阅读Addy Osmani关于撰写AMD模块的出色博客文章 I start with a simple chunk of js that I lifted from his post: 我从他的帖子中摘录了一个简单的js块:

define('modTest', [],
  // module definition function
  function () {
      // return a value that defines the module export
      // (i.e the functionality we want to expose for consumption)

      // create your module here
      var myModule = {
        doStuff:function(){
          console.log('Yay! Stuff');
        }
      }

      return myModule;
  }
);

I took out the dependencies on foo and bar . 我取出了对foobar的依赖。 Just want a simple object that logs to the console. 只需要一个简单的对象登录到控制台。

So I save that in /js/modTest.js and then try to load it: 所以我将其保存在/js/modTest.js ,然后尝试加载它:

curl(['/js/modTest.js'])
 .then(function(mt) {
     console.log("Load complete"); 
     console.log("mt:"); 
     console.log(mt); 
     mt.doStuff()
  }, function(ex) {alert(ex.message);})

Result: error: Multiple anonymous defines in URL . 结果:错误: Multiple anonymous defines in URL OK that didn't work. 好,那没用。 Tried adding in a namespace: define('myCompany/modTest', [], , same result. Tried adding an empty string in the dependency array, same result. 尝试添加名称空间: define('myCompany/modTest', [], ,,相同结果。尝试在依赖项数组中添加空字符串,结果相同。

Also tried curl(['modTest.js'], function(dep){console.log(dep)}); 还尝试了curl(['modTest.js'], function(dep){console.log(dep)}); with the same result. 结果相同。

Is the code in Addy's blog post incorrect? Addy博客文章中的代码不正确吗? Am I doing something wrong? 难道我做错了什么? Maybe a bug in curl? 也许是卷曲的错误?

Update 5/24 : I ditched curl.js in favor of require.js. 更新5/24 :我放弃了curl.js,而使用了require.js。 Zero odd errors, very little work to change over. 零奇数错误,转换工作很少。 I did have to deal with amdefine a bit to get my code running client and server side (one object is in both places, so grunt had to be configured to take care of that). 我确实需要处理amdefine才能使我的代码在客户端和服务器端运行(两个地方都有一个对象,因此必须配置grunt来解决这一问题)。 My defines generally look like: 我的定义通常如下所示:

define(->
    class AlphaBravo 
    ... 

And never have any trouble loading. 而且加载也没有任何麻烦。

You asked curl() to fetch a module called "/js/modTest.js". 您要求curl()获取名为“ /js/modTest.js”的模块。 It found the file and loaded it and found a module named "modTest", so it complained. 它找到文件并加载它,并找到了一个名为“ modTest”的模块,因此它抱怨。 :) (That error message is horribly wrong, though!) :)(但是,该错误消息非常错误!)

Here's how you can fix it (pick one): 解决方法如下(选择一个):

1) Remove the ID from your define(). 1)从您的define()中删除ID。 The ID is not recommended. 不建议使用ID。 It's typically only used by AMD build tools and when declaring modules inside test harnesses. 通常仅在AMD构建工具中以及在测试工具中声明模块时使用。

2) Refer to the module by the ID you gave it in the define(). 2)通过在define()中给它的ID来引用该模块。 (Again, the ID is not recommended in most cases.) (同样,在大多数情况下,不建议使用ID。)

curl(['modTest'], doSomething);

3) Map a package (or a path) to the folder with your application's modules. 3)将包(或路径)映射到包含应用程序模块的文件夹。 It's not clear to me what that would be from your example since modTest appears to be a stand-alone module. 我不清楚您的示例中会有什么,因为modTest似乎是一个独立的模块。 However, if you were to decide to organize your app's files under an "app" package, you packages config might look like this: 但是,如果您决定将应用程序的文件组织在“ app”软件包下,则您的config可能如下所示:

packages: [ { name: 'app', location: 'app' } ]

Then, when you have code that relies on the modTest module, you can get to it via an ID of "app/modTest". 然后,当您有依赖于modTest模块的代码时,可以通过ID“ app / modTest”来获取它。

curl(['app/modTest'], doSomething);

I hope that helps clear things up! 我希望这有助于清理问题!

Fwiw, Addy's example could actually work with the right configuration, but I don't see any configuration in that post (or my eyes missed it). Fwiw,Addy的示例实际上可以使用正确的配置,但是我在那篇文章中没有看到任何配置(或者我没看过)。 Something like this might work: 这样的事情可能会起作用:

packages: [ { name: 'app', location: '.' } ]

-- John - 约翰

I've just had a similar problem which turned out to be the include order I was using for my other libraries. 我有一个类似的问题,原来是我在其他库中使用的包含顺序。 I'm loading handlebars.js, crossroads.js, jquery and a few other libraries into my project in the traditional way (script tags in head) and found that when I place the curl.js include first, I get this error, but when I include it last, I do not get this error. 我以传统方式(在头中的脚本标签)将handlebars.js,crossroads.js,jquery和其他一些库加载到我的项目中,发现当我首先放置curl.js时,会出现此错误,但是最后添加它时,不会出现此错误。

My head tag now looks like this: 我的头部标签现在看起来像这样:

<script type="text/javascript" src="/js/lib/jquery.js"></script>
<script type="text/javascript" src="/js/lib/signals.js"></script>
<script type="text/javascript" src="/js/lib/crossroads.js"></script>
<script type="text/javascript" src="/js/lib/handlebars.js"></script>
<script type="text/javascript" src="/js/lib/curl.js"></script>
<script type="text/javascript" src="/js/main.js"></script>

You have a problem with your define call. 您的define调用有问题。 It is NAMED 它被命名

See AMD spec for full story on how to write defines, but here is what I would expect to see in your js/modTest.js file: 有关如何编写定义的完整信息,请参见AMD规范 ,但这是我希望在js/modTest.js文件中看到的js/modTest.js

define(/* this is where the difference is */ function () {
      // return a value that defines the module export
      // (i.e the functionality we want to expose for consumption)

      // create your module here
      var myModule = {
        doStuff:function(){
          console.log('Yay! Stuff');
        }
      }

      return myModule;
  }
);

Now, the boring explanation: 现在,无聊的解释:

CurlJS is awesome. CurlJS很棒。 In fact, after dealing with both, RequireJS and CurlJS, I would say CurlJS is awesome-er than RequireJS in one category - reliability of script execution ordering. 实际上,在处理完RequireJS和CurlJS两者之后,我会说CurlJS在一个类别中比RequireJS更加出色-脚本执行顺序的可靠性。 So you are on the right track. 因此,您处于正确的轨道上。

On of the major things that are different about CurlJS is that it uses "find at least one anonymous define per loaded module, else it's error" logic. 关于CurlJS的主要不同之处在于,它使用“每个加载的模块至少找到一个匿名定义,否则就是错误”的逻辑。 RequireJS uses a timeout, where it effectively ignores cases where nothing was defined in a given file, but blows up on caught loading / parsing errors. RequireJS使用超时,它可以有效地忽略给定文件中未定义任何内容的情况,但是会因捕获的加载/解析错误而崩溃。

That difference is what is getting you here. 区别就是让您来这里的原因。 CurlJS expects at least one anonymous (as in NOT-named) define per loaded module. CurlJS期望每个加载的模块至少有一个匿名 (如未命名)定义。 It still handles named defines fine, as expected. 仍按预期处理命名定义很好的句柄。 The second you move the contents of "js/modTest.js" into inline code, you will have to "name" the define. 第二步,将“ js / modTest.js”的内容移动到内联代码中,您将不得不为定义“命名”。 But, that's another story. 但是,那是另一个故事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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