Upgrade to Pro — share decks privately, control downloads, hide ads and more …

jQuery Tool Objects

othree
August 19, 2012

jQuery Tool Objects

othree

August 19, 2012
Tweet

More Decks by othree

Other Decks in Technology

Transcript

  1. Real World left +=10 left +=10 left +=10 left +=10

    left +=10 10ms 25ms 41ms 53ms 68ms
  2. var fadeOut = function (target) {! target.style.opacity -= 0.1;! if

    (target.style.opacity > 0) {! setTimeout(fadeIn, 10);! }! };! ! var fadeIn = function (target) {! target.style.opacity += 0.1;! if (target.style.opacity < 1) {! setTimeout(fadeIn, 10);! }! };
  3. ... • Not fade in • Infinity loop • If

    we use time period on each call • Not infinity loop • Animation is ugly
  4. Animate and Queue • .animate always use ‘fx’ as its

    queue name • .queue has default queue name: ‘fx’
  5. If Boss Want • Fade Out • Change content •

    Fade In • Move right 500 pixel • Add class ‘active’ • Move left 500 pixel • Change content again
  6. Callbacks $('#id').fadeOut(function () {! $(this).html('new content');! $(this).fadeIn(function () {! $(this).animate({left:

    '+=500'}, function () {! $(this).addClass('active');! $(this).animate({left: '-=500'}, function () {! $(this).html('even new content');! });! });! });! });
  7. Queue $('#id')! .fadeOut()! .queue(function () {! $(this).html('new content');! $(this).dequeue();! })!

    .fadeIn()! .animate({left: '+=500'})! .queue(function () {! $(this).addClass('active');! $(this).dequeue();! })! .animate({left: '-=500'})! .queue(function () {! $(this).html('even new content');! $(this).dequeue();! });
  8. Advantages • Less indent • Delay or clear queue is

    much easier • Better for maintain • What happens if boss want to change spec
  9. $('#A')! .fadeOut()! .queue(function () {! var $that = $(this);! $('#B').fadeOut(function

    () {! $that.dequeue();! });! })! .queue(function () {! var $that = $(this);! $('#C').fadeOut(function () {! $that.dequeue();! });! });
  10. $({}).queue(function () {! var $that = $(this);! $('#A').fadeOut(function () {!

    $that.dequeue();! });! }).queue(function () {! var $that = $(this);! $('#B').fadeOut(function () {! $that.dequeue();! });! }).queue(function () {! var $that = $(this);! $('#C').fadeOut(function () {! $that.dequeue();! });! });
  11. History • a.k.a Promise • Idea since 1976 (Call by

    future) • Dojo 0.9 (2007), 1.5 (2010) • jQuery 1.5 (2011) • CommonJS Promises/A
  12. What is Deferred • In computer science, future, promise, and

    delay refer to constructs used for synchronization in some concurrent programming languages. http://en.wikipedia.org/wiki/Futures_and_promises
  13. jQuery Deferred • Multiple handler functions • Register handlers at

    any time • jQuery.when http://api.jquery.com/category/deferred-object/
  14. Example: Image Loader function imgLoader(src) {! var _img = new

    Image(),! _dfd = $.Deferred();! ! _img.onload = _dfd.resolve; //success! _img.onerror = _dfd.reject; //fail! ! _img.src = src! ! return _dfd.promise();! }
  15. Use Image Loader var logo_dfd = imgLoader('/images/logo.png');! ! logo_dfd.done(function ()

    {! $('#logo').fadeIn();! }).fail(function () {! document.location = '/404.html';! });! ! //Some place else! logo_dfd.done(function () {! App.init();! });! ! //Another place ! logo_dfd.fail(function () {! App.destroy();! });
  16. var account = $.getJSON('/api/account');! var device = account.pipe(function () {!

    $.getJSON('/api/device');! });! ! var data = $.when(account, device).pipe(function () {! $.getJSON('/api/data');! });! ! $.when(account, device, data).done(render);
  17. How to Use function fn( value ){! console.log( value );!

    }! ! var callbacks = $.Callbacks();! callbacks.add( fn );! callbacks.fire( "foo!" );
  18. Methods • add • remove • has • fire •

    fireWith • fired • lock • locked • disable • disabled • empty
  19. once function fn1( value ) {! console.log( value );! }!

    ! var callbacks = $.Callbacks('once');! callbacks.add( fn1 );! callbacks.fire( "foo!" ); // foo!! callbacks.fire( "foo!" ); //
  20. memory function fn1( value ) {! console.log( value );! }!

    function fn2() {! console.log( '2' );! }! ! var callbacks = $.Callbacks('memory');! callbacks.add( fn1 );! callbacks.fire( "foo!" ); // foo!! callbacks.add( fn1 ); // foo!! callbacks.add( fn2 ); // 2! callbacks.fire( "bar!" ); // bar!, bar!, 2
  21. once memory function fn1( value ) {! console.log( value );!

    }! function fn2() {! console.log( '2' );! }! ! var callbacks = $.Callbacks('once memory');! callbacks.add( fn1 );! callbacks.fire( "foo!" ); // foo!! callbacks.add( fn1 ); // foo!! callbacks.add( fn2 ); // 2! callbacks.fire( "bar!" ); //
  22. unique function fn1( value ) {! console.log( value );! }!

    function fn2() {! console.log( '2' );! }! ! var callbacks = $.Callbacks('unique');! callbacks.add( fn1 );! callbacks.add( fn1 ); ! callbacks.add( fn2 );! callbacks.fire( "foo!" ); // foo!, 2
  23. When to Use • If you have to manage callback

    functions • If you need any feature of the flags • If you want to break down your codes
  24. PubSub var topics = {};! ! jQuery.Topic = function( id

    ) {! var callbacks,! topic = id && topics[ id ];! if ( !topic ) {! callbacks = jQuery.Callbacks();! topic = {! publish: callbacks.fire,! subscribe: callbacks.add,! unsubscribe: callbacks.remove! };! if ( id ) {! topics[ id ] = topic;! }! }! return topic;! }; http://addyosmani.com/blog/jquery-1-7s-callbacks-feature-demystified/