Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not override/bind AJAX #181

Closed
runlevel5 opened this issue Jul 10, 2011 · 6 comments
Closed

Could not override/bind AJAX #181

runlevel5 opened this issue Jul 10, 2011 · 6 comments

Comments

@runlevel5
Copy link
Contributor

With jQuery 1.4.2 and Rails 3.x (manual method), I could override and bind the ajax to invoke callbacks on ajax success. However, this doesn't work with jquery-ujs 1.0.12 and Rails 3.1 (git) that leads to two requests to be invoked instead of one. I suspect this is related to issue #10 Here is an example:

  1. Generate test app

    rails new test123
    cd test123
    rails g scaffold states name:string abbr:string
    rake db:migrate
    
  2. Make sure we're usin the latest 3-1-stable by modify Gemfile:

    gem 'rails', :git => 'git://github.com/rails/rails.git', :branch => '3-1-stable'
    
  3. bundle install
  4. Modify following files:

app/controllers/states_controller.rb:

  def new
    @state = State.new

    respond_to do |format|
      format.html # new.html.erb
      format.js { render :text => 'hello world' } # we add this 
    end
  end

app/views/states/index.html.erb:

<%= link_to 'New State', new_state_path, :remote => true %>

app/assets/javascripts/states.js:

$(function(){
    $('a[data-remote=true]').live('click', function(){
        $.get(this.href, function(data) {
          alert(data);
        });
        return false;
    });
});

With the help of Firebug, it is indicated that there are 2 requests invoked. This might explain that the binding is not successful.

@JangoSteve
Copy link
Member

Firstly, jquery-ujs no longer supports jquery 1.4.2, it's now jquery 1.6 and later. So there are going to be things that don't work when using 1.4.2. However, this is unrelated to the particular behavior you're seeing.

What's happening is that 2 requests are being made, because first, jquery-ujs is making a remote call (since you put :remote => true on the link). And then you're also making the call outside of jquery-ujs with your $.get function in your states.js file.

If you want jquery-ujs to handle the remote call and fire the callbacks, then get rid of your manual ajax call. Otherwise, get rid of the :remote => true.

If you're still having problems, please post over on the jquery-ujs issues.

@JangoSteve
Copy link
Member

Also, it might be helpful to read through some of the articles in the wiki. I also wrote a top-down guide to using jquery-ujs that might help.

@runlevel5
Copy link
Contributor Author

@JangoSteve: I am working on porting Spree project to the new jQuery 1.6. And peculiar I found that the same code only create 1 request under jQuery 1.4.2 + old UJS version. So I guess jQuery 1.4.2 + old UJS did the job wrongly.

@JangoSteve
Copy link
Member

That could be the case. It's been a very long time since we jquery-ujs supported 1.4.2.

@JangoSteve
Copy link
Member

Has this been figured out then? I'd like to close the issue if possible.

@runlevel5
Copy link
Contributor Author

yes please, it's resolved.

On Tue, Jul 19, 2011 at 3:18 AM, JangoSteve <
reply@reply.github.com>wrote:

Has this been figured out then? I'd like to close the issue if possible.

Reply to this email directly or view it on GitHub:
#181 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants