New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: $.Deferred #353

Closed
justinbmeyer opened this Issue Dec 5, 2011 · 48 comments

Comments

Projects
None yet
@justinbmeyer

justinbmeyer commented Dec 5, 2011

I'd like to get JavaScriptMVC running on Zepto. I've got most of it working except for those areas around use of $.Deferred.

Are Deferreds something you guys are looking to add to Zepto? I can get it working if there is interest. Thanks!

@ollym

This comment has been minimized.

Contributor

ollym commented Dec 5, 2011

Sounds like it would be good as a plugin - i'm not in favour of this in the core though.

@mislav

This comment has been minimized.

Collaborator

mislav commented Dec 5, 2011

Me neither. It's an interesting concept but I've never used it in jQuery. It's a lot of code for something that in the end just adds some syntactic sugar and few features that could otherwise be done manually in user code by simply using events and callbacks.

@yoka

This comment has been minimized.

yoka commented Dec 11, 2011

Well.. Deferreds are nowadays important part of jQuery. If zepto is trying have jQuery compatible syntax, this is where it fails badly. jQuery ajax functionality goes hand in hand with Deferreds implementation at the moment, IMO it would be awesome if Deferred were to be implemented in zepto. It is not that much of code to add.

Just my few cents. Cheers.

@arextar

This comment has been minimized.

Contributor

arextar commented Dec 11, 2011

I think we could implement a simplified version of the Deferreds without too much code

@mislav

This comment has been minimized.

Collaborator

mislav commented Dec 11, 2011

If someone wants to add deferreds in a separate file w/ tests, we can provide it as an optional add-on.

@justinbmeyer

This comment has been minimized.

justinbmeyer commented Jan 22, 2012

I've got this working by hacking ajax like:

$.ajax = function(options){

    var success = options.success,
        error = options.error;

    var d = Deferred();

    options.success = function(){

        updateDeferred(xhr, d);
        d.resolve.apply(d, arguments);
        success && success.apply(this,arguments);
    }
    options.error = function(){
        updateDeferred(xhr, d);
        d.reject.apply(d, arguments);
        error && error.apply(this,arguments);
    }
    var xhr = AJAX(options);
    updateDeferred(xhr, d);
    return d;
};

If this sorta thing is acceptable, I'll make a pull.

@ollym

This comment has been minimized.

Contributor

ollym commented Jan 23, 2012

Make sure the deferred object is visible as $.Deferred;

@mislav

This comment has been minimized.

Collaborator

mislav commented Jan 24, 2012

@justinbmeyer sure, pull request is welcome. With tests.

@justinbmeyer

This comment has been minimized.

justinbmeyer commented Mar 5, 2012

My horrible hack to get it done in case people are interested ...

https://github.com/jupiterjs/canjs/blob/master/util/zepto/zepto.js#L90

Here's the implementation of deferred I used

https://github.com/jupiterjs/canjs/blob/master/util/deferred.js

replace can with $.

@justinbmeyer

This comment has been minimized.

justinbmeyer commented Mar 5, 2012

oh, I also hacked it to support sync ajax requests

@sudhirj

This comment has been minimized.

sudhirj commented Apr 14, 2012

+1 Deferreds are actually a very useful tool when doing async coding with multiple objects - It's actually the only thing holding me back from using Zepto right now.

@sudhirj

This comment has been minimized.

sudhirj commented Apr 26, 2012

Would you consider this in core? It's really small and thoroughly tested - https://github.com/sudhirj/simply-deferred

@ianstormtaylor

This comment has been minimized.

ianstormtaylor commented Apr 26, 2012

+1

Especially since jQuery $.ajax returns a deferred object now, this makes it harder to swap out jQuery for Zepto.

@fdn

This comment has been minimized.

fdn commented May 9, 2012

+1 for deferred object support.

@sudhirj

This comment has been minimized.

sudhirj commented May 10, 2012

I've updated the deferred library I wrote to add Zepto installation, here it is:
http://sudhirj.github.com/simply-deferred/

It provides simplified compatibility with jQuery's $.Deferred, and wraps Zepto's $.ajax to return a promise.

@gunta

This comment has been minimized.

gunta commented Jun 5, 2012

+1 using @sudhirj plugin!
I think this should be in the core, Deferred has got its moment now~
I was indeed surprised that wasn't on the core, as AJAX (and recently referred) is one of the main reasons of using jQuery/Zepto

@FranckErnewein

This comment has been minimized.

FranckErnewein commented Jun 7, 2012

+1
I really think that Deferred should be in the core.
Without Deferred, lot of Zepto plugins are different cause they return Zepto object instead of a deferred.
Plugins like "ajax", "fadeIn", "slideIn" etc.

@drewwells

This comment has been minimized.

drewwells commented Jun 20, 2012

Ignorance of Deferred is not a valid reason for exclusion from Zepto core. They are a powerful way for managing asynchronous events.

@doronin

This comment has been minimized.

doronin commented Jun 23, 2012

Deferred is a really useful mechanism to improve flow control. Should be in Zepto's core

@ghost

This comment has been minimized.

ghost commented Jul 1, 2012

+1 deffered. some styles not in effect till defer(1ms).

@james-gardner

This comment has been minimized.

james-gardner commented Jul 16, 2012

Does this mean we have support for $.Callbacks? I've not seen too much discussion on getting this working with Zepto.

@tkellen

This comment has been minimized.

tkellen commented Aug 26, 2012

+1 on deferred

@olegomon

This comment has been minimized.

olegomon commented Sep 14, 2012

+1 Deferrds. Right now it is difficult to use backbone.js with Zepto.js and backbone.layoutmangaer since the layout manager uses $.Deferreds

@andornaut

This comment has been minimized.

andornaut commented Oct 8, 2012

+1 on deferred

@olegomon

This comment has been minimized.

olegomon commented Oct 8, 2012

Hi all, I've been using the underscore.deferred implementation https://github.com/wookiehangover/underscore.deferred with Zepto.js, Backbone, Backbone.Layoutmanager which uses jQuery deferreds for rendering. It is a port of the original jQuery $.Deferred implementation.
It works like a charm. So maybe this is a good candidate for Zepto.js.

@peeter-tomberg

This comment has been minimized.

peeter-tomberg commented Oct 10, 2012

+1 on deferred.

Any feedback on if this will be in the core or as a separate plugin?

@AndyWoodly

This comment has been minimized.

AndyWoodly commented Oct 10, 2012

+1 on deferred. must have.

@wshaver

This comment has been minimized.

wshaver commented Dec 6, 2012

+1 for this being needed

@jesiah-hackinglife

This comment has been minimized.

jesiah-hackinglife commented Dec 10, 2012

+1 for me too

@nathanboktae

This comment has been minimized.

nathanboktae commented Dec 10, 2012

+1 Promises are really powerful and personally I am not interested in switching to this without it.

@eldh

This comment has been minimized.

eldh commented Dec 11, 2012

+1, absolutely

@wwwboy

This comment has been minimized.

wwwboy commented Dec 15, 2012

+1 it's the very needed functionality!
Are you using callbacks on async operations? We are going to you...)))

@sheebz

This comment has been minimized.

sheebz commented Dec 16, 2012

-1, I was for this before, but have since seen the light. I think zepto should remain a DOM only library. Deferred really doesn't have much at all to do with a DOM lib and IMO it was a mistake to put it in JQuery. There are plenty of deferred implementations out there, and people have very strong opinions on how they are implemented.

If you are looking to include one, I personally would recommend Q, its worked very well for me so far, and I am using zepto as well. Honestly I like Q's implementation better than JQuery deferreds.

@wwwboy

This comment has been minimized.

wwwboy commented Dec 16, 2012

+1
it was in the pas when we need only simple DOM manipulation with js - now we use Zepto as a part of other more complicated libraries as Backbone.js and its critical missing Deffereds in Zepto because DOM manipulation is a last thing we do.
User interaction and Ajax activity needs Deffereds as an ear!

@wyuenho

This comment has been minimized.

wyuenho commented Dec 16, 2012

+1. Backbone 0.9.9 has now removed the xhr parameter from parse, so relying on the Deferred $.ajax returns is one of the remaining more elegant ways to get to the xhr object.

@wmertens

This comment has been minimized.

wmertens commented Jan 2, 2013

+1, deferreds make for more readable and less error-prone code, as well as easy caching of results.

At the very least provide a way to quickly plug Q into zepto.

@sudhirj

This comment has been minimized.

sudhirj commented Jan 2, 2013

@wmertens Try https://github.com/sudhirj/simply-deferred ? It's more compatible with the normal jQuery API, and plugs in to Zepto with one line.

@wmertens

This comment has been minimized.

wmertens commented Jan 2, 2013

@sudhirj Excellent!

I'll leave my +1 standing though because I think promises really make for better code at a very low cost and having it available by default lowers the speedbump to adoption.

@sudhirj

This comment has been minimized.

sudhirj commented Feb 1, 2013

Thanks... just added resolveWith and rejectWith too, so it has almost full parity with jQuery. (Only the progress notifications are missing).

@themouette

This comment has been minimized.

themouette commented Feb 5, 2013

that would be delightful !
Promise helps so much in writing better code, that it would be welcome in core for ajax.
👍

@ccverg

This comment has been minimized.

ccverg commented Feb 12, 2013

I just posted a $200 bounty that'll go to the person whose pull request gets accepted for this. Everyone that +1'd should consider adding to the bounty:

https://www.bountysource.com/#repos/madrobby/zepto/issues/353

@sudhirj

This comment has been minimized.

sudhirj commented Feb 14, 2013

@ccvergara Think this a really neat idea, but it's a bit of a false carrot. The probability of the Zepto team finding a particular implementation to their liking is quite low for a number of reasons (religiously followed semicolon-free style guide, deep integration requirements).

This means that the bounty just encourages more pull requests that might or might not meet the Zepto specs, not to mention the fact that you're asking folks to implement a full feature set with no guarantee of the code ever being used. Instead, why not just give it to the plugin you like the best that supports whatever you want to do and integrates easily? Like https://github.com/sudhirj/simply-deferred :shipit:

@dorey dorey referenced this issue Jul 3, 2013

Closed

zepto.js #1168

@easy-one

This comment has been minimized.

easy-one commented Jul 5, 2013

Thanks @sudhirj (his solution works) & @ccvergara. Really need a fork of Zepto with Deferred. Anyone having a working one - expect for my donation!

@smcmurray

This comment has been minimized.

smcmurray commented Oct 18, 2013

If you want deferred, there are libraries like Q. If you want ajax with promises, Zepto.ajaxSettings.xhr=qXHR, where qXHR is something like https://github.com/smcmurray/qXHR.

This will add a promise property to the xhr used in Zepto ajax methods. promise is a Q promise.

So you can do this:

$.ajax(options).promise.then(...)

or

$.getJSON(url).promise.then(...)

Note: Q promises are a bit different from jQuery promises.

@mislav

This comment has been minimized.

Collaborator

mislav commented Nov 24, 2013

The Deferred support has landed in 92ac149 thanks to the awesome work by @caitp

@jgillich

This comment has been minimized.

jgillich commented Jan 8, 2014

ajax doesn't return something like the jqXHR; any interest in changing that? I should be able to work on a patch.

@mislav

This comment has been minimized.

Collaborator

mislav commented Jan 9, 2014

Zepto 1.1 release notes explicitly state that you can get the jqXHR-like interface by loading the optional "deferred" and "callbacks" modules. Also see the docs for $.ajax under "Promise callback interface".

@jgillich

This comment has been minimized.

jgillich commented Jan 9, 2014

Oh that's great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment