function convert(button,cmap){
var form = $(button).parent();
var value = form.children("input[name='from']").val();
var f = form.children("select[name='tounit']").children("option:selected").val();
var t = form.children("select[name='fromunit']").children("option:selected").val();
alert(value+' '+f+'_'+t);
var result = value;
if(f != t){
var c=cmap[f+'_'+t];
alert(c);
result=parseFloat(value)*c;
if (isNaN(result)){
result = "unknown conversion factor";
}else{
result = result.toFixed(4);
}
}
form.children("input[name='to']").val(result);
};
jQuery.fn.unitconverter = function(options){
var cmap = $.extend({},$.fn.unitconverter.conversion_map,options);
var from = new Array();
var to = new Array();
for (var key in cmap){
var units = key.split("_");
from.push(units[0]);
to.push(units[1]);
}
var id = "unitconverter"+new String(Math.floor(Math.random() * 255 * 255));
var html = '<form id="'+id+'"><input name="from" type="text" value="1" />';
html += '<select name="fromunit">';
html += '<option selected="true">'+from.pop()+'</option>';
var len = from.length;
for (var i=0; i<len; i++){ html += '<option>'+from.pop()+'</option>'};
html += '</select> = ';
html += '<input name="to" type="text" readonly="true" />';
html += '<select name="tounit">';
html += '<option selected="true">'+to.pop()+'</option>';
var len = to.length;
for (var i=0; i<len; i++){ html += '<option>'+to.pop()+'</option>'};
html += '</select>';
html += '<button name="convert" type="button">convert</button></form>';
//alert(html);
this.append(html);
$("#"+id+" button").button({
icons: {
primary: 'ui-icon-refresh'
},
text: false
}).click(function(){return convert(this,cmap);});
$("#"+id).css('float','left');
$("#"+id).css('background-color',$("#"+id+" button").css('background-color'));
$("#"+id).css('background-image',$("#"+id+" button").css('background-image'));
$("#"+id).css('background-repeat',$("#"+id+" button").css('background-repeat'));
$("#"+id).addClass("ui-widget");
return this;
};
jQuery.fn.unitconverter.conversion_map = {
"inch_cm":1.0/2.54,
"cm_inch":2.54
}
return convert() vs. convert()
Page 1 of 16 Replies - 274 Views - Last Post: 26 November 2012 - 02:35 AM
#1
return convert() vs. convert()
Posted 25 November 2012 - 02:46 PM
I am learning jQuery UI and encountered the below code. I just don't understand the part (in line 59), why not just invoke convert(), instead of returning convert()? I think convert() has already done its job before reaching its returning point. Many thanks.
Replies To: return convert() vs. convert()
#2
Re: return convert() vs. convert()
Posted 25 November 2012 - 11:10 PM
IIRC this has something to do with jQuery’s event handling. by returning true/false you can cancel the event (would have to look it up to say for sure).
#3
Re: return convert() vs. convert()
Posted 25 November 2012 - 11:18 PM
Dormilich, on 25 November 2012 - 11:10 PM, said:
IIRC this has something to do with jQuery’s event handling. by returning true/false you can cancel the event (would have to look it up to say for sure).
Thanks. But convert() really return nothing, so by default the value would be 'undefined'. If my understanding is correct, returning an 'undefined' would be 'false', so it will automatically inhibit the .click method?
#4
Re: return convert() vs. convert()
Posted 25 November 2012 - 11:57 PM
can’t tell for sure, I haven’t done jQuery events for some time, I too would have to google for that.
#5
Re: return convert() vs. convert()
Posted 26 November 2012 - 02:16 AM
The following is based only on my memory, a highly unreliable source:
When you return false from it, IIRC you suppress the default action, AND bubbling.jQuery is in this case doing it different than DOM event handlers which would be I think to suppress the default action, but not the bubbling. see post below.
Ugh, these things elude me, and to think it wasn't all that long that I dabbled with jQuery.
Yup, see: http://api.jquery.com/bind/
Returning true should have no effect, as would returning nothing.
When you return false from it, IIRC you suppress the default action, AND bubbling.
Ugh, these things elude me, and to think it wasn't all that long that I dabbled with jQuery.
Yup, see: http://api.jquery.com/bind/
Quote
Returning false from a handler is equivalent to calling both .preventDefault() and .stopPropagation() on the event object.
This post has been edited by Xupicor: 26 November 2012 - 02:33 AM
#6
Re: return convert() vs. convert()
Posted 26 November 2012 - 02:18 AM
DOM Event handlers are totally unimpressed by any return value. the only time it matters is when you use the HTML event attributes, where it indeed suppresses the default action.
#7
Re: return convert() vs. convert()
Posted 26 November 2012 - 02:35 AM
Ah, well I think I mixed something up with dispatchEvent then. Thanks for the info.
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote




|