Rails 3.1 Asset Pipeline for Javascript

Well, I read a lot of information about the new Asset Pipeline for Rails 3.1, and I could not find the right answer to my doubts.

I downloaded the .js files according to the # action of the kind that I performed, on demand. I did this to prevent incorrect bindings and to load small .js files.

candidate_opportunities # index

$(".sortable_drag_n_drop").sortable({
    update: function(event, ui) {
        $.post('/candidate_opportunities/sort', $(this).sortable('serialize'));
    },
    handle: 'span'
});

candidate_companies # index

$(".sortable_drag_n_drop").sortable({
    update: function(event, ui) {
        $.post('/candidate_companies/sort', $(this).sortable('serialize'));
    },
    handle: 'span'
});
$(".sortable_drag_n_drop").disableSelection();

What is the best solution now?

  • Should I change the bindings and let Sprockets compile all my .js files with //= require_tree .?
  • Or should I try to download .js according to my views, so I am not getting a huge application.js
+5
2

, . , , .

, - JS minfy/ . , , - -.

.

1. , .

, . , JS . , , :

config.assets.precompile += ['candidate_opportunities.js', 'candidate_companies']

/javascripts, , .

Rails .

, , .

2. (TM)

, /javascripts require_tree, .

, JS ( URL- ), . require_tree , .

3.1 ( ), , ( ) , application.js

JS. JS, . data-post-url :

<ul class="sortable_drag_n_drop" data-post-url="/candidate_opportunities/sort">

URL JS.

DRYer, JS, , .

+6

Rails. , , , Rails javascript, .

Rails javascript . . js. , js , , , , . , , - javascript, .

, javascript . , , , , javascript .

, javascript, , , , Rails. - :

PageJs = {};
PageJs["users/new"] = function(){ 
  alert("I will be called when users/new action is executed");
};

, . . Paloma , js javascript, , .

, Paloma:

Javascript:

Paloma.callbacks['users/new'] = function(params){
  // This will only run after executing users/new action
  alert('Hello New Sexy User');
};

Rails:

def UsersController < ApplicationController
  def new
    @user = User.new
    # No special function to call, 
    # the javascript callback will be executed automatically
  end
end

, , , . .

!

0

All Articles