-
the when method
In jQuery 1.5, there is a "when" method.
function when(objects) {}
-
the promise object
The jQuery.Deferred object has a function .promise(), it returns a promise object. Some other people call it future. In the async framework of .net, it is called Task<T> . It an object that promise to deliver a value in the future. So if believe that promise, you can express your believing like this
promise.done(function () { var valuesOfFuture = [].slice.call(arguments, 0); alert(); });
The promise object does not has method of "resolve" and "reject". Because these method belong to server side. This pattern is very popular, but jQuery stream line this pattern. It is very useful pattern.
-
jQuery.Deferred Object
Internally, the Deferred object is implemented with two _deferred object. The Deferred object offer two set of functions, done/resolveWtih/resolve/isResolved and fail/rejectWith/reject/isRejected. The are basically the functions of two internal _deferred object. The failed set function is the done/resolveWith/resoved/isREsolved function of fail object. But we don't have cancel function, because it is unnecessary, it is replaced with reject function. The done function will empty the failed list, and the reject function will empty the done list.
It also introduced two new functions, then and promise. The then function is short cut to push a done function and failed function at one shot. We care about the success and compensation at the same time, we should consistently use then function instead of interweaving done or fail method. But if we don't care about that, we can interweave done and fail method. However, I think the then method should always be used, as it adds very little cost.
Another new method is promise method. It basically return a new object or decorate an existing object with "promise prospect" or promise methods then, done, fail, isResolved, isRejected, promise. The promise object does the same thing as Deferred object, except it can not resolve, reject, which should be used at the server side. When we first call the d.promise(obj) with a real obj, then the obj is augmented with the promise methods and cached internally, and this pattern should be used at the first time. The subsequent call d.promise() will return the cached object.
The constructor Deferred(initialize) can be passed with an initialize function. Your function can be defined as
function (deferred) { //this == deferred this.then(x, y); this.m1 = "m1"; }
-
jQuery._Deferred Object
The jQuery._Deferred is an object maintaining a task list. To understanding it, help you to understand how the jQuery.Deferred and jQuery.when works. The _Deferred object has the following use case.
- Push your task into the list to be run(resolved) in the future. It has been resolved, call the resolved method again.
- Push your task into the list to be run(resolved) in the future. It has been resolved, call the resolved method again.
-
Git finally
After using Git for a few months with GitHub, I am finally loving it and setup Git server using IIS 7 and git-dot-aspx. I finally feel the force is with me now.
Source control is more important and more complicated I thought many years ago. Over the years of programming, I understand more about it. I used cvs, vss, svn, tfs and now finally Git. I have seen many other people's discussions of version control, I also have many discussion with other people. But the views are so different, it is like for a Christian to convince an atheist to believe there is a god. How can you explain someone who love single checkout in vss that multi checkout is better in vss, how can you explain someone who love vss that svn is better, and how can you explain to someone who love svn that Git is the best. I have seen a IT manager said that "I never seen enterprise software can been successfully implemented without exclusive checkout". It is not worthy for such kind of discussion. But semantics remains the same, we just try to get a tool to satisfy our need, make our work more productive, make us in control. But if we don't have that kind of need, or we don't know there is such kind need, or we don't have time to need, it is very hard to accept some new concept. Git is specially designed to make you feel less intelligent than you think you were. Learn it.