Topic: Switching to jQuery replacing remote_function (from RJS)

So I am in the process of switching over to using jQuery and can't quite get the replacement for remote_function to work.

(basically I have a html5 music player that you can click a link to play next track which is working, but also has a handler to detect the end a song and then used to use remote_function to call the same function.... with jQuery I am close but having trouble)

Everything works fine when I click on the link....

view:
  <%= link_to image_tag("control_fastforward.png"), playlists_next_track_url, :remote => true %>

controller:
  def next_track
    puts 'next track was called'
   
    # code to set next track
   
    respond_to do |format|
      format.js
    end
  end

js (next_track.js.erb):
  $('#player_with_controls')
    .html("<%= escape_javascript(render(:partial => 'playlists/player_with_controls', :object => @playlist)) %>");

This works fine.... but when its called a javascript function I have not been able to figure it out.

  function next_song() {
      jQuery.ajax({
        async:true,
        url:'<%= url_for playlists_next_track_url %>',
        type:'GET',
        success: function(){alert('worked')},
        error:function(request){ alert('Error playing next track')}
      })
  }

It does make it to the controller function and sets the next track but the format.js to replace the html never happens (so my next track never starts playing)

I noticed that in the console when I click the link it shows...
Started GET "/playlists/next_track" for 127.0.0.1 at Sat Aug 06 18:02:22 -0500 2011
  Processing by PlaylistsController#next_track as JS

and when the javascript function is called the console shows...
Started GET "/playlists/next_track" for 127.0.0.1 at Sat Aug 06 18:04:04 -0500 2011
  Processing by PlaylistsController#next_track as

I assume since the link uses :remote => true that data-remote is being set and the controller knows to respond to format.js where as the url I am constructing does not.

I'm very new to jQuery so I may not even be approaching this the right way but any help would be appreciated... I've already spent 1/2 a day googling and tweaking just to get this far.

Last edited by BradenWright (2011-08-07 17:11:51)

Re: Switching to jQuery replacing remote_function (from RJS)

So in case anyone else comes across this issue... the solution was easy...

I added the         
  dataType:"script",
line and now it is all working well!!!

jQuery.ajax({
        async:true,
        url:'<%= playlists_next_track_url %>',
        type:'GET',
        dataType:"script",
        success: function(data){},
        error:function(request){ alert('Error playing next track')}
      })