ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

liveUrlTitle() JS scope issues

August 29, 2012 2:04pm

Subscribe [1]
  • #1 / Aug 29, 2012 2:04pm

    dehuszar

    99 posts

    This is slight tangent from this post: http://ellislab.com/forums/viewthread/219260/

    I have a form which barring the issue in the above link is working well.  I’m loading a separate safecracker form in a jQueryUI dialog so that if a Playa entry is not available, an author/editor can load the form up quickly, create their entry, and then using stock jQuery AJAX success triggers closing the overlay and blammo, the new entry is waiting in Playa.

    Problem is, I’m getting some scope separation problems.  My JS skills are a little weak here, and and I can’t figure out where the liveUrlTitle() function is being drawn from.

    Here’s what’s happening.  The title field of the form in the dialog is updating the url_title field on the form below the dialog.  This may be because I’m using jQuery’s $.load() method to pull only the form contents from the original template which is a complete and independent form which can be accessed directly, so I don’t know if the new instance of the liveUrlTitle() function is binding to the original form.

    Is there any insight on how/where this code is, or what I might do to correct this situation?  The dialog loaded form submits correctly, and safecracker seems to be generating the url_title fine in the DB even if the url_title field is blank in the dialog form.  I just need to find out where/when/how the liveUrlTitle() code is binding to url_title field so I can prevent the data from crossing forms.

    Thanks in advance.

  • #2 / Aug 30, 2012 3:33pm

    Kevin Smith

    4784 posts

    Hi dehuszar,

    Wow. This sounds awesome. I like how you’ve thought through the user’s experience to this degree. I’ve gotta say though, this is definitely outside my wheelhouse. Could I move you over to Community Help to see if any JS ninjas over there can lend a hand?

  • #3 / Aug 30, 2012 3:52pm

    dehuszar

    99 posts

    For sure.  Once there’s a completed solution, I’ll def blog out what I did for others to reuse.

  • #4 / Oct 04, 2012 8:39pm

    dehuszar

    99 posts

    So, I’ve been digging back into this issue a bit, and further investigation reveals that the javascript which normally loads and gets appended after the safecracker form tag is not getting loaded during the AJAX request.  The CSS resources for each relevant fieldtype is getting appended after the form. 

    I’m guessing this is jQuery issue, but I’m wondering if anyone else has any insight on how to make sure the fieldtype JS gets loaded and eval’d via the AJAX request, or manually loaded and eval’d afterward?  I’ve tried a few things, but nothing seems to stick.

    Here’s my script presently:

    $( ".form-dialog-link" ).click(function(e) {
      var thisUrl = $(this).attr('href');
      $("#content").append('<div id="form-overlay"></div>');
     $( "#form-overlay" ).dialog({
      autoOpen: false,
      dialogClass: "subform",
      width: 500,
      modal: true,
      close: function() {
       allFields.val( "" ).removeClass( "ui-state-error" );
      }
     });
     $('#form-overlay').load(thisUrl+' #content', function() {
      $(".safecracker fieldset label:first, .safecracker #url_title, .safecracker label[for='url_title'], .safecracker label[for='status'], .safecracker .checkbox").hide();
      var loadingImage = "/images/interface/loading.gif";
      var submitButton = "<input type='submit' value='submit' />";
      $('.safecracker').submit(function(event) {
        /* stop the form from submitting normally */
        event.preventDefault();
        $("#output").empty();
        $("#output").removeClass();
        /* replace the submit button with a loading image to prevent multiple clicks and indicate the form is being processed */
        $(".safecracker input[type='submit']").replaceWith(loadingImage); 
        /* send the form data using post and check the results for any errors*/
        $.ajax({
         url: thisUrl,
         type: "post",
         data: $(this).serialize(),
               
         /* If there was some kind of an AJAX error, display an appropriate message or take some other action of your choice */
         error: function(jqXHR, textStatus, errorThrown) {
          displayAjaxMessage("Sorry, there was an error submitting your form. Please contact CCR by phone for further assistance.");
         },
            
         /*  parse the HTML returned by EE to see if they forgot to enter an email address or a message.
         If so, the HTML will contain a specific error string we can match, and then we can display our own message */
         success: function(data) {
          if (data.match(/<title>Error<\/title>/)) {
           //grab the error message
           var error = $(data).find('ul li:first').text();
           $("#output").empty();
           $("#output").removeClass().addClass("error").html(error);
           $("img.loading").replaceWith(submitButton);
          } else {
           /* clear any error messages still showing in the output area and prep with success message */               
           $("#output").empty().hide();
           $("#output").removeClass().addClass("success").text('Thank you!  We have received your submission.');
           /* clear fields now that the form has been successfully completed */
           $("img.loading").replaceWith(submitButton);
           /* display our success message with enough time to read it and then clear output */
           $("#output").fadeIn("slow").delay(4000).fadeOut("slow", function() {
            $(this).removeClass().html('');
           });
         $('#form-overlay').dialog('close');
          }
         }
        });
      });
     });
     $('#form-overlay').dialog('open');
     e.preventDefault();
    });

    What this does is, if I have a form which can be loaded normally at http://domain.com/template_group/template, when I need to load it in an overlay from another form, I click on a link with the above href, the click event prevents the redirection, sets that href as a var which gets passed to all the AJAX request url declarations, and the form loads really nicely in the overlay.  ...except for the JS, which is problematic for a few fields.

    Any help would be great!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases