Skip to content

johnpan/whenDefined

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

whenDefined.js

A tiny library for Featue Detection and Object Detection. In other words, it provides a simple way to check object/ library/ function availability and trigger jQuery promises. Promises will be rejected after one and a half minutes, and you can change that by altering the window.whenDefined_timeOutLimit variable

Dead-easy usage:

whenDefined(objectName).then(doSmth);
whenDefined(objectName).then(doSmth, doSmthElseOnFail);

With the second parameter you can ensure that the promise resolves only if a method returns true.

whenDefined(objectName, functionNameMustReturnTrue).then(doSmth, doSmthElse);

Examples:

whenDefined("ko").then(function(){alert( "knockout.js is loaded"); }); 
whenDefined("myLib", "myLib.isOddMonth").then(function(){alert( "myLibrary says it's an odd month"); });

You can group promises as usual, using $.when

$.when( whenDefined("coolLibrary"), whenDefined("niceLibrary") ).then(doSmthNiceNcool);

You can bind promises with nested syntax

whenDefined("wheel").then (
  function () { whenDefined("car").then(
    buildCarFactory
  )}
);

About

A tiny Featue / Object Detection library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published