开发者

Holy javascript options in rails! Help!

There are a lot of different ways to use javascript in rails 3.0. I was wondering what would be the best approach for a very c开发者_如何学运维omplex ajax application.

It seems that the more progressive programmers are using coffee script these days, and I don't mind learning something a little new.

I have already removed prototype and swapped it for jquery-rails.

Still, I'm not sure if this is enough. I reallyed like require-js, because it really helps modularize code and split everything up into different files. But does this work well with rails' approach?

Also, I may want to use a javascript application framework like backbone. Again, is this overkill or will everything work?

What is the best stack to make very complex ajax applications in rails 3.0.7? What is easiest to develop with and is a nice fit for how rails works?

These decisions are pretty important to make early on, and I don't want to be re-doing my javascript over and over until I get it right. I'd like to draw on the experience of those who've built really complex ajax apps using rails. Thanks!


I think you're overthinking it. Rails 3 + jQuery + jQuery UI has served me just fine for complex Ajax apps.

Edit: Asset expansions allow you to group related JavaScript or stylesheet files so you can easily include them in your views.

For example, say you add this to your config/application.rb:

module YourApp
  class Application < Rails::Application
    # JavaScript files you want as :defaults (application.js is always included).
    config.action_view.javascript_expansions[:defaults] = %w(
      http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js
      http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js
      rails)

    config.action_view.javascript_expansions[:multiselect] = %w(
      jquery-tmpl/jquery.tmpl.min
      jquery-blockui/jquery.blockUI
      jquery-localisation/jquery.localisation.min
      jquery-multiselect/js/ui.multiselect)

In your view you might then say:

<%= javascript_include_tag :multiselect %>

You can do the same with stylesheets. The view helper for that is stylesheet_link_tag. The config method is stylesheet_expansions.

Also look at the content_for helper if you want to put these in a certain place in your layout.


This depends on how godly you are at not writing organically growing spaghetti code with your jQuery code everywhere.

An alternative approach is to keep your JavaScript completely separate from your rails.

You just need one entry point in a view somewhere

script(type="text/javascript" src="require/require.js" data-main="main.js")

I personally recommend you treat main.js as a boot-strapper.

Now you can do all your JavaScript through your main javascript library including loading anything you care about.

ender, Backbone, underscore, extJS (for the UI), whatever you want.

This does mean you JavaScript is very separate from your rails server, but that's ok because you do all your communication over ajax using well defined web APIS.

If your complex ajax application needs to be real time you might want to look at comet or websockets.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜