Posts for: #Jquery

Rails: Unobtrusive and i18n Javascript confirm

How many times do you heard you should separate content from behavior? Never? Hmmm don’t try to cheat me.. So, why do you still use :confirm option for link_to helper?

Here a little snippet to archive our goal, and as extra bonus, you get it i18n:


<script type="text/javascript">
window._authenticity_token = "";
</script>

t(:’asset.destroy’),
:class => “delete”,
:’data-confirm’ => t(:’asset.destroy_confirm’) %>


$(document).ready(function(){
$(".delete").bind("click", function() {
if(window.confirm($(this).attr("data-confirm"))) {
$.ajax({
url: $(element).attr("href"),
data: {
_method: "delete",
authenticity_token: window._authenticity_token
},
type: 'post',
dataType: 'script'
});
}
return false;
});
});

[]

Hanoi: Automated jQuery tests with QUnit

About a month ago, we have released adva-cms 0.2.0, the most relevant change was the migration from Prototype to jQuery. I always been a loyal Prototype fan, and, until then, I always tested my code with `unittest.js`, combined with `jstest.rb`.

Like other rewrite we needed to rely on a solid test layer, but what I noticed was the lack of a valid automation support for QUnit, the test framework used internally by the jQuery team. So, in a couple of days I built what we need, just creating a custom version of jstest.rb and changing a little bit QUnit, in order to communicate with it.

[]

How to use Sprockets with Rails

Sam Stephenson has recently released a Javascript preprocessor called Sprockets. It’s distributed as Ruby gem, and it helps to declare scripts dependencies through special comments, and safely build them one script. It’s very useful for maintain your Javascript projects, extract reusable code and share across applications.

Installation

$ (sudo) gem install sprockets It will also install sprocketize executable.

How it works?

To declare a dependency use require directive:
//= require //= require "cookies" When you use angular brackets the script will be searched in all the Sprockets load path, if you use quotes instead, you are forcing to load the file from the current directory. Usually you should use the first way to require frameworks or libraries and the second one for your scripts.

[]

Rails caching and Javascript pt. 2

In the previous article I showed how to take the advantage of Javascript for page caching.

This cache strategy allows to generate static HTML and allow the web server to serve it, without waste a useless Rails request/response cycle. But, since the cached page is stateless we ask help to Javascript for make it a little bit dynamic.

Flash

All of you has probably noticed that flash object is an enemy of page caching. Let me show you why.
class SessionController Now suppose the application is in a blank state, when the user hits the dashboard_url, ActionPack will cache the page, including the flash status. This means it will be always rendered, even if the flash will be expired.

[]

Rails caching and Javascript pt. 1

Rails offer a multi-level view caching based on: page, action and fragment.

Page Caching

For Rails cache a page means to generate the HTML code on the first visit, then allow front-end web server to send it statically.
This is the fastest way to serve pages, because Rails puts the cached page in the public folder, the web server intercept the request and doesn’t forward it to the application server, avoiding an useless Rails request cycle.
class HomeController

[]