I am reading a book for "Ruby on Rails". In the "application.js", we are including other JavaScript libraries in the following way and more specific - jQuery UI:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
As this is ordinary JavaScript file (not ruby extensions here like "html.erb" for exmaple) how the application knows to execute the require command? What kind of JavaScript syntax is this:
//=
and as this is ordinary JavaScript file, why we are not using "script" tags to include JavaScript files?
Also, I have read here that "require" method will check for this library in define by $LOAD_PATH variable folders. How can I see where the "jquery-ui" is stored? I am asking because in other applications in order to use jQuery UI I should add not only JavaScript file, but css file and images used by the library - in this case, we are doing this only with single line?
What kind of JavaScript syntax is this.
Anything starting with a // is a Javascript comment.
How is it able to process it ?
Sprockets on the server side scans the JS file for directives. //= is a special Sprocket directive. When it encounters that directive it asks the Directive Processor to process the command, require in this example. In the absence of Sprockets the //= require .. line would be a simple JS comment.
Ruby require vs Sprockets require
These are two completely different things. The one you link to is Ruby's require.
Why not use script tags to load JS files.
Usually, you want to concatenate all your app JS files and then minify them into 1 master JS file and then include that. I recommend reading the YSlow best practices on this.
I also recommend watching the Railscasts on Asset Pipline - http://railscasts.com/episodes/279-understanding-the-asset-pipeline
Cheers!
Related
I am new to rails so pardon the question. Now, why do we have to require the JS files inside the application.js file or the css files inside the application.css file? As far as I have read that when we launch the server rails loads all the javascript and css files from the directory into one file, so if it already loads all the files from the directory why there is a need to write inside the application.js or application.css file?
For example:
//= require abc
//= require xyz
If I already have abc.js and xyz.js file, why should I require them inside application.js file?
You are misunderstanding the concept. Let me explain the process. As you know, when you launch the server, rails first precompiles the files inside the assets folder with the help of sprockets-rails gem, but it does so by following the directives specified inside the manifest files i.e application.js and application.css.
Now inside the application.js you have "//= require_tree .", this tells sprockets to load all the files inside the javascript directory, process them, compress and combine them to produce one master Javascript file, this helps to reduce the page load time of the website. Now,here is your question, since "//= require_tree ." directive already takes all the javascript files present inside the javascript directory, why there is a need to specify the javascript files inside application.js? The answer is "Order".
"//= require_tree ." it loads, compress and combine all the JS files in an unspecified order or random order. Now, if you are a web developer or have started now, you might know or will come to know that many times you have to load JS files in some specific order, otherwise there may arise some conflict when we implement them, they might not work as we want them to.
One such famous combo is jquery and bootstrap. In order to use bootstrap JS part it needs jQuery, so you have to initialise jquery first and then bootstrap. Precisely for this reason in rails you require the files inside application.js specifying the order in which you want the sprockets to load,compress and combine into one master JS file. As sprockets processess the directives from top to bottom in the order specified in the application.js file, it becomes important to require the files in application.js file. If you have 2 javascript or css files which in no way connflict with each other then, there is no need for you to require the files inside application.js or application.css file and they will still work fine.
For example:
//= require jquery
//= require jquery_ujs
//= require bootstrap.min
//= require_tree .
Above, the sprockets-rails gem will first load jquery.js file then jquery_ujs.js file and then bootstrap.min.js file in that order. There is no need to add the extension as it assumes that all the files will be of type javascript only.
The above whole explanation also applies for the precompilation of css files specified inside application.css.
For more information, I advise you to visit http://guides.rubyonrails.org/asset_pipeline.html and read about rails asset pipeline.
New to Rails so bear with me...I was looking at a manifest file (application.js) today while researching the asset pipeline and was curious how the directives such as //= require jquery are being read. Is this something Sprockets is doing in the background? How? Why must the directive be commented out first, and the equal sign added? If I uncomment the directives and load the application.js file in my broswer, I no longer see the jquery library contents. Just curious how this is working in the background.
Also, when I add my own custom css stylesheet, do I add a require directive in the application.css manifest file, or do I add the stylesheet link such as <link rel="stylesheet" type="text/css" href="mystyle.css">? Or do I do both? I'm assuming I shouldn't add css directly inside of the manifest file...
Thanks!
Dont know how much you know, so will try to explain in details.
Rails stores our assets (like images, css, js files) in separate places, so its all in order and better for us - devs, to use. So thats called Assed Pipeline. When Rails loads those assets, say, css files, it creates one big file from all our app files, to avoid multiple calls. And Manifest is like a map or rules for Rails which files to include in that big css file and this *= is whats telling Rails what exactly to include (I consider it as a Rails syntax). So when you have something like this:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
require_tree . tells Rails to grab all files from the javascripts folder, while //= require jqueryand others directs Rails to special cases - assets, usually used by your gems (those files you never keep in your javascripts/stylesheets folders, so //= require_tree . cant see them).
When you add your on css file, you just add it in the stylesheets folder and require_tree informs Rails to include it in the big picture. But Rails has a nice feature - scaffolding. You scaffold your object with command rails g scaffold User and Rails creates everything for you - views, controller, model, tests (and who knows what else :) ). So in this case you don't even need to create your css file, just insert css rules into it and Rails will find it due require_tree .
A bit different story with sass files:
If you want to use multiple Sass files, you should generally use the
Sass #import rule instead of these Sprockets directives. When using
Sprockets directives, Sass files exist within their own scope, making
variables or mixins only available within the document they were
defined in.
So if you will be using Bootstrap (probably will), this is a important to know as well.
Hope this helps
How? Why must the directive be commented out first
Because this is sprockets directive. It is executed well before any js/coffee in that file gets the chance to run. And css is not "runnable" code at all. How do you make this kind of code not produce any errors? You comment it.
... and the equal sign added?
To tell these special directives apart from other, "regular" comments, which may be in that file.
I'm assuming I shouldn't add css directly inside of the manifest file...
Why not, go ahead. Although you may want to put any custom code in separate files for reasons of code organization. But technically, there's no problem here.
Also, when I add my own custom css stylesheet, do I add a require directive in the application.css manifest file
No need, require_tree . will find and include your file.
or do I add the stylesheet link such as <link rel="stylesheet"type="text/css" href="mystyle.css">?
Nope, don't do that.
I have a similar problem of using Angular with Grails, pls. refer this question that already gives all the details/solutions/answers: How to integrate angular js into grails 2.3.4?
In the answers to this question, people have given good answers, but they are talking about ....
"require the module on screen"
... & sample code (?) fragments like these:
//= require angular/angular.js
//= require_tree views
//= require_self
What is the meaning of "requiring" the module ("resource bundles": as given in the first answer), & other/above code fragments, or whatever they are.
Does it means normal require statements:
require(['angular']);
etc.?
Things are not very clear.
In a Ruby on Rails application you would use manifest files to include all your dependencies. That is a code snippet from the application.js (default) manifest file where it's including javascript files or paths using the "//=" javascript include syntax.
Grails uses different resource mangement plugins for javascript files, css, images, etc.. Code you mentioned
//= require angular/angular.js
//= require_tree views
//= require_self
is from grails asset-pipeline plugin.
Resource bundles term used in grails resources plugin where you declare bundles in ApplicationResource.groovy file.
When you want set of js or css file to be included in a page you just need to include manifest file (for assest pipeline) or require module (for resources).
Both plugin have good documentation to refer.
//= require angular/angular.js
Is simply rails syntax for requiring and registering dependencies to your rails applications. Although your question is a little vague if you are trying "require an angular module" than you must list it as a dependency when you declare your angular module. This is totally unrelated to rails but will look something like this:
angular.module('myApp', ['ngRoute', 'ngTouch',])
where the items inside the array (ngRoute, ngTouch) are other modules that become depencies of 'myApp'.
This page should help!
I'm planing to create a simple Ruby gem. The functionality is depending on some .js files. My plan is to install the gem via Github.
My questions are:
should I write a generator to copy the js files?
what is the best place to keep the js files (is it vendor/assets/javascripts?)
how to write test cases for this (rspec2) ?
I'm using Rails > 3, Ruby 1.9.x and Ruby 2.x.
We can easily use Javascripts library in ruby in following way -
1) We can put all js libararies in Vendor section and for their use
we can define then application.js in following way
//= require datatable-pagination
.
.
2) Next if you want to use your own javascript then u put them into
your assets folder and use then application.js in above way.
3) And if you want to make your own js library, then you can put
your js libarary in lin folder.
I'm using the Rails3 asset pipeline for js. I'd like to wrap some third-party libs in a namspace, but I can't get it to work. For example, this
var myns= function() {
//= require swfobject
};
produces literally:
var myns= function() {
//= require swfobject
};
That is, the require wasn't invoked. Is the asset pipeline syntax so fragile? Where is this limitation documented? Is there a workaround or better way?
First time I see this use case of assets pipeline.
Anyway you should check the sprockets documentation https://github.com/sstephenson/sprockets (The Directive Processor)
Sprockets runs the directive processor on each CSS and JavaScript
source file. The directive processor scans for comment lines beginning
with = in comment blocks at the top of the file