Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of using Coffeescript classes and RequireJS (or Curljs or similar) for client side browser library

Tags:

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.

like image 287
Dunco Avatar asked Jul 09 '12 06:07

Dunco


1 Answers

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!

like image 141
Jason L. Avatar answered Sep 20 '22 12:09

Jason L.