-1

The question:

How do I get somthing like that in coffeescript

modules_list[some_calculation(__FILE__)]=a_local_class.new  

I have a module manager, that handles all my coffeescripts (js) at runtime - i.e. while in browser, that means:

I have

  • many modules, not all are in all situations loaded 'statically'

  • only some have dependencies

  • module manager resolves things like init and dynamic reload (ajax) and dynamic init

A module looks like this in prinzip:

class Book
    constructor: ->
        ...

    init: =>
        $$$.get_module('BookReader') #<- ajax loaded if not already

    later_on: => 
        $$$.get_module('LetterCounter').count_letters(@) #<- ajax loaded if not already

    ...


@$$$.modules_list['Book'] = ->
    new Book

this all works (very) satisfying.

But I have a rendunancy in my Logic, because a 'module', has a module name, a class name and - thats the point - a file name

the class name is - thats clear - not a problem, I could name all 'Foo'. Its only not nice to do that.

But modulename ( modules_list['Book'] ) and the coffee- (js-) file name are redundant in sence of Rails CoC.

Any ideas how to get

@$$$.modules_list[some_calc(__FILE__)] = -> new Book

i was there, and there may be a solution for me, but if, I dont understand it.

thanks in advance

ps.: for those who want to know "why I do this":

I have 3 completely different apps for 3 different customers, I am far away to say what is the base for all, 2 apps a realy big with a huge amount of js, that is not always needed so I want to reload it dynamicaly. I "help" asset pipeline a bit with putting all files (staticaly) used in a single one without the ones I was working on the last 20 mins (dynamicaly) for better debugging, and I get rid of all (down-)load ordering problems

Community
  • 1
  • 1
halfbit
  • 3,773
  • 2
  • 34
  • 47
  • Why does it need to happen at compiletime? Does `module.filename` work? – Aaron Dufour Dec 18 '14 at 21:55
  • @AaronDufour: I think that only at compile time, the information is available, because after that its typical within a "md5-somthing.js", and the original name of the file is maximal in a comment somewhere. oops, could this be a solution? Or do you think about a generator? There is no module.filename – halfbit Dec 19 '14 at 10:15
  • I'm not sure what you mean by "There is no module.filename". Are you running some ancient version of node? It's even [documented](http://nodejs.org/api/modules.html#modules_module_filename). – Aaron Dufour Dec 19 '14 at 16:14

1 Answers1

0

There is no need for __FILE__or something like that, in that case. I see that now. It would be the wrong direction of naming.

Its the same 'problem' that you have with Rails it self. If you have

module Base
   class Fun

your filename should be base/fun.rb not vice versa!

The Filename resolves out of "its content" not the other direction.

halfbit
  • 3,773
  • 2
  • 34
  • 47