Giter Club home page Giter Club logo

ext.ux.deferred's Introduction

Ext.ux.Deferred

Ext.ux.Deferred provides promises for ExtJS and Sencha Touch.

It allows to manage async functions with ease.

Problems

The first problem is with the Pyramid of Doom, namely nested asynchronous functions, like this:

aSync1 (10, function (val1) {
	aSync2 (val1, function (val2) {
		aSync3 (val2, function (val3) {
			alert ('Top of the pyramid with: ', val3);
		});
	});
});

What we want here is to have a comfortable way to chain those asynchronous functions. With Ext.ux.Deferred it will be rewritten as follows:

aSync1 (10)
	.then (aSync2)
	.then (aSync3)
	.then (function (val3) {
		alert ('End of the queue with: ', val3);
	});

Each result is given as an argument to the next async function on the chain path.

The second problem is make a function start after the execution of a set of asynchronous functions. Take the above functions as example: now, we want to start the last anonymous function at the end of their execution: Ext.ux.Deferred has a static method, called when, that allows you to do that! Get the following example:

Ext.ux.Deferred
	.when (aSync1, aSync2, aSync3)
	.then (function (val3) {
		alert ('End of everything with: ', val3);
	});

Tutorial

Ext.ux.Deferred can be used to defer asynchronous processes. The first thing to do is to make a new deferred:

function aSync1 (val) {
	var dfd = Ext.create ('Ext.ux.Deferred') ,
		task = setInterval (function () {
			// here your async task
			clearInterval (task);
		}, 1000);
		
	return dfd;
}

Then, use the resolve/reject method to tell your promise what to do:

function aSync1 (val) {
	var dfd = Ext.create ('Ext.ux.Deferred') ,
		task = setInterval (function () {
			if (ipoteticalCounter > IPOTETICAL_VALUE) dfd.resolve (data);
			else dfd.reject (data);
			
			clearInterval (task);
		}, 1000);
	
	return dfd;
}

Now, you are ready to use your deferred by handling the result! The first method, then, accepts two args: the first one is the success callback, while the second one is the fail callback:

var promise = aSync1(10);

promise.then (
	function (result) {
		alert ('The promise has been solved with: ', result);
	} ,
	function (result) {
		alert ('The promise has been rejected with: ', result);
	}
);

Otherwise, it can be used the done-fail approach:

var promise = aSync1(10);

promise
	.done (function (result) {
		alert ('The promise has been solved with: ', result);
	})
	.fail (function (result) {
		alert ('The promise has been rejected with: ', result);
	});

Or the always method, invoked in every situation (both success and fail):

var promise = aSync1(10);

promise.always (function (result) {
	alert ('The promise has been solved or rejected with: ', result);
});

Install via Bower

First of all, install Bower.

Then install Ext.ux.Deferred:

$ bower install ext.ux.deferred

Now, you got the extension at the following path: YOUR_PROJECT_PATH/bower_components/ext.ux.deferred/

It contains Deferred.js file and a minified version Deferred.min.js.

Let's setup the Ext.Loader to require the right file:

Ext.Loader.setConfig ({
	enabled: true ,
	paths: {
		'Ext.ux.Deferred': 'bower_components/ext.ux.deferred/Deferred.js'
		// or the minified one: 'Ext.ux.Deferred': 'bower_components/ext.ux.deferred/Deferred.min.js'
	}
});

Ext.require (['Ext.ux.Deferred']);

Usage

Load Ext.ux.Deferred via Ext.require:

Ext.Loader.setConfig ({
	enabled: true
});

Ext.require (['Ext.ux.Deferred']);

Now, you are ready to use it in your code as follows:

var dfd = Ext.create ('Ext.ux.Deferred') ,
	task = setInterval (function () {
		deferred.resolve (10);
		clearInterval (task);
	}, 1000);

Ext.ux.Deferred
	.when (dfd)
	.then (function (value) {
		alert (value); // will print 10
	});

Documentation

You can build the documentation (like ExtJS Docs) with jsduck:

$ jsduck ux --output /var/www/docs

It will make the documentation into docs dir and it will be visible at: http://localhost/docs

License

The MIT License (MIT)

Copyright (c) 2013 Vincenzo Ferrari [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

ext.ux.deferred's People

Contributors

wilk avatar dikmax avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.