views:

105

answers:

2

When writing partials in Rails I sometimes feel the urge to use inline Javascript for basic DOM manipulation functions that will only ever be relevant to the HTML in the partial. Should I feel bad about this?

My thoughts are that separating out 20 lines of JS into a separate file won't save more than a few KB of bandwidth (in fact it'll probably cost due to the latency of the extra req) and will be hard to organize since I'll have a JS file full of miscellaneous functions separated from their context.

Every one says never use inline JS, but I feel like there's something I'm just not getting here, can someone set me straight?

+1  A: 

I personally think it's more than just to save the bandwidth, it's also about

  1. proper architecture of the application (MVC model, important for code readability, handing over/taking over, etc),
  2. easier debugging experience (since the js are in a single file), and
  3. you can also make the app efficient by caching the js file if it doesn't change that often (no more download queues which may sometimes messed up the whole app if the js is not loaded in the right order)

I believe there are a whole lot more reasons where you are encouraged to separate the js from the partial

Hope it helps =)

Staelen
Still not buying it, JS is in the view whether it's in a .js or in the html. Also, the bandwidth does not matter for this app (each page has megs of images or videos, making 20 lines of JS not even relevant). Also, i'm talking short scripts, < 20 lines.
Andrew Cholakian
well i guess you have to ask yourself why are you concerned with in the first place, if it's only about the bandwidth then you're probably right. For me, it's about readability, debug experience (for me and the next developer), and even reusability of the same js codes across different pages =) there's a lot of other reasons as i'd mentioned =) however, i guess it's each to his own; there's only best practices but no laws in coding =)
Staelen
Well, I'm concerned because while there's no hard laws, when you break with best practices its good to doubt yourself.
Andrew Cholakian
A: 

When necessary, I prefer to generate only inline function calls, perhaps with some server-generated parameters. The function is implemented in a .js file so that I can ensure consistency across the app.

fullware