Javascript/Ajax questions for rails applications

I am loving just about everything in rails right now. I find a lot of things faster than java and it's a pleasure to work with.

One area though that I am not sure how to proceed with is ajax/ javascript.

For those of you using rails and a lot of ajax, what are your thoughts on the built-in functionality for javascript? What about jquery-rails? Do you go with it? Do you fight against it? Do you start from scratch?

With 3.1 having so many changes when it comes to Javascript, does it make sense to setup your 3.0.7 project in a certain way to make it easier to upgrade later?

There are also a lot of possibilities with other libraries on top of jquery. For example, some people recommend using something like backbone.

I have found on my last java program that require.js was really handy, but I'm not sure if that was the 'best' way to structure large applications. It was definitely better than doing it ad-hoc and just sprinkling 10 script tags on every page.

I'd like this project to have some cohesion and structure when it comes to how I lay out my javascript this time around. The javascript features are going to be really intense, and I don't want to run into any pitfalls.

What would you suggest? While I am really experienced in developing applications, I am not the most experienced when it comes to ajax- specific ones. I am also pretty new to rails... so if there's common conventions and common library setups that everyone "just uses", I won't be aware of it right now.

Thanks for the help.

I am loving just about everything in rails right now. I find a lot of

things faster than java and it’s a pleasure to work with.

One area though that I am not sure how to proceed with is ajax/

javascript.

For those of you using rails and a lot of ajax, what are your thoughts

on the built-in functionality for javascript? What about jquery-rails?

Do you go with it? Do you fight against it? Do you start from scratch?

Given that I knew very little about ajax and jquery when I started working with rails I have never touched the built in rjs and probably never will. Up to now it has just never seemed necessary and another place that something could go wrong. Definitely use jQuery if you have not already. That is a level of abstraction that is a lifesaver.

You might want to jump directly to coffeescript (use the barista gem). It is the rubyfication of javascript and makes coding client side both fun and productive again and will be part of rails 3.1. It works more than nicely with jquery too. Take a look at this presentation if you aren’t convinced yet: http://coffeescript-seattlejs.heroku.com

Martin

You might want to jump directly to coffeescript (use the barista gem). It is the rubyfication of javascript and makes coding client side both fun and productive again and will be part of rails 3.1. It works more than nicely with jquery too. Take a look at this presentation if you aren't convinced yet:http://coffeescript-seattlejs.heroku.com

Martin

I have coffee-script and barista installed. I got everything working *almost*, but I ran into 1 little snag.

Whenever i make a change to a .coffee file... Barista does the right thing and attempts to compile all of my .coffee files automatically. This is cool.

However, I always get a compile error - even if the syntax should is correct and the script is very simple. The compilation error doesn't say anything specific about what went wrong either. It just says:

    Barista::CompilationError

    Error: In app/coffeescripts/lib/dynamic_menu.coffee, V8::JSError

    Rails.root: /home/egervari/Projects/training

The strange this is... if I restart the server, everything is fine again. When I go to make 1 change to the .coffee file - ANY change at all - this error rears its ugly head again, even though the syntax is 100% okay.

Any reason why it would be doing this?

Coffeescript is very sensitive to whitespace, and it does not support !== and ===

if you have that in your code that might be a reason for the error, cost me an hour today:)

Why don’t you post your code in a gist so that we can take a look at it?

Martin

Coffeescript is very sensitive to whitespace, and it does not support !== and === if you have that in your code that might be a reason for the error, cost me an hour today:) Why don't you post your code in a gist so that we can take a look at it?

Martin

Well, I have 2 scripts so far. One is 'tour.coffee' and the other is 'dynamic_menu.coffee'.

Whenever I make a change to 'tour.coffee', everything works just fine - it will compile over and over again as I make changes.

However, if I make a change to 'dynamic_menu.coffee' - a file that isn't being directly loaded yet - that is when I get the compile error (regardless of what is in this file).

I don't know if this dynamic_menu.js works - I haven't bothered to really test it. But the overall point is that I can put:

    alert "Hey"

... and it will still have a compile error.

Is there a better way I can do dependencies? 'tour.js' is supposed to depend on this dynamic_menu.js. I would have used require_jquery.js before, but I don't think I can use that with coffeescript anymore. However, if coffeescript can include them internally, then that would be fantastic and might solve this compiling problem in one go.

Actually, to be honest, I get compile errors when I work with tour.coffee directly. Anything non-trivial and Barista just isn't good errors - it's not giving ANY errors.

Is this normal? Is the coffeescript compiler just a pure "yes/no" syntax checker? If so... I really don't think I can use it. I really want my tools to help me out and give me good feedback.

I really hope I am just doing something wrong. I don't think this is a question of whether I am writing good or bad coffeescript... I think something more fundamental is just wrong with compiling and giving good errors.

Yeah, I add lines like:

      i = 3 + 4

... just anything random and it's causing these "compilation errors".

I can't use coffeescript if this is what it is like to develop using it. Please tell me I have it setup wrong. If the coffeescript parser can't give good feedback as it compiles to javascript, then I am positive it would set developers to a productivity loss rather than a productivity gain.

On top of this, my internet connection has died 2 times today as I try and find answers... Ubuntu 11.04 has constantly removing my minimize/ maximize buttons on my windows... and I had to reboot several times for other misc. problems with linux.

Overall, today has just been a really bad day for development. It doesn't seem like anything is 'just working' :frowning:

Quoting egervari <ken.egervari@gmail.com>:

I am loving just about everything in rails right now. I find a lot of things faster than java and it's a pleasure to work with.

One area though that I am not sure how to proceed with is ajax/ javascript.

For those of you using rails and a lot of ajax, what are your thoughts on the built-in functionality for javascript? What about jquery-rails? Do you go with it? Do you fight against it? Do you start from scratch?

Unless you have a lot of existing RJS, don't use jquery-rails. It is not being maintained and is still at jQuery 1.3. IIRC, jQuery 1.6 has been released. jquery-rails was a nice transition point for moving from RJS and Prototype to jQuery. Having done that, I am going to convert all the RJS to straight jQuery and/or move to Coffescript. At this point, unless jQuery-rails is updated from the newer jQuery releases, it is useful only as a transition of existing RJS. Don't write new code for it.

HTH,   Jeffrey

Is this normal? Is the coffeescript compiler just a pure "yes/no" syntax checker? If so... I really don't think I can use it. I really want my tools to help me out and give me good feedback.

I don't think coffeescript is a yes/no compiler. On their webpage http://jashkenas.github.com/coffee-script/ in try coffeescript tab, if you make syntax error, compiler will tell you exactly the line number and type of your mistake.

So, maybe problem is with barista gem?

Just to help investigate the issue: I have exactly the same compilation errors.

My configuration: rails 3.0.7 jquery-rails barista webrick haml compass Ubuntu 11.04 (but I have no problems with Internet or minimize/ maximize buttons, only this coffeescript error)

Same thing happens to me. I've taken to using 'rake barista:brew' which makes everything work again. Faster than restarting the server at least...

Hi, Not sure if I should start a new thread with the original question for this one... this thread branched off to coffee script, I think it will be awesome if we can get the sentiments of the folks here about the questions put forth...

I don't know much about coffee script, and I am a follower in Rails having not migrated to Rail3 either. What I know is that I don't like going around the bush of things, and coffee script is that for me. So, I want to use JS directly.

The question is: what is Rails' strategy for Javascript? I don't think hiding it with things like RJS/CoffeeScript will yield good performing apps. I am personally working to leverage JS directly, which gives me the freedom at the client-side and the performance too. However, I am not leveraging Rails enough that way. I, for example, use the Dojo library; which is awesome for building a large app, though there is not interworking with Rails that I know of. The original question above mentioned libraries like backbone, require.js, etc. And in the mobile space there is embed.js and others...

While I have Rails Conf videos on my TODO list to watch soon; can anyone elaborate on what the direction is?

Coffeescript does not impact performance, not at all. It translates directly into javascript statements but takes the annoying parts, like prototype definitions and checks for undefined, out of it.

Here is a sample I did to show you the difference between the code. You will see that the javascript that is the compilation result is quite close to how it would have been written if done by hand.

Coffeescript https://gist.github.com/979717

Javascript https://gist.github.com/979725

The original poster asked, among other things the following:

With 3.1 having so many changes when it comes to Javascript, does it make sense to setup your 3.0.7 project in a certain way to make it easier to upgrade later?

I am quite new to the rails world, but even for me it is obvious that coffeescript combined with jquery will be the client/ language/lib of choice in Rails 3.1 and that RJS is dead. So it is sensible to start right now to develop future code in jquery and coffeescript, which directly answers the above question.

I also believe that backbone.js will be the winner for MVC for web based apps. Can’t speak for mobile though

To answer another question: I pretty much ignore the built in ajax functionality, except for the delete functionality, which is nice. I use jammit and jammit-s3 instead of something like require.js, and personally I hope to use it in 3.1 as well. I also use backbone and coffeescript together to easily modularize my apps.