Що таке відкладені об'єкти?


Відповіді:


101

Відкладений об’єкт

Станом на jQuery 1.5, об'єкт Deferred пропонує спосіб реєструвати декілька зворотних викликів у керованих чергах зворотного виклику, викликати черги зворотного виклику у відповідних випадках та передати стан успіху чи відмови будь-якої синхронної чи асинхронної функції.

Відкладені методи:

  • deferred.done ()
    • Додайте обробники для виклику, коли вирішений об'єкт відкладено.
  • deferred.fail ()
    • Додайте обробники для виклику, коли відхилений об'єкт відкладено.
  • deferred.isRejected ()
    • Визначте, чи відхилено об’єкт відкладеного.
  • deferred.isRessol ()
    • Визначте, чи вирішено відкладений об’єкт.
  • deferred.reject ()
    • Відхиліть відкладений об’єкт і зателефонуйте до будь-яких помилок виклику із заданими аргументами.
  • deferred.rejectWith ()
    • Відхиліть відкладений об’єкт і зателефонуйте до будь-яких помилок виклику із заданим контекстом та аргументами.
  • deferred.resolve ()
    • Вирішіть відкладений об’єкт і зателефонуйте до будь-яких виконаних зворотних зворотів із заданими аргументами.
  • deferred.resolveWith ()
    • Вирішіть відкладений об'єкт та зателефонуйте до будь-яких виконаних зворотних зворотів із заданим контекстом та аргументами.
  • відкладено.then ()
    • Додайте обробники для виклику, коли об'єкт "Відкладений" буде вирішено або відхилено.

Відкладено в дії:

$.get("test.php").done(
    function(){ alert("$.get succeeded"); }
);

$.get("test.php")
    .done(function(){ alert("$.get succeeded"); })
    .fail(function(){ alert("$.get failed!"); });

І схоже, що існуючі зворотні виклики методу ajax () можна сковувати, а не оголошувати в налаштуваннях:

var jqxhr = $.ajax({ url: "example.php" })
    .success(function() { alert("success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

Приклад роботи із публікації в блозі Еріка Гіндса : http://jsfiddle.net/ehynds/Mrqf8/


jqXHR

Станом на jQuery 1.5, метод $ .ajax () повертає об'єкт jXHR, який є надмножиною об'єкта XMLHTTPRequest. Для отримання додаткової інформації дивіться thejXHR розділ запису $ .ajax


З JQUERY 1.5 ЗВ'ЯЗАНО :

ВІДМОВНІ ОБ'ЄКТИ

Поряд із переписанням модуля Ajax була введена нова функція, яка також стала загальнодоступною: Відкладені об’єкти . Цей API дозволяє працювати з поверненими значеннями, які можуть бути не відразу (наприклад, результатом повернення з асинхронного запиту Ajax). Крім того, це дає можливість приєднувати декілька обробників подій (те, що раніше не було можливо в API Ajax).

Крім того, ви можете зробити власні відкладені об'єкти за допомогою відкритого jQuery.Deferred. Більш детальну інформацію про цей API можна знайти в документації " Відкладений об'єкт" .

Ерік Хайндс написав хороший підручник з використання відкладених файлів у jQuery 1.5 .


19
Будь ласка, поясніть більше. Як створити власні відкладені об'єкти. Як вони працюють?
— користувач113716

3
Насправді я серйозно. Це гарне запитання щодо абсолютно нової функції. Я не маю уявлення, як вони працюють, і думаю, що було б добре, якби StackOverflow добре пояснив це питання для тих, хто запитає про це в майбутньому.
— користувач113716

1
оновлення: я думаю, що визначення "відкладено" вище, яке я додав вгорі, дає більш чітке уявлення про те, що він насправді робить. Здається, це більше стосується можливості ланцюга зворотних викликів, а не необхідності їх оголошення в налаштуваннях, переданих у функцію.
— мисливець

1
@Hunter Я також хотів би пояснити, як це працює. Це перше питання по ньому, тому зробіть це гарною відповіддю!
— Райнос

2
є кілька головних переваг: можливість абстрагувати результат можливої ​​задачі на асинхронізацію, можливість прив’язати декілька обробників різних типів, прив’язати обробників до завдання навіть після того, як завдання вирішено, прив’язати результат декількох запитів на асинхронізацію разом, умовно додайте обробники тощо
— ehynds

13

Замість того, щоб сказати вам, що це робить, я покажу вам, що це робить, і поясню це.

Копія пов’язаного джерела jQuery 1.5 із примітками, що пояснюють, що він робить. Я думаю, що коментарі здебільшого вірні.

Це може принести користь

// promiseMethods. These are the methods you get when you ask for a promise.
// A promise is a "read-only" version
// fullMethods = "then done fail resolve resolveWith reject rejectWith isResolve    isRejected promise cancel".split(" ")
// As you can see it removes resolve/reject so you can't actaully trigger a
// anything on the deferred object, only process callbacks when it "finishes".
promiseMethods = "then done fail isResolved isRejected promise".split(" "),

// Create a simple deferred (one callbacks list)
/* Class: _Deferred.
 *  methods: done, resolve, resolveWith, isResolved
 *  internal method: cancel
 *
 *  Basically allows you to attach callbacks with the done method.
 *  Then resolve the deferred action whenever you want with an argument.
 *  All the callbacks added with done will be called with the resolved argument
 *  Any callbacks attached after resolvement will fire immediatly.
 *
 *  resolveWith allows you to set the this scope in the callbacks fired.
 *
 *  isResolved just checks whether it's resolved yet.
 *
 *  cancel blocks resolve/resolveWith from firing. the methods added throug
 *  done will never be called
 */
_Deferred: function () {
    var // callbacks list
    callbacks = [],
        // stored [ context , args ]
        // stores the context & args that .resolve was called with
        fired,
        // to avoid firing when already doing so
        firing,
        // flag to know if the deferred has been cancelled
        // in Deferred cancel gets called after the first resolve call
        cancelled,
        // the deferred itself
        deferred = {

            // done( f1, f2, ...)
            done: function () {
                if (!cancelled) {
                    var args = arguments,
                        i, length,
                        // elem in callback list
                        elem,
                        // type of elem in callback list
                        type,
                        // cached context & args for when done is called
                        // after resolve has been
                        _fired;
                    // If resolve has been called already
                    if (fired) {
                        // mark it locally
                        _fired = fired;
                        // set fired to 0. This is neccesary to handle
                        // how done deals with arrays recursively
                        // only the original .done call handles fired
                        // any that unwrap arrays and call recursively
                        // dont handle the fired.
                        fired = 0;
                    }
                    // for each function append it to the callback list
                    for (i = 0, length = args.length; i < length; i++) {
                        elem = args[i];
                        type = jQuery.type(elem);
                        // if argument is an array then call done recursively
                        // effectively unwraps the array
                        if (type === "array") {
                            // def.done([f1, f2, f3]) goes to
                            // def.done(f1, f2, f3) through the apply
                            deferred.done.apply(deferred, elem);
                        } else if (type === "function") {
                            // if its a function add it to the callbacks
                            callbacks.push(elem);
                        }
                    }
                    // if it's already been resolved then call resolveWith using
                    // the cahced context and arguments to call the callbacks
                    // immediatly
                    if (_fired) {
                        deferred.resolveWith(_fired[0], _fired[1]);
                    }
                }
                return this;
            },

            // resolve with given context and args
            resolveWith: function (context, args) {
                                // if its been cancelled then we can't resolve
                                // if it has fired then we can't fire again
                                // if it's currently firing then we can't fire. This check is
                // there because of the try finally block. It ensures we
                // cant call resolve between the try & finally in the catch phase.
                if (!cancelled && !fired && !firing) {
                    firing = 1;
                    // try block because your calling external callbacks
                    // made by the user which are not bugfree.
                                        // the finally block will always run no matter how bad
                                        // the internal code is.
                    try {
                        while (callbacks[0]) {
                            callbacks.shift().apply(context, args);
                        }
                                        // cache the content and arguments taht have been called
                                        // and set firing to false.
                    } finally {
                        fired = [context, args];
                        firing = 0;
                    }
                }
                return this;
            },

            // resolve with this as context and given arguments
            // just maps to resolveWith, this sets the this scope as normal
            // maps to this.promise which is the read only version of Deferred.
            resolve: function () {
                deferred.resolveWith(jQuery.isFunction(this.promise) ? this.promise() : 
this, arguments);
                return this;
            },

            // Has this deferred been resolved?
            // checks whether it's firing or if it has fired.
            isResolved: function () {
                return !!(firing || fired);
            },

            // Cancels the action. To be used internally
            cancel: function () {
                cancelled = 1;
                callbacks = [];
                return this;
            }
        };

    return deferred;
},
/* Class: Deferred.
 *  methods: then, done, fail, resolve, reject, resolveWith, rejectWith, isResolved, 
isRejected, promise
 *
 *  then is a shortcut for both assigning done & fail in one function.
 *
 *  This one has two underlying lists with different semantic meanings. You
 *  can bind to both the done callbacks and the fail callbacks then either
 *  resolve or reject your Deferred object.
 *
 *  You can check whether it has been resolved or rejected. useful to see
 *  Afterwards which one has happened.
 *
 *  Call .promise to return a new object which doesn't have the resolve/reject
 *  methods on it. This means you can only bind to it and not resolve/reject it.
 *  This is effectively read-only.
 *
 */
// Full fledged deferred (two callbacks list)
Deferred: function (func) {
        // the main deferred which deals with the success callbacks
    var deferred = jQuery._Deferred(),
                // the failure deferred which deals with the rejected callbacks
        failDeferred = jQuery._Deferred(),
                // the read only promise is cached.
        promise;
    // Add errorDeferred methods, then and promise
    jQuery.extend(deferred, {
                // def.then([f1, f2, ...], [g1, g2, ...] is a short hand for
                // def.done([f1, f2, ...])
        // def.fail([g1, g2, ...])
        then: function (doneCallbacks, failCallbacks) {
                        // fail exists here because this code will only run after
                        // deferred has been extended.
            deferred.done(doneCallbacks).fail(failCallbacks);
            return this;
        },
                // map def.fail to the second underlying deferred callback list
                // map all the other methods for rejection/failure to the underlying
                // failDeffered object so that Deferred has two callback lists stored
                // internally.
        fail: failDeferred.done,
        rejectWith: failDeferred.resolveWith,
        reject: failDeferred.resolve,
        isRejected: failDeferred.isResolved,
        // Get a promise for this deferred
        // If obj is provided, the promise aspect is added to the object
                // no clue what to do with "i"
        promise: function (obj, i /* internal */ ) {
                        // if no argument is passed then just extend promise
            if (obj == null) {
                                // if cached return the cache.
                if (promise) {
                    return promise;
                }
                                // set promise & arg to be {}
                promise = obj = {};
            }
                        // for each promiseMethods in the read only promise list
            i = promiseMethods.length;
            while (i--) {
                                // set the deferred method on the object
                obj[promiseMethods[i]] = deferred[promiseMethods[i]];
            }
                        // returns the "read-only" deferred without
                        // resolve, resolveWith, reject & rejectWith.
                        // So you cant "resolve" it but only add "done" functions
            return obj;
        }
    });
    // Make sure only one callback list will be used
        // if either resolve or reject is called cancel both.
        // this means that the one that has been called cant be called again
        // and the other one will never be called. So only the done or the fail
        // methods will ever be called
    deferred.then(failDeferred.cancel, deferred.cancel);
        // Don't mess with cancel!
    // Unexpose cancel
    delete deferred.cancel;
    // Call given func if any
        // function argument to be called. This was passed in. Allows you to
        // handle the deferred object after creating a new one, both as this scope
        // and as a new argument.
    if (func) {
        func.call(deferred, deferred);
    }
    return deferred;
},

/* Method: when
 * Arguments: none OR 1 of type(any & !deferred) OR n of type(deferred).
 *
 * If no arguments are passed then it gets resolved immediatly. A good way to
 * call multiple callback functions? Don't really know a good use of $.when()
 *
 * If one argument is passed and its not a deferred object then it resolves
 * immediatly and passes that argument to all the done callbacks attached.
 *
 * if n arguments are passed of type deferred object then the the done callbacks
 * will only fire if all of them succeed. If a single one fails then the
 * fail callbacks fire.
 *
 * Returns a promise read-only deferred object
 */
// Deferred helper
when: function (object) {
    var args = arguments,
        length = args.length,
                // If you pass in a deferred object then set deferred to be the promise
        // if you pass in anything else then set deferred to be a new deferred
        deferred = length <= 1 && object && jQuery.isFunction(object.promise) ?
                object :
                        jQuery.Deferred(),
        // cache the promise
        promise = deferred.promise(),
                // store an array
        resolveArray;

        // if multiple objects are passed in
    if (length > 1) {
                // create an arrey to store of values.
        resolveArray = new Array(length);
                // for each object that we wait on
        jQuery.each(args, function (index, element) {
                        // when that object resolves then
            jQuery.when(element).then(function (value) {
                                // store value in the array or store an array of values in it
                resolveArray[index] = arguments.length > 1 ? slice.call(arguments, 0) : 
value;
                                // if length === 1 then we finished calling them all
                if (!--length) {
                                        // resolve the deferred object with the read only promise
                                        // as context and the resolved values array as the argument
                    deferred.resolveWith(promise, resolveArray);
                }
                        // if any fail then we reject or deferred
            }, deferred.reject);
        });
        // if deferred was newly created but there was only one argument then
    // resolve it immediatly with the argument.
    } else if (deferred !== object) {
        deferred.resolve(object);
    }
        // return the read-only deferred.
    return promise;
},

6
Це було б набагато приємніше, якщо у вас не було горизонтальної смуги прокрутки: /
— gnarf

@gnarf Problem solved. До речі, це джерело 1.5beta. Я думаю, що в 1.6 є деякі зміни
— Raynos,

9

Виправте мене, якщо я помиляюся, але нещодавно для мене натиснули, що це по суті асинхронний виконавець завдань. Обіцянка - це підсумковий контракт, який гарантує отримання ... чогось, але без гарантії, коли ви його отримаєте.


Отже, просто старе вино в новій пляшці!
— ankush981

3

Під час роботи в Javascript ми стикаємося з ситуацією, коли виклики функцій асинхронні. Тобто потік функції кале (скажімо X) не чекає викликаної асинхронної функції (Нехай скажемо Y). Типовий приклад - коли ми робимо дзвінки на сервер, щоб отримати деякі дані з бази даних або HTML-сторінки. Якщо ці виклики не були асинхронними, користувальницький інтерфейс буде застряг в очікуванні реакції сервера. Цей асинхронний характер призводить до проблеми, коли ви хочете виконати речі в порядку, наприклад, ви хочете надрукувати щось після того, як Y (асинхрон) буде виконано або виконано отримання даних. Тут jQuery надає нам Запропонований об’єкт. В основному, jQuery подбав про весь код котла, який ми зазвичай пишемо, щоб вирішити цю ситуацію. Ось простий приклад:

  $.ajax({
      ...
  }).done(function(){
      //write here what you wish to do when this ajax call is success
  }).fail(function(){
      //write here what you wish to do on failure of this ajax call
  }); //see more on jQuery Deferred page

Ви можете написати власну відкладену (асинхронну) функцію

function DoSomethingTimeConsumingAsynch(){
    var deferred = $.Deferred();

    _.defer(function(){ //I am using underscore, you can also use setTimeout
        ...  
        deferred.resolve();//When the process is done successfully 
        ...
        deferred.reject(); //When the process has failed
    });
    return deferred;
}

//HEre how to use your own asynch function
DoSomethingTimeConsumingAsynch()
.done(function(){
   //this will be invoked on success
})
.fail(function(){
   //this will be invoked on failure
})

Я сподіваюся, що це допомогло.

Використовуючи наш веб-сайт, ви визнаєте, що прочитали та зрозуміли наші Політику щодо файлів cookie та Політику конфіденційності.
Licensed under cc by-sa 3.0 with attribution required.