How to use dojo toolkit with rails 3.1 asset pipeline and coffeescript?

前端 未结 1 987
时光说笑
时光说笑 2021-01-13 04:55

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 coffeesc

相关标签:
1条回答
  • 2021-01-13 05:23

    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:

    • app/assets/javascripts/modules/
    • app/assets/javascripts/widgets/

    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!

    0 讨论(0)
提交回复
热议问题