Kombo
Kombo

Reputation: 2381

Respond to different js functions on the same show action?

I'm on Rails 3 using jQuery. I'm having a small issue with an application on how to deal with responding to multiple functions from the same action.

On my Show action I have two things going on, Ajax pagination and then a lightweight live search (via Ryan Bate's Railscasts). The Ajax for these looks like:

Pagination:

$(function() {
  $(".pagination a").live("click", function() {
    $.getScript(this.href);
    return false;
  });
});

and Search:

$(function() {  
  $("#songs_search input").keyup(function() {
    $("#search_songs ul").show();
    $.get($("#songs_search").attr("action"), $("#songs_search").serialize(), null, "script");
    return false;
  });
});

Both of these are working fine. What I'm struggling with is responding to these scripts in my show.js.erb file, I don't know how to only respond to one of these actions at the same time, my show.js.erb file looks like:

$("#list_songs").append("<%= escape_javascript(render(@list_songs)) %>"); #pagination
$("#search_songs").html("<%= escape_javascript(render("songs/search")) %>"); #searching

The problem is, with every keyup from the searching, the show.js.erb file will respond and append more pagination. How do I isolate these responses to only fire when the appropriate script is being run?

Thanks

Upvotes: 0

Views: 95

Answers (1)

tastybytes
tastybytes

Reputation: 1308

You could send the script a variable denoting which action called it (the click or keyup), then when you get the response you do the appropiate action, add pagination, or show the results.

Upvotes: 1

Related Questions