Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

也来写个JSONP好了 #5

Open
ghost opened this issue Mar 13, 2016 · 1 comment
Open

也来写个JSONP好了 #5

ghost opened this issue Mar 13, 2016 · 1 comment

Comments

@ghost
Copy link

ghost commented Mar 13, 2016

带Promise的话后续操作感觉更顺,请斧正:

ES6:

'use strict';
!function() {
let JSONP = function(url, {data, callback} = {}) {
    let args = Object(data);
    let index = JSONP.counter++;
    let script = document.createElement('script');
    let queryStrings = Object.getOwnPropertyNames(args)
        .map(key => key + '=' + encodeURIComponent(args[key]))
        .concat(['callback=' + encodeURIComponent(`JSONP[${index}]`)]);
    let promise = new Promise(function(resolve, reject) {
        Object.defineProperty(JSONP, index, {
            writable: false,
            configurable: true,
            value: resolve
        });
        script.onerror = reject;
    });
    let head = document.head;
    let onload = () => (script.onload = script.onreadystatechange = null, head.removeChild(script));
    let scrUrl = String(url);
    script.src = scrUrl + (-1 === scrUrl.indexOf('?') ? '?' : '&') + queryStrings.join('&');        
    script.onload = onload;
    script.onreadystatechange = () => /loaded|complete/.test(script.readyState) && onload();
    head.appendChild(script);
    return promise
        .then(response => (delete JSONP[index], response))
        .then(response => 'function' === typeof callback ? callback(response) : response)
        .catch(error => console.warn(error));
};
JSONP.counter = 0;
return window.JSONP = JSONP;
}();

jQuery:

'use strict';
!function() {
    var JSONP = function(url, descriptor) {
        descriptor = Object(descriptor);
        url = String(url);
        var data = descriptor.data;
        var callback = descriptor.callback;
        var args = Object(data);
        var index = JSONP.counter++;
        var script = document.createElement('script');
        var queryStrings = [];
        var promise = new jQuery.Deferred();
        var head = document.head;
        var hasOwnProperty = Object.prototype.hasOwnProperty;
        var onload = function() {
            script.onload = script.onreadystatechange = null;
            return head.removeChild(script);
        };
        for(var key in args) {
            if(hasOwnProperty.call(args, key)) {
                queryStrings.push(key + '=' + encodeURIComponent(args[key]));
            }
        }
        queryStrings.push('callback=' + encodeURIComponent('JSONP[' + index + ']'));
        JSONP[index] = function(response) {
            delete JSONP[index];
            return promise.resolve('function' === typeof callback ? callback(response) : response);
        };
        script.src = url + (-1 === url.indexOf('?') ? '?' : '&') + queryStrings.join('&');
        script.onerror = promise.reject;
        script.onload = onload;
        script.onreadystatechange = function() {
            return /loaded|complete/.test(this.readyState) && onload();
        };
        head.appendChild(script);
        return promise.fail(function(error) {
            return console.warn(error);
        });
    };
    JSONP.counter = 0;
    return window.JSONP = JSONP;
}();
@island205
Copy link
Owner

'use strict'; 写在应该写在函数内部

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant