I'm trying to use dojo-toolkit as the JS framework on a rails 3.1 app, but I'm struggling to incorporate the dojo require structure with the sprockets require and coffeescript. It seems dojo expects the JS files on the disk, but I guess they're created on the fly from coffeescript.
Wondering if anyone has an idea of how the two require-s can co-exist.
I recently had to install dojo with rails 3.1 and the asset pipeline. Here are the steps I followed to make it work:
1/ Include Dojo
Put the dojo SDK under vendor/assets/javascripts so you get the dojo, dijit and dojox folder in it. Include it in your template:
= javascript_include_tag "dojo/dojo", :'data-dojo-config' => %Q(baseUrl: '/assets/dojo/', modulePaths: {modules: '/assets/modules', widgets: '/assets/widgets'})
Don't forget the leading '/' on assets!
You can use the Google CDN with a fallback:
script var djConfig = { baseUrl: '/assets/dojo/', modulePaths: {modules: '/assets/modules', widgets: 'widgets'} };
= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js"
script ="typeof(dojo) === \"undefined\" && document.write(unescape('%3Cscript src=\"#{asset_path('dojo/dojo')}\"%3E%3C/script%3E'));".html_safe
The first line set your djConfig. The second actually requires dojo from Google. The third is the fallback.
2/ Include your base file
Remove all "require" in your app/assets/javascripts/application.js and put something like that (for instance):
dojo.provide("myapp");
3/ Play with dojo.require
In the djConfig in 1/, I set the modulePaths, custom them to what you want. In my exemple, you would have those two where you can put your files:
If I want to require modules/test.js, I just do:
dojo.require("modules.test");
4/ Use coffeescript and ERB
Just add the right extension and start right erb, like described in the Rails documentation.
I hope it helps you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With