[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Can someone tell if this is javascript or else (jquery)?

pascal baro

2/14/2016 10:49:00 PM

new (function() {
var jqsdElements = { };

this.changeActiveState = function(obj, mode, enabled) {
switch (mode) {
case 'hide':
if (enabled) {
obj.show();
} else {
obj.hide();
}
break;
case 'disable':
default:
if (enabled) {
obj.attr('disabled', false);
} else {
obj.attr('disabled', 'disabled');
}
break;
}
};

this.disableChildren = function(node_id) {
if (node_id in jqsdElements) {
for (var dependent in jqsdElements[node_id]) {
var el = $('#' + dependent);
var mode = el.attr('jqsd:hide_mode');
el.find(':nth-child(1)').prop('selected',true);
this.changeActiveState(el, mode, false);
this.disableChildren(dependent);
}
}
};

this.onChange = function(id, val) {
for (var dependent in jqsdElements[id]) {
var dObj = $('#' + dependent);
var keys = ['_ALL_'];

if (val in jqsdElements[id][dependent]['options']) {
keys[keys.length] = val;
}

dObj.find('option').remove();

var totalOps = 0;
var opsToAdd = { };

for (var i=0;i<keys.length;i++) {
var key = keys[i];
for (var optOrder in jqsdElements[id][dependent]['options'][key]) {
var opt = jqsdElements[id][dependent]['options'][key][optOrder];
opsToAdd[optOrder] = opt;
totalOps++;
}
}

if (Object.keys) {
var reOrder = opsToAdd, keys = Object.keys(opsToAdd), i, len = keys.length;
keys.sort();

for (i = 0; i < len; i++) {
k = keys[i];
dObj.append(opsToAdd[k]);
}
} else {
for (var orderKey in opsToAdd) {
dObj.append(opsToAdd[orderKey]);
}
}

dObj.find(':nth-child(1)').prop('selected',true);

this.disableChildren(dependent);

if (totalOps > 0) {
if (totalOps == 1) {
if (dObj.find(':nth-child(1)').val() != '') {
this.changeActiveState(dObj, jqsdElements[id][dependent].hidemode, true);
}
} else {
this.changeActiveState(dObj, jqsdElements[id][dependent].hidemode, true);
}
}
}
};

this.init = function() {
var p = this;
$('select[jqsd\\:depends_on]').each(function() {
var selId = $(this).attr('id');

if (!selId) {
alert('JQSD Error - One or more selects are missing id values');
return;
}

var hideMode = ($(this).attr('jqsd:hide_mode')) ? $(this).attr('jqsd:hide_mode') : 'disable';
var depends_on = $(this).attr('jqsd:depends_on');

p.changeActiveState($(this), hideMode, false);

if ($('#' + depends_on).length == 0) {
alert('JQSD Error - One or more selects are missing id values');
return;
}

var options = {
'_ALL_': { }
};

var optOrder = 0;
$(this).find('option').each(function() {
var rel = $(this).attr('rel');
if (!rel) {
options['_ALL_'][optOrder] = $(this);
} else {
var opts = rel.split(',');
for (var i=0;i<opts.length;i++) {
if (!options[opts[i]]) {
options[opts[i]] = [ ];
}

options[opts[i]][optOrder] = $(this);
}
}

optOrder++;
});

if (!jqsdElements[depends_on]) {
jqsdElements[depends_on] = { };
}

jqsdElements[depends_on][selId] = {
options: options,
hidemode: hideMode
};

p.onChange(depends_on, $('#' + depends_on).val());
});

$('select').on('change', $.proxy(function(evt) {
var id = $(evt.target).attr('id');

if (!(id in jqsdElements)) {
return;
}

var val = $(evt.target).val();

this.onChange(id, val);
}, this));
}

this.preInit = function() {
$(document).ready($.proxy(this.init, this));
}

})().preInit();
27 Answers

Stefan Weiss

2/14/2016 11:05:00 PM

0

On 02/14/2016 23:49, bpascal123 wrote:
> new (function() {
> var jqsdElements = { };
[snip]

Yes, this is JavaScript, almost certainly using jQuery (which is a
library written in JavaScript).

- stefan

pascal baro

2/14/2016 11:26:00 PM

0

Thanks, I think I know what jQuery is. So in other words, this script written using javascript syntax uses (calls) jQuery objects or methods?

Stefan Weiss

2/15/2016 12:23:00 AM

0

On 02/15/2016 00:26, bpascal123 wrote:
> Thanks, I think I know what jQuery is. So in other words, this script
> written using javascript syntax uses (calls) jQuery objects or methods?

Yes, that's correct.

- stefan

Michael Haufe (\"TNO\")

2/15/2016 1:41:00 PM

0

On Sunday, February 14, 2016 at 5:26:24 PM UTC-6, bpascal123 wrote:
> Thanks, I think I know what jQuery is. So in other words, this script written using javascript syntax uses (calls) jQuery objects or methods?


If that was true, then you should be aware that there exists a a property called $.jquery you can test for

Thomas 'PointedEars' Lahn

2/15/2016 7:00:00 PM

0

Stefan Weiss wrote:

> On 02/15/2016 00:26, bpascal123 wrote:
>> Thanks, I think I know what jQuery is. So in other words, this script
>> written using javascript syntax uses (calls) jQuery objects or methods?
>
> Yes, that's correct.

Aside from the fact that there is no â??javascript syntaxâ? as there is no
â??javascriptâ?.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

2/15/2016 7:00:00 PM

0

Stefan Weiss wrote:

> On 02/15/2016 00:26, bpascal123 wrote:
>> Thanks, I think I know what jQuery is. So in other words, this script
>> written using javascript syntax uses (calls) jQuery objects or methods?
>
> Yes, that's correct.

Aside from the fract that there is no â??javascript syntaxâ? as there is no
â??javascriptâ?.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

pascal baro

2/16/2016 12:59:00 AM

0

So JQSD is likely to stand for some jsquery methods...?

Another question if you have the time for a short answer

Is it possible to achieve the same result of this script (3 dependant drop down validation boxes) using javascript without any jquery.

I can see javascript is evolving fast with objects of its own, before these were functions and now that there are formal objects and constructors. Can we achieve things like this script that have nothing to do with responding design jquery is best for (please tell me if I'm wrong) but just simple DOM elements to manipulate data? Javascript is becoming good at it. However, still a long road to Microsoft Excel spreadsheets, my daily tool.

Thanks

Pascal

Stefan Weiss

2/16/2016 3:20:00 AM

0

On 02/16/2016 01:59, bpascal123 wrote:
> So JQSD is likely to stand for some jsquery methods...?

Possibly. I don't know anything called JQSD, so I can't comment on that.

> Is it possible to achieve the same result of this script (3 dependant
> drop down validation boxes) using javascript without any jquery.

jQuery is just JavaScript; anything you can do with it you can also do
without it. Maybe not as conveniently, but you can always use other
toolkits or write your own custom tools if you feel like it.

I don't know what the script you posted does, and I'm too tired to read
it at the moment. It would help if you could explain where you found it
and what you're trying to accomplish.

> I can see javascript is evolving fast with objects of its own, before
> these were functions and now that there are formal objects and
> constructors.

Objects and constructors have been basic building blocks in JavaScript
from the start.

> Can we achieve things like this script that have
> nothing to do with responding design jquery is best for (please tell
> me if I'm wrong) but just simple DOM elements to manipulate data?
> Javascript is becoming good at it. However, still a long road to
> Microsoft Excel spreadsheets, my daily tool.

Spreadsheets have been written as browser-based components with
JavaScript before (see Google Docs, for example). I can tell you from
experience that developing something like Excel in HTML+JS is not
trivial at all. Granted, it's almost been 10 years since I took a stab
at it, and things have improved a lot since then, but today I wouldn't
start a project like that without a *very* good reason. There are plenty
of ready-made components to choose from, if you just want to use the
spreadsheet (as opposed to programming it yourself). Many of them depend
on external libraries like jQuery etc.

As for what jQuery is best for... most of the regulars in this group are
heavily biased against jQuery, so you're unlikely to get a straight
answer here :)
Still, if it's a plain JS library (like jQuery), you can do everything
it does with plain JS yourself. General purpose libraries can make
certain tasks easier, and they often have an ecosystem of plugins (of
varying quality) to choose from. I can't give any recommendations either
way, until I know more about your level of experience with JavaScript
and programming in general, and what you're trying to create.


- stefan

Michael Haufe (\"TNO\")

2/16/2016 6:19:00 AM

0

On Monday, February 15, 2016 at 9:20:20 PM UTC-6, Stefan Weiss wrote:

> Spreadsheets have been written as browser-based components with
> JavaScript before (see Google Docs, for example). I can tell you from
> experience that developing something like Excel in HTML+JS is not
> trivial at all. Granted, it's almost been 10 years since I took a stab
> at it, and things have improved a lot since then, but today I wouldn't
> start a project like that without a *very* good reason.

I created a WYSIWYG editor for an email campaign manager just 3 years ago, and you're right. Even today something as reduced as a basic Excel-like editor is non-trivial. You frankly can not rely on Content Editable and have to do nearly everything from scratch to get the desired results at scale. For good or for ill at least one learns quite a lot about the hairiness of table layout...

Thomas 'PointedEars' Lahn

2/16/2016 7:37:00 AM

0

Stefan Weiss wrote:

> As for what jQuery is best for... most of the regulars in this group are
> heavily biased against jQuery, so you're unlikely to get a straight
> answer here :)
> Still, if it's a plain JS library (like jQuery), you can do everything
> it does with plain JS yourself.

No, because "JS" (here short for the proper term â??ECMAScript
implementations�) does not include APIs like the DOM that are necessary to
achieve this. Those are provided by the respective host environment and can
be used with "JS" (and other programming languages) either directly or
through libraries like jQuery that wrap this accessing capability for
developer convenience.

The problem with jQuery and the likes is not that you can live without them,
but that in their design, convenience has been given greater priority than
runtime and memory efficiency and backwards compatibility (marketing calls
this problem â??write less, do moreâ?), and that as APIs converge and new CSS
features emerge, an increasing number of features more conveniently provided
by jQuery and the likes becomes unnecessary to use to achieve something.
For example, simple transitions are better written in CSS directly nowadays.

The problem with jQuery and the likes is that you learn to do things a
certain way (with CSS-like selectors) which will make it harder for you to
even consider alternatives when they are better than the jQuery-ish way.

<http://youmightnotneedjquer...

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.