jeudi 19 février 2015

Javascript Not Executing When Web Page Loaded from Cache


See Update 3 for the latest state. I'm getting the error "Tried to register widget with id==contact-form but that id is already registered" from my dojo script. This issue has come up before here. I believe my situation may be different. I get the error when I go from one page on my site to another. All pages have the same script. If I reload a page, I don't get the error. If I go to another web site and then come back, I don't get the error. My site used to use the Symfony framework. I just converted it to Ruby on Rails. I'm pretty sure I didn't have this problem with Symfony, but I'm not positive. I just changed my name servers to the new version this morning so I can't verify. The site is amcolan.info. If you follow the link, it should work fine on the first page. Hoover over the image to test. It should enlarge.


This is the script where the widget is instantiated:



require(["dojo/dom",
"dojo/request",
"dojo/dom-form",
"dijit/Dialog",
"dijit/form/Form",
"dijit/form/TextBox",
"dijit/form/ValidationTextBox",
"dijit/form/Textarea",
"dijit/form/Button",
"dojox/validate/web",
"dojo/domReady!"],
function(dom, request, domForm, Dialog, Form, TextBox, ValidationTextBox, TextArea, Button ){

alertDialog = new Dialog({
title: "Email Result",
content: "<p>No Content</p>",
sytle: "width: 50em"
});

emailDialog = new Dialog({
title: "Email Form",
content: dom.byId("emailFormHTML").textContent,
style: "width: 50em"
});

emForm = new Form({
},"contact-form");

toText = new TextBox({
name: "addressee",
readOnly: "readOnly",
value: "Staff"
},"to");

nameText = new ValidationTextBox({
name: "name",
placeholder: "Your Name",
required: "true",
missingMessage: "We'd like to know who you are."
},"name");

emailText = new ValidationTextBox({
name: "email",
placeholder: "Your email",
validator: dojox.validate.isEmailAddress,
invalidMessage: "This is not a valid email address!"
},"email");

messageText = new TextArea({
name: "message",
placeholder: "Your message"
},"message");

sendBtn = new Button({
label: "Send",
onClick: function(){
if (emForm.validate() == true){
// Post the data to the server
request.post("<%= contact_create_path %>",
{data: domForm.toObject("contact-form"),
// Wait 2 seconds for a response
timeout: 4000

}).then(function(response){
emailDialog.hide();
alertDialog.set("content", response)
alertDialog.show();
});
}
}
},"submit-btn");
});
</script>


If I comment out the contact-form widget, it errors out on the next one. Could this have something to do with how Rails handles pages?


Update


I was able to treat the symptom by testing for the widget before creating it like this:



emform = registry.byId("contact-form");
if (!emform){
emForm = new Form({
},"contact-form");}


Once I did this for all the widgets, that error went away. The fix may have caused others. I'd still like to know why I had this problem. Have I missed some fundamental Javascript or dojo concept about the retention of widgets?


Update 2


I thought I got it working, but no. I no longer get any errors. I still have the problem of the Javascript not executing when navigating from another page on the site. Again, if a I reload the page or enter from another web site, it works fine. This is the Javascript that enables the contact link at the bottom of the page:



require(["dojo/on",
"dojo/dom",
"dojo/mouse",
"dojo/dom-style",
"dojo/domReady!"],
function(on, dom, mouse, style)
{
var emailLink = dom.byId("emailClickable");

on(emailLink, "click", function(evt){
toText.set("value","Site Administrator");
emailDialog.show();
});
on(emailLink, mouse.enter, function(evt){
style.set(emailLink, "cursor", "pointer")
});
});


Update 3


I put console.info statements in all the javascript functions. I don't see any messages when I go from page to page within the site. I've also noticed in the Firebug Net tab that the problem only occurs when the page is loaded from cache.





Aucun commentaire:

Enregistrer un commentaire