开发者

How to play dojo with sprockets in rails edge?

Rails 3.1 edge use sprockets to handle .js and .scss files. Sprockets use comments to deal with dependencies.

I put the three folders (dojo dijit dojox) in vendor/assets/javascripts. Then add a line in app/assets/javascripts/application.js //= require dojo/dojo console.log(d开发者_Python百科ojo);

Now dojo has been merged into application.js. But dojo has dependency system itself. When I require more dojo modules. It cannot find the right path. dojo.require("dojox.grid.DataGrid"); // Error in webkit console: Error: Could not load 'dojox.grid.DataGrid'; last tried '../dojox/grid/DataGrid.js' // Error in rails server log: Started GET "/undefined../dojox/grid/DataGrid.js" for 127.0.0.1 at Sat Apr 16 01:26:05 +0800 2011

These are two different dependency systems. How can I put them together?


dojo.js doesn't take too naturally to being renamed. This is because part of Dojo's initialization process involves searching through the DOM for the script tag responsible for loading itself. It does this for two reasons:

  1. Determine its baseUrl if not already specified
  2. Pick up any djConfig (or data-dojo-config in 1.6+) properties specified in the script tag itself

When it searches for this script tag, it looks for one with src set to dojo.js or dojo.xd.js; it's not finding it in your case, thus the failures.

It should be possible to work around both of these issues, by specifying djConfig (or dojoConfig in 1.6+) fully programmatically in another script tag before the one that loads Dojo, and by specifying baseUrl manually in these config properties.

For example, if your application.js were in vendor/assets/javascripts as referred to in your original post, you could try doing this:

<script type="text/javascript">
    //if you're using 1.5 or earlier, use djConfig instead of dojoConfig
    var dojoConfig = {
        //note that baseUrl points to the folder containing dojo.js, therefore the dojo folder
        baseUrl: 'vendor/assets/javascripts/dojo/'
    };
</script>
<script type="text/javascript" src="vendor/assets/javascripts/application.js"></script>

For more information on dojo config settings: http://dojotoolkit.org/reference-guide/djConfig.html (in fact, that page actually has a note under baseUrl specifically about renamed dojo.js.)


Try adding the dojo files into a dojo/scripts directory under /vendor/assets/javascripts. In the dojo directory, create a dojo.js file in the dojo directory and put //= provide "scripts" in that file.

Then in your application.js file put //= require <dojo>. Check out the Sprockets site http://getsprockets.org/ for info but that should work.

The docs aren't out for Rails 3.1 and the asset pipeline yet so a lot of this stuff is kind of trying to figure out on your own.


Umm... why are you placing your javascripts in vendor and app rather than public/javascripts? you have include anything in public/javascripts with =javascript_include_tag('path_to_javascript') where path_to_javascript would be "dojo/DataGrid" for public/javascripts/dojo/DataGrid.js

http://guides.rubyonrails.org/getting_started.html


I know it's maybe a bit late but I could help someone else.

I finally made it work and posted the explanations here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜