Advanced jQuery
- 4. The Plan
•not just Tips &
Tricks but, yeah,
some of those
•overlooked jQuery
features
•coding the jQuery
- 10. end() Command
$('.menu').find('li')
.filter('.current')
<div class=menu>
<ul>
<li class=current>New</li>
<li>Open...</li>
<li>Print...</li>
</ul>
</div>
<div class=menu>
...
</div>
- 11. end() Command
$('.menu').find('li')
.filter('.current')
.end()
<div class=menu>
<ul>
<li class=current>New</li>
<li>Open...</li>
<li>Print...</li>
</ul>
</div>
<div class=menu>
...
</div>
- 12. end() Command
$('.menu').find('li')
.filter('.current')
.end().end()
<div class=menu>
<ul>
<li class=current>New</li>
<li>Open...</li>
<li>Print...</li>
</ul>
</div>
<div class=menu>
...
</div>
- 20. data() Storage
var myForm = $('#myform')[0];
var formResult = {
errors: validateForm(myForm),
form: myForm
};
myForm.result = formResult;
//later...
var result = $('#myform')[0].result;
if(result.errors.length){
alert('Fix the errors.');
}
- 21. data() Storage
var formResult = {
errors: validateForm(myForm),
form: $('#myform')
};
$('#myform').data('result', formResult);
//later...
var result = $('#myform').data('result');
if(result.errors.length){
alert('Fix the errors.');
}
- 22. data() Storage
<ul>
<li data-movie-id="131">Crash</li>
<li data-movie-id="722">Up</li>
</ul>
var id = $('li:first').data('movieId');
// ==> id == 131
- 25. map() Utility
array-like: has
most of what's
needed to be used
as an array
var arrayLike = {
length: 2,
0: 'first',
1: 'second'
};
- 26. map() Utility
bonus utility:
makeArray()
var arr = $.makeArray(arrayLike);
// ==> ['first','second']
- 27. map() Utility
var users = getUsersArray();
var emails =
$.map(users, function(user, i){
return user.profile.email;
});
- 28. map() Utility
var users = getUsersArray();
var emails =
$.map(users, function(user, i){
return user.profile.email;
});
- 29. map() Utility
the callback
function
•return new value
for the position
•return null to
exclude that position
- 30. re
Quick
ca
•
p
end(): avoid
unnecessary
traversals
•computed
attributes
•relative CSS
attributes
- 34. bind() vs. live()
$('#container .action')
.bind('click', function(){
//do stuff here
});
//same as: $('#container .action')
// .click(function(){...});
//prefer:
$('#container .action')
.live('click', function(){
//do stuff here
});
- 37. live() vs. delegate()
• live(): syntax is
slightly easier
• live(): attaches to
document
• delegate():
attaches closer
- 42. Custom Events
$('#btn1').on('lock', function(){
$(this).prop('disabled', true);
});
//elsewhere, maybe in another js file:
$('#toolbar').on('lock', function(){
$(this).find('.icon').remove();
});
//when appropriate:
$.event.trigger('lock');
- 45. re
Event
ca
•
p
pick the right
binding command
•delegate() over
live()
•on() unifies it all
•decouple with
- 48. Custom Commands
function stripeTable(table, alt) {
$(table)
.find('tr:odd')
.addClass(alt||'alternate');
}
//...
var table = $('#mytable')[0];
stripeTable(table, 'altRow');
- 49. Custom Commands
jQuery.fn.stripe = function(alt) {
this
.children(':odd')
.addClass(alt||'alternate');
return this;
};
//...
$('#mytable').stripe('altRow');
- 50. Custom Commands
jQuery.fn.stripe = function(alt) {
this
.children(':odd')
.addClass(alt||'alternate');
return this;
};
//...
$('#mytable').stripe('altRow');
- 56. Tidy Callbacks
startLongProcess(
function(result){
useResult(result);
logCompletion(result);
},
function(error){
logCompletion(result);
logFailure(error);
}
);
- 58. Tidy Callbacks
var task = startLongProcess()
.done(useResult)
.always(logCompletion)
.fail(logFailure);
doSomethingElse();
//time passes...
task.fail(function(){
alert('error');
});
- 59. Tidy Callbacks
var task = startLongProcess()
.always(logCompletion)
.fail(logFailure);
$.when(task, otherTask)
.then(function(args1, args2){
useResult(args1[0])
.done(updateScreen);
});
- 61. Ajax
var fetch = $.get('/data.aspx')
.done(validateResponse)
.done(doSomething)
.done(doMoreStuff)
.fail(reportError);
fetch.fail(resetUI);
- 62. when() Utility
$.when(
$.get('/somedata'),
$.get('/moredata'),
$.post('/updatedata', data),
someOtherDeferred)
.then(function(){
alert('all done');
});
- 63. promise()
$('.widget')
.first().fadeOut(500).end()
.last().fadeOut(1000).end()
.promise() //default: 'fx' queue
.done(function(){
//do something...
});
- 64. re
Deferred
ca
p
•common way for
callbacks
•returned by jQuery
Ajax
•for command
queues too
- 66. github
https://
github.com/jquery
- 67. github
https://
github.com/jquery
Editor's Notes
- \n
- \n
- \n
- for this talk I'll assume you all know the basics of jQuery and have been using it for DOM manipulation and Ajax calls. I'll show some of the other things in jq that could be labeled as intermediate to advanced. I'll try to highlight anything that is new in 1.7.\n
- for this talk I'll assume you all know the basics of jQuery and have been using it for DOM manipulation and Ajax calls. I'll show some of the other things in jq that could be labeled as intermediate to advanced. I'll try to highlight anything that is new in 1.7.\n
- I normally don't like presentations that are all about tricks and trips because it's hard to keep track of everything. I'll try to keep the machine gun quiet but I still wanted to highlight a handful of helpful gems in jq.\n
- \n
- \n
- \n
- \n
- \n
- DEMO!\n
- \n
- DEMO!\n
- \n
- \n
- \n
- DEMO!\n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- this should be familiar to most of you. The Select() Linq extension does that for IEnumerables in .net. We do the conversion with callback.\n
- this should be familiar to most of you. The Select() Linq extension does that for IEnumerables in .net. We do the conversion with callback.\n
- But there are a few things to note in the callback function.\n
- \n
- Event handling is one of the main reasons we use jquery for...\n
- It makes it so easy to watch for an event of an element... just choose the element and...\n
- Oh, that's that too. It's easy but it takes a little practice to pick the most efficient route. I'm sure a few of us still has to think about it or look up docs to choose the right one for the task.\n
- both bind and live allow us to setup handlers for one or many elements in a single command. Select all the elements you want and attach the handler. main diff: dynamically added elements\n
- unique elements: elements that don't have a similar purpose with any other element on the page.\n
- OK, it was easy to figure out between the previous 2. But those two appear to do the same. Attach a handler for a selector.\nDiff: delegate() attaches a handler to the parent, not Doc. Syntax.\n
- \n
- on() can also take a data parameter that goes to event.data\n
- \n
- \n
- global events\n
- \n
- DEMO!\n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- DEMO!\n
- \n
- \n
- \n
- this is possible way to invoke a callback-based async function... note the two different callbacks, for success and error. note the duplication too.\n
- it could get worse. look what would happen if useResult was also async... what if an error happens in useResult? another error callback?\n
- see how much cleaner the code becomes when we introduce the deferred task object.\nWe can even subscribe later and it will fire immediately if the task has completed already. DEMO1!\n
- the tasks allow us to compose more complicated scenarios without sacrificing too much the read-ability of the code.\n
- \n
- \n
- DEMO2!\n
- Internally jQuery can maintain queues of actions. One such queue is the one that allows effects to execute sequentially.\nThe promise command returns a deferred task that resolves when the queue empties.\n
- \n
- \n
- \n
- show github, show how files are split, show local repo, build\n
- \n
- \n
- \n
- \n