GithubHelp home page GithubHelp logo

nfeldman / requirejs-promise Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jokeyrhyme/requirejs-promise

0.0 2.0 0.0 183 KB

plugin for Require.JS that allows modules to resolve asynchronously via Promises

License: BSD 2-Clause "Simplified" License

JavaScript 100.00%

requirejs-promise's Introduction

requirejs-promise

Build Status

This is a plugin for Require.JS that allows modules to resolve asynchronously via jQuery Promises. The current AMD standard does not specify how to deal with asynchronous dependencies, and as such it is perfectly reasonable that Require.JS does not handle them. That said, I still find them unavoidable from time to time, so here we are.

Status

This plugin seems to behave as expected for Promise objects generated by jQuery, Q and RSVP. It now seems to work okay with the Require.JS Optimiser.

Licence

I'm putting this out under the Simplified BSD Licence (see LICENCE). Go nuts.

Requirements

You'll also (obviously) need Require.JS. Likewise, I'll always be targetting the current version.

As this is just a plugin, it doesn't make sense to have further requirements.

I do intend to eventually extend support to other implementations of Promises (with your help) so jQuery won't always be needed.

Usage

Basics

You shouldn't need to include this directly in your HTML as a script element.

Configure Require.JS so that it has a path to resolve, for example:

    require.config({
        config: {
            paths: {
                // remember to leave out the .js on the end
                'promise': '/path/to/requirejs-promise'
            }
        }
    });

Your asynchronous module will probably look something like this:

    define('myAsyncModule', ['jquery'], function($) {
        var deferred = new $.Deferred(),
        myModule = {};

        // we'll use AJAX here, but you could do anything asynchronous
        $.ajax({
            url: '//domain/path/to/script',
            complete: function(jqxhr, textstatus) {
                if ($.inArray(jqxhr.status, [0, 200, 204, 304])) {
                    // TODO: do other stuff for successful AJAX
                    myModule.response = jqxhr.responseText;
                    // if your consumers are expecting something, give it to them
                    deferred.resolve(myModule);
                } else {
                    // TODO: do other stuff for failed AJAX
                    deferred.reject(null);
                }
            }
        });

        return deferred.promise();
    });

Then, when it comes time to actually load in an asynchronous Promise-based dependency, do something like this:

    require(['promise!myAsyncModule'], function(asyncModule) {
        // TODO: do something cool with asyncModule.response
        ...
    });

The goal of this little plugin is to make module usage completely seemless. Module resolution is asynchronous within Require.JS anyway, so (assuming everything goes according to plan) nobody downstream should notice that the dependency had a Promise.

Require.JS Optimisation

See the test/ directory.

index.html is a QUnit test case using non-optimised sources

index2.html is a QUnit test case using optimised (minified) sources

For hints on what needs to be done to get the Optimiser working, see test/build.js and test/main.js

requirejs-promise's People

Contributors

frost-nzcr4 avatar jokeyrhyme avatar nfeldman avatar

Watchers

 avatar  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.