News:

Looking for documentation? Take a look on our wiki

Main Menu

No cleanup js code in VM 3

Started by 2dmaster, January 02, 2016, 11:50:02 AM

Previous topic - Next topic

2dmaster

Virtuemart so have bad js code;  issue with javascript errors when using javascript compressors/combiners/cachers.
tested with jch optimize pro

GJC Web Design

Your point is?

Are you going to say what you think is bad about it and suggest improvements?
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

PRO

I use jchoptimize

& exclude these files

jquery.min.js
jquery-noconflict.js
jquery-migrate.min.js

Studio 42

#3
Try to check the VM functions, some have no local names(certainly some mistakes), modules\mod_virtuemart_cart\assets\js\update_cart.js

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( $ ) {

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 :

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

Milbo

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.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Studio 42

#5
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