将 Coffeescript 类和 RequireJS(或 Curljs 或类似)用于客户端浏览器库的示例 [英] Example of using Coffeescript classes and RequireJS (or Curljs or similar) for client side browser library

查看:15
本文介绍了将 Coffeescript 类和 RequireJS(或 Curljs 或类似)用于客户端浏览器库的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想使用Coffeescript 开发一个浏览器(仅限客户端)库,特别是,除了纯函数之外,我们更倾向于使用Coffeescript 的类"功能.该库将相对较大,因此我们希望从使用定义明确的模块模式开始,但不是到我们希望每个咖啡脚本类"都有一个咖啡文件的地步.我们不想即时编译咖啡文件,而是作为特定的构建步骤,并且希望不必将所有输出的 JS 合并到一个文件中.作为最终要求,我们将使用 Jasmine 之类的东西进行测试.

We want to develop a browser (client side only) library using Coffeescript, and in particular, we tend to use the "class" capability of Coffeescript quite a bit, in addition to pure functions. The library will be relatively large, so we want to start out using a well defined module pattern, but not to the point where we want a single coffee file for every coffeescript "class". We don't want to compile the coffee files on the fly, but rather as a specific build step, and would prefer to not have to concat all the outputted JS into one file. As a final requirement, we will be using something like Jasmine for testing.

有谁知道以这种方式开发的一个很好的示例库,使用 Coffeescript 和 RequireJS、CurlJS、Browserify 等东西?我查看了 Github,有一些示例,但我看不到任何特定于我需求的内容.

Does anyone know of a good example library developed in this way, using Coffeescript with something such as RequireJS, CurlJS, Browserify etc? I have looked on Github, and there are some examples, but I couldn't see anything specific to my needs.

我尝试了 Coffee-Toaster ,因为它似乎有希望让它变得简单定义依赖等,但它无法处理 Windows 路径(旧的 vs/),所以放弃了,主要是因为它似乎有点轻"的一面 - 像 RequireJS 这样的东西似乎有它背后有更好的社区支持.

I tried Coffee-Toaster , as it seemed to hold some promise in making it simple to define dependencies etc, but it failed to deal with Windows paths (the old vs /), so gave up on that, mainly because it seemed to be a bit on the "light" side - something like RequireJS would seem to have a much better community support behind it.

感谢您提供的任何帮助.如果可能的话,我真的在寻找有效的源代码示例.

Thanks for any help you can provide. I am really looking for working source code examples if possible.

推荐答案

首先,如果您使用的是 RequireJS,您将很难从定义函数中返回多个事物".RequireJS 使用 AMD(!NOT!CommonJS)格式标准",它不包含用于导出东西"的 module.exports 对象,而是依赖于返回的东西.

First off, if you're using RequireJS you're going to have a not-easy time returning multiple "things" from a define function. RequireJS uses AMD (!NOT! CommonJS) format "standards", which doesn't contain a module.exports object for exporting "stuff" but instead relies on return something.

话虽如此,我不确定您在这里寻找什么,但是使用 RequireJS 进行课程工作非常容易.像这样的:

With that said, I'm not exactly sure what you're looking for here but having a class work with RequireJS is pretty easy. Something like this:

define ['my/required/module'], (myModule) ->
    class MyOtherModule
        privateField = 0

        constructor: ->
        publicMethod: ->

    return MyOtherModule

这可以在 require/define 函数中使用,就像任何其他脚本一样.举个例子:

This can be used in a require/define function just like any other script. Take this example:

require ['my/other/module'], (MyOtherModule) ->
    instance = new MyOtherModule()

我们甚至可以将它与扩展"一起使用

We can even use it with "extends"

define ['my/other/module'], (MyOtherModule) ->
    class MyThirdModule extends MyOtherModule
        ...   

希望这会有所帮助!

这篇关于将 Coffeescript 类和 RequireJS(或 Curljs 或类似)用于客户端浏览器库的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆