Ask a Jedi: Two jQuery and ColdFusion questions
Syed sent some interesting questions regarding jQuery and ColdFusion. I will attempt to answer them, but I will remind people that I've only begun to play with jQuery. I'm far from being an expert. Anyway, his questions:
Hi Reymond, I've been trying to make cfform work with jquery, i think you were able to with the code below:
$.post("sendcontact.cfm",{dname:$("#dname").val(),demail:$("#demail").val(),comm ents:$("#comments").val()},formDone);For some reason, i cant get it to work. I actually have the cfform under jquery Tabs, which should submit the form without loading, like it was doing under cf ajax feature.
So this isn't much of an answer, but I'd be willing to bet the JavaScript that cfform is spitting out is conflicting with jQuery. I would not mix the two. I'm not saying they can't be mixed, but I just wouldn't do it.
I'm trying to convert from cf ajax to jquery. The only problems i'm having is with the link within the tabs and forms.
How did you replace ajaxlink() in jquery?
Ah, now thats an interesting one. The ajaxLink() function in ColdFusion basically says, "Load URL X into the current container." So to do a basic "Load X into container Y" in jQuery, you can just do:
$("##content").load(someurl);
Nice and simple, but note that I had to specify a 'block' to load the content into. You could simply write a helper function. I haven't tested this, so forgive any typos:
function go(container,url) {
$(container).load(url);
}
<div id="content">
<a href="javascript:go('content','someurl.cfm')>Load it like you mean it</a>
</div>
I'm sure a simpler solution exists. You could probably do it inline as well, but I find that a bit nicer to read myself. I'm not a big fan of making things shorter just to save typing. That's one of my complaints about jQuery. I like the power of it - but sometimes I almost wish it had a bit more code to it to make it readable. (Blasphemy, I know.)
Comments
$(document).ready(function () {
$("a[class=loadHere]").click(function() {
var thisLink= $(this);
//Load the page defined in the href attribute of the link into the parent container
thisLink.parent().load(thisLink.attr("href"));
//Cancel the normal click action
return false;
});
});
...
<div>
<a class="loadHere" href="someurl.cfm">Load it like you mean it</a>
</div>
This approach works as long as the hyperlink isn't enclosed in a different container from the one you want to populate with the results of the HTTP call: if you changed the above example and put the hyperlink inside of a <p></p> block, then the content would be loaded in that paragraph block instead.
http://ui.jquery.com/
They are currently in RC1 for the 1.5 release which is going to be awesome. Their website is somewhat halfway built right now, but they are supposed to release the 1.5 on the 8th.
You can read more about the release here:
http://jquery.com/blog/2008/06/02/jquery-ui-15-rel...
Thank you Raymond!
Sorry for misspelling your name in the email.
$( "#actionmsg" ).html("<span style='color:red;'>Question deleted and data updated!</span>")
.show()
.fadeIn('slow')
.animate({opacity: 1.0}, 3000)
.fadeOut('slow');
Notice how it's not all on 1 line and is very readable. You can also do this:
var myObj = $( "#actionmsg" );
myObj.html("<span style='color:red;'>Question deleted and data updated!</span>");
myObj.show();
myObj.fadeIn('slow');
myObj.animate({opacity: 1.0}, 3000);
myObj.fadeOut('slow');
Again, you have flexibility to write it in either way.
Hope this helps. :)
So I'm being picky now - and I know it - but as a JS newbie I found this to be very unfriendly when I saw it.
example:
javascript:ColdFusion.navigate('/modules/payroll.cfm','pay');
where '/modules' is a secured directory.
tried cf mapping and move to 1 level above the wwwroot, but the bind attributes for cfdiv etc do not recognized maps.
At the same time, if /modules is a CF mapping, and NOT a web server mapping, then again - because AJAX == HTTP, you won't be able to view it. Remember, if you can't open your browser and manually go to yoursite.com/modules/payroll.cfm, than Ajax requests won't be able to as well.

