VirtueMart Forum

VirtueMart 2 + 3 => General Questions => Topic started by: 2dmaster on January 02, 2016, 11:50:02 am

Title: No cleanup js code in VM 3
Post by: 2dmaster on January 02, 2016, 11:50:02 am
Virtuemart so have bad js code;  issue with javascript errors when using javascript compressors/combiners/cachers.
tested with jch optimize pro
Title: Re: No cleanup js code in VM 3
Post by: GJC Web Design on January 02, 2016, 11:55:09 am
Your point is?

Are you going to say what you think is bad about it and suggest improvements?
Title: Re: No cleanup js code in VM 3
Post by: PRO on January 02, 2016, 14:15:16 pm
I use jchoptimize

& exclude these files

jquery.min.js
jquery-noconflict.js
jquery-migrate.min.js
Title: Re: No cleanup js code in VM 3
Post by: Studio 42 on January 02, 2016, 22:35:56 pm
Try to check the VM functions, some have no local names(certainly some mistakes), modules\mod_virtuemart_cart\assets\js\update_cart.js
Code: [Select]
if (typeof Virtuemart === "undefined")
Virtuemart = {};

jQuery(function($) {
Virtuemart.customUpdateVirtueMartCartModule = function(el, options){
var base = this;
var $this = $(this);
base.$el = $(".vmCartModule");

base.options = $.extend({}, Virtuemart.customUpdateVirtueMartCartModule.defaults, options);

base.init = function(){
$.ajaxSetup({ cache: false })
$.getJSON(window.vmSiteurl + "index.php?option=com_virtuemart&nosef=1&view=cart&task=viewJS&format=json" + window.vmLang,
function (datas, textStatus) {
base.$el.each(function( index ,  module ) {
if (datas.totalProduct > 0) {
$(module).find(".vm_cart_products").html("");
$.each(datas.products, function (key, val) {
//jQuery("#hiddencontainer .vmcontainer").clone().appendTo(".vmcontainer .vm_cart_products");
$(module).find(".hiddencontainer .vmcontainer .product_row").clone().appendTo( $(module).find(".vm_cart_products") );
$.each(val, function (key, val) {
$(module).find(".vm_cart_products ." + key).last().html(val);
});
});
}
$(module).find(".show_cart").html( datas.cart_show);
$(module).find(".total_products").html( datas.totalProductTxt);
$(module).find(".total").html( datas.billTotal);
});
}
);
};
base.init();
};
// Definition Of Defaults
Virtuemart.customUpdateVirtueMartCartModule.defaults = {
name1: 'value1'
};

});

jQuery(document).ready(function( $ ) {
jQuery(document).off("updateVirtueMartCartModule","body",Virtuemart.customUpdateVirtueMartCartModule);
jQuery(document).on("updateVirtueMartCartModule","body",Virtuemart.customUpdateVirtueMartCartModule);
});

Ths is only to help the developpers(i don't know what dev do the javascript currently, but review yourself.
Two time same function for nothing and not using same code.
jQuery(function($) {  == jQuery(document).ready(function( $ ) {

Code: [Select]
var base = this;
var $this = $(this);
base.$el = $(".vmCartModule");
Why base=this and $this = $(this) ?
Why base.$el    = $(".vmCartModule");, using $el = $(".vmCartModule"); do the same effect inside the getJSON if you replace base.$el with $el

I have see in other function missing var before some local variables. So if you try to compres it, this fail.

SO the most valid code here is :
Code: [Select]
if (typeof Virtuemart === "undefined")
Virtuemart = {};

jQuery(document).ready(function( $ ) {
Virtuemart.customUpdateVirtueMartCartModule = function(el, options){
var base = this,
$el = $(".vmCartModule");

base.options = $.extend({}, Virtuemart.customUpdateVirtueMartCartModule.defaults, options);

base.init = function(){
$.ajaxSetup({ cache: false })
$.getJSON(vmSiteurl + "index.php?option=com_virtuemart&nosef=1&view=cart&task=viewJS&format=json" + vmLang,
function (datas, textStatus) {
$el.each(function( index ,  module ) {
var $mod = $(module);
if (datas.totalProduct > 0) {
$mod.find(".vm_cart_products").html("");
$.each(datas.products, function (key, val) {
$mod.find(".hiddencontainer .vmcontainer .product_row").clone().appendTo( $mod.find(".vm_cart_products") );
$.each(val, function (key, val) {
$mod.find(".vm_cart_products ." + key).last().html(val);
});
});
}
$mod.find(".show_cart").html( datas.cart_show);
$mod.find(".total_products").html( datas.totalProductTxt);
$mod.find(".total").html( datas.billTotal);
});
}
);
};
base.init();
};
jQuery(document).off("updateVirtueMartCartModule","body",Virtuemart.customUpdateVirtueMartCartModule)
.on("updateVirtueMartCartModule","body",Virtuemart.customUpdateVirtueMartCartModule);
});

But in all, this is not completly optimise because in case of 2 modules or more, the $el.each loop get called 2 times.
So i only hope a day that someone review a little the javascript code, because many new mistake are in VM 3.0.x

I do not want to denigrate the work of developer, but i think some review is needed in the javascripts, if you paste this code in jslint for eg. all this mistake get immediatly displayed as errors.

Regards,
Patrick
Title: Re: No cleanup js code in VM 3
Post by: Milbo on January 04, 2016, 15:03:52 pm
2dMaster

you try to minify a minified version. Do you understand your error? and Patrick, the js code basis is yours. lol you talk about your own errors.
Title: Re: No cleanup js code in VM 3
Post by: Studio 42 on January 04, 2016, 22:18:39 pm
Max, this code is new and don't existed before.
And i'm not anymore in developper team. If i report error, is this so complicate to check.
If you find this is right so, then leave as it is.
All days i find a bug and not only in VM, in other component too.
I have reported 3 or 4 bugs, in onepage checkout, same in Joomla this year. In some plugins and extention my customer paid for.
One of them i had to rewrite most of the code. I had no problem, all developper say me thanks, but you always come for a old code from 2013, this is now 3 year old.
All do errors, you and me too! Simply check the current security fix in Joomla!

Regards,
Patrick