1.0.12 • Published 8 years ago

lazyiterators v1.0.12

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
8 years ago

LazyIterators

lazyIterators will allow you to iterate over Arrays of Objects or any end-point using the same simple iteration API, taking care of all the hard work for you.

In the current LazyIterators module you can find an Array of Objects lazyIterator and a generic end-points lazyIterator which will handle for you responses out of order, connections errors and timeouts.

Please note that lazyIterators are framework independant and compatible with AngularJS, meaning that if you use them from within AngularJS they will detect it is present and require transparently its required services, if Angular is not present it will use XMLHTTPRequest native methods :)

Installation

 $ npm install lazyiterators

LazyArrayIterators

LazyArrayIterators implement the lazyIterators API and allows you to iterate over an Array of Objects.

LazyArrayIterators Usage:

	/* 
	 * require LazyArrayIterator from lazyIterators module.
	 *
	 **/

  var LazyArrayIterator = require('lazyiterators').ArrayIterator;

	/* lazyIterator elements loading success/error callbacks */

	function error(error) {
		console.log('error loading elements:', error);
	}

	function success(elements) {
		console.log(' - retrieved elements:', elements);
	}

	/* create list of elements to iterate */

	var i, myElements = [];

	for (i = 0; i < 100; i++) {
		myElements.push({ id: i, name: 'element ' + i });
	}

	/* Apply iterator over list */

	var myLazyIterator = new LazyArrayIterator(myElements, { iter: 5 });

	/* 
	 * Get a badge of elements, current index 0,
	 * first five elements will be returned.
	 *
	 **/

	myLazyIterator.next(success, error);

	/* 
	 * Get a badge of 10 elements, current index 5,
	 * elements from index 5 till 15 will be returned.
	 *
	 **/

	myLazyIterator.next(success, error, 10);

	/* Get back a badge of 15 elements from current
	 * collection index, from 15 to 0.
	 *
	 **/

	myLazyIterator.prev(success, error, 15);

	/*
	 * Use get() method to retrieve elements from collection,
	 * by using this method the specified elements will be
	 * returned without affecting the current iterator index.
	 *
	 **/

	myLazyIterator.get({
		index: 50,
		limit: 10,
		error: error,
		success: success,
	});

	/* Get a badge of elements, iterator index was left at
	 * zero before using the previous get() method so the
	 * first badge of elements will be returned.
	 *
	 **/

	myLazyIterator.next(success, error);

	/*
	 * Index element 95 and request one badge of elements, 
	 * getting to the end of the collection. LazyIterators
	 * should return true in its done() method when this
	 * condition is true.
	 *
	 **/

	myLazyIterator.index(95).next(success, error);

	/* */

	setTimeout(function() {
		console.log('end of collection?', 
			(myLazyIterator.done()) ? 'YES' : 'NO');

	}, 0);

LazyEndPointIterators

lazyEndPointIterators will allow you to iterate over almost any HTTP end-point transparently using the lazyIterators API. The iterator will transparently handle for you responses out of order, connection errors or timeouts. For more detailed information about their config options kindly check the "lazyIterators" section in the "lazyContainers" documentation located at: http://www.slidonjqueryslider.com/lazycontainer/

LazyEndPointIterators Usage:

  /* Require LazyEndPointIterator from lazyIterators module */

  var LazyEndPointIterator = require('lazyiterators').EndPointIterator;

  /*
   * This example assumes we are using an end-point located at
   * "/api/items.php" which has an "index" parameter to specify
   * the starting index in the end-point's collection we want to
   * request and a "limit" parameter stating the number of 
   * elements we need from within the specified "index".
   *
   * For example, to request the first 10 elements we would need
   * the following query: "/api/items.php?index=0&limit=10"
   *
   * Don't worry about AngularJS services, lazyEndPointIterator
   * will detect if Angular is present, request app injector and
   * request the necesary services. If using them from another
   * framework or whatsoever, they will detect Angular is not
   * present and use native XMLHTTPRequest :)
   *
   **/

  /* Build end-point iterator */

  var endPointUrl = "/api/items.php?index=:index&limit=:limit";
  var myLazyIterator = new LazyEndPointIterator(endPointUrl, config);

  /*
   * From here on you use the end-point just as the LazyArrayIterator,
   * the beauty of the lazyIterators (thus the lazyContainers) is that
   * at any given point you can switch the elements stream transparently.
   *
   **/

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License

1.0.12

8 years ago

1.0.11

8 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.3

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago