0

I am trying to using jQuery dialog to make a user sign in. I am using ajax and devise. After users sign in, the dialog windows should close. I put dialog("close") inside bind("ajax:success"), but it doesn't work and I get error:

"cannot call methods on dialog prior to initialization; attempt to call method 'close'"

Code:

$(function(){
$(" #sign_in").click(function(){
     $('<div id="box" >').dialog({
        open: function(){ 
                 var that=this;
                 $(this).load("/users/sign_in",function(){ 
                    $("#new_user").bind("ajax:success",function(evt,data,status,xhr){
                    $("div#utility").html('welcome'+data.user+' |<a href="/users/sign_out" data-method="delete" rel="nofollow">sign out</a> ') ;
                    $(that).dialog('close');
                      })
               })
             },
        title: 'Sign in '
        });
   });
}) 

Can anyone help me figure out what the problem is?
thanks

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2366280
  • 131
  • 1
  • 1
  • 5

1 Answers1

0

here is a question that has answers that might help with your problem.

jQuery: Load Modal Dialog Contents via Ajax

you can also change your selector for the dialog to $('#box').dialog({ ...

and in your ajax success callback (after you make changes) you can do $('#box').dialog('close')

EDIT: this work?

$(function(){
$(" #sign_in").click(function(){
 $('#box').dialog({
    open: function(){ 
        $(this).load("/users/sign_in");
    },
    title: 'Sign in '
    });
});

$("#new_user").bind("ajax:success",function(evt,data,status,xhr){
    $("div#utility").html('welcome'+data.user+' |<a href="/users/sign_out" data-method="delete" rel="nofollow">sign out</a> ') ;
    $('#box').dialog('close');
   })
}) 
Community
  • 1
  • 1
ajgiv
  • 88
  • 6
  • thanks ajgiv, I have tried using $('#box').dialog('close') and I have tried $('
    ').load(...).dialog(). but I always meet the error 'cannot call methods on dialog prior to initialization'. the problem is when I want to close the dialog, it already there.
    – user2366280 May 09 '13 at 14:18
  • are you doing an ajax request somewhere for the sign in/sign up portion? – ajgiv May 09 '13 at 14:29
  • yes, I load page from /users/new, I use devise. every thing works fine but the dialog cannot be closed after sign in. I can close the dialog by clicking the close button. – user2366280 May 09 '13 at 14:49
  • I got solution from http://stackoverflow.com/questions/15626675/cannot-call-methods-on-dialog-prior-to-initialization-attempted-to-call-method and it really works – user2366280 May 10 '13 at 07:08