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

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

问题描述

我们想使用Coffeescript开发一个浏览器(仅客户端)库,特别是,除了纯函数之外,我们倾向于使用Coffeescript的类功能。库将相对较大,因此我们要开始使用一个定义良好的模块模式,但不是为了每个coffeescript类的单个咖啡文件。我们不想在运行中编译咖啡文件,而是作为一个特定的构建步骤,并且不希望将所有输​​出的JS连接到一个文件中。作为最后的要求,我们将使用类似Jasmine的东西进行测试。



有没有人知道这样开发的一个好的示例库,使用Coffeescript和RequireJS ,CurlJS,Browserify等?我已经查看了Github,还有一些例子,但我看不到任何特定于我的需要。



我试过咖啡 - 烤面包机,因为它似乎有一些承诺,使定义依赖关系等简单,但它无法处理Windows路径(旧\ vs /),所以放弃了,主要是因为它似乎有点轻一边 - 像RequireJS似乎有更好的社区支持。



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

解决方案

首先,如果你使用RequireJS有一个不容易的时间从define函数返回多个东西。 RequireJS使用AMD(!NOT!CommonJS)格式的标准,它不包含用于导出东西的module.exports对象,而是依赖返回东西。



说到这里,我不知道你在找什么,但使用RequireJS类的工作是很容易。这样的东西:

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

constructor: - >
publicMethod: - >

return MyOtherModule

这可以在require / define函数中使用像任何其他脚本。以这个例子:

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



我们甚至可以使用extend p>

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

希望这有助于! p>

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.

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.

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.

解决方案

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.

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

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
        ...   

Hopefully this helps!

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

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