1

I have one companion script file for a Rails model, that uses code I've broken down into a hierarchy of over a dozen classes, for things like jQuery/Bootstrap UI code, factoring out similarities between different types of dialog, and so on. Let's say I'm working with articles.js.coffee as the "main page script" here.

I can define Coffeescript classes, namespace them as something like window.ourproject.OurUIDialog, and save them in separate, per-class source files such as app/assets/javascripts/OurUIDialog.js.coffee. Restart the Rails server, and that class can be subclassed, e.g., window.ourproject.PostInfoDialog extends window.ourproject.OurUIDialog. As long as PostInfoDialog is in articles.js.coffee (where the instantiation of the PostInfoDialog is), all is well.

But, if I move the subclass (PostInfoDialog) out into a separate file, e.g., PostInfoDialog.js.coffee, then attempting to do anything at all with it within the main articles script produces

Uncaught TypeError: Cannot read property 'prototype' of undefined

Again:

  1. This revolves around a Rails model's companion script file, here called articles.js.coffee;
  2. window.ourproject.OurUIDialog gets picked up whether it's in its own file or in articles.js.coffee
  3. window.ourproject.PostInfoDialog (which extends OurUIDialog) can only be used if it's not in a separate file, even though viewing the generated HTML shows PostInfoDialog being included with all the other script files.

I'm tearing my hair out trying to figure this out, and I didn't have much left to begin with. Any ideas?

Jeff Dickey
  • 1,071
  • 11
  • 21
  • possible duplicate of [Structuring coffeescript code?](http://stackoverflow.com/questions/6150455/structuring-coffeescript-code) – Trevor Burnham Jun 01 '12 at 18:56

1 Answers1

0

Pretty sure that Trevor Burnham answered my question when he answered this one; I just didn't see it the first dozen times I searched. :-P

Thanks to both of you for reading this one, though. :-)

Community
  • 1
  • 1
Jeff Dickey
  • 1,071
  • 11
  • 21