On Github jetmartin / front-end-for-dummies
Click to set fullscreen.
Be very careful if you use these features, take care of the browser scope !
Even Http headers are changing !
Content-Security-Policy: script-src 'self' https://apis.google.comProtect to XSS, exception made for some domains as "self" & "apis.google.com".
Here is an example of a new kind of web app using WebRTC & X-speech : Talkshow
Using jQuery, you will have two dropdowns The items of the second one depend of the current selected item from the first one The datas are provided by a JSON/REST webservice
The following code is issued from a real project source code history
The commits are scheduled on several days, that means it seems to be a big job
35 lines of code
; (function ($) { $.fn.listbox = function () { this.each(function () { $(this).bind("change", listBox); }); return $(this); }; function listBox(events, object) { var trigger = events ? $(this) : object; var source = trigger.attr("data-listbox-source") == "" ? $(this) : $(trigger.attr("data-listbox-source")); if (trigger.attr("data-listbox-target") != "") { var target = $(trigger.attr("data-listbox-target")); var url = trigger.attr("data-listbox-url"); $.ajax({ url: url, data: { id: source.val() }, success: function (data) { format(target, data); } }); } } function format(target, data) { target.empty(); $.each(data, function (index, item) { target.append("<option value='" + item.Value + "'>" + item.Text + "</option>"); }); } $(document).ready(function () { $('select.listbox').listbox(); }); })(jQuery)
45 lines of code
; (function ($) { $.fn.LinkedListbox = function () { function linkedListbox(events, object) { var trigger = events ? $(this) : object; var source = trigger.attr("data-listbox-source") == "" ? $(this) : $(trigger.attr("data-listbox-source")); if (trigger.attr("data-listbox-target") != "") { var target = $(trigger.attr("data-listbox-target")); var url = trigger.attr("data-listbox-url"); $.ajax({ url: url, data: { id: source.val() }, success: function (data) { format(target, data); } }); } } function format(target, data) { target.empty(); if (data.length > 0) { target.removeAttr('disabled'); $.each(data, function (index, item) { target.append("<option value='" + item.Value + "'>" + item.Text + "</option>"); }); } else { target.attr('disabled', 'disabled'); } } this.each(function () { $(this).bind("change", linkedListbox); }); return $(this); }; $(document).ready(function () { $('select.listbox').LinkedListbox(); }); })(jQuery)
... each release doesn't matter ...
I will not review the code but talk about the main frame
Crap ! Your function name already exists : plugins.jquery.com/listbox/
A simple search : "select ajax" on plugins.jquery.com.
Uh ! I guess it already exists... it's at least very close : plugins.jquery.com/chained/
I can try the demo to be sure that this is what I need No, try the demo, it will make sure that it's what you need and looking at the demo code will save your time the first time
I've just saved my time and the code is already tested and documented ! Using apropriate search, I've spent less than 10 minutes (including testing)
; (function ($) { $.fn.listbox = function () { }; function listBox(events, object) { var trigger = [...]; } function format(target, data) { } $(document).ready(function () { $('select.listbox').listbox(); }); })(jQuery)
Oh ? any comments ? so sad...
A variable named "trigger" ? It's a little confusing, isn't it ?
You can see the developer has taken some time to look at the jQuery doc : "How to made a plugin". but...
These functions are not part of the plugin... take care of common names such as "format", some other tool may use it...
The event listener is part of the plugin ? What happens if you want to use it elsewhere ? use AJAX ?
Did you think about it : could it be on multiples elements ? Will the event affect a single one or all of them ?
; (function ($) { $.fn.LinkedListbox = function () { function linkedListbox(events, object) { } function format(target, data) { } }; $(document).ready(function () { $('select.listbox').LinkedListbox(); }); })(jQuery)
Still no comments...
Now the functions are part of the plugin itself. No risk of conflict
There is finally a V3 for having a single event listener out of the plugin :
$(document).ready(function () { $('select.listbox').on('change', function() { $(this).LinkedListbox(); }); });