Andrew Cholakian
Andrew Cholakian

Reputation: 4462

Inline Javascript in rails partials

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?

Upvotes: 1

Views: 578

Answers (2)

aceofspades
aceofspades

Reputation: 7586

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.

Upvotes: 0

Staelen
Staelen

Reputation: 7841

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 =)

Upvotes: 2

Related Questions