UNPKG

lettuce

Version:

Lettuce JS, Mini Mobile Framework for Romantic with DSL.

85 lines (73 loc) 2.87 kB
(function($){ var jsonpID = 0; function empty() {} $.ajaxJSONP = function(options){ var jsonpString; jsonpString = 'jsonp' + ++jsonpID; window[jsonpString] = function(j){ options.success(j) } var script = document.createElement('script'); $(script).attr({ src: options.url.replace(/callback=\?/, 'callback=' + jsonpString), type: 'text/javascript' }); $('head').append(script); }; $.ajax = function(options){ // { type, url, data, success, dataType, contentType } options = options || {}; if (options.url && /callback=\?/.test(options.url)) { return $.ajaxJSONP(options) } var data = options.data, callback = options.success || empty, errback = options.error || empty, mime = mimeTypes[options.dataType], content = options.contentType, xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if (xhr.readyState == 4) { if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 0) { if (mime == 'application/json') { var result, error = false; try { result = JSON.parse(xhr.responseText); } catch (e) { error = e; } if (error) errback(xhr, 'parsererror', error); else callback(result, 'success', xhr); } else callback(xhr.responseText, 'success', xhr); } else { errback(xhr, 'error'); } } }; xhr.open(options.type || 'GET', options.url || window.location, true); if (mime) xhr.setRequestHeader('Accept', mime); if (data instanceof Object) data = JSON.stringify(data), content = content || 'application/json'; if (content) xhr.setRequestHeader('Content-Type', content); xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xhr.send(data); }; var mimeTypes = $.ajax.mimeTypes = { json: 'application/json', xml: 'application/xml', html: 'text/html', text: 'text/plain' }; $.get = function(url, success){ $.ajax({ url: url, success: success }) }; $.post = function(url, data, success, dataType){ if (data instanceof Function) dataType = dataType || success, success = data, data = null; $.ajax({ type: 'POST', url: url, data: data, success: success, dataType: dataType }); }; $.getJSON = function(url, success){ $.ajax({ url: url, success: success, dataType: 'json' }) }; $.fn.load = function(url, success){ if (!this.dom.length) return this; var self = this, parts = url.split(/\s/), selector; if (parts.length > 1) url = parts[0], selector = parts[1]; $.get(url, function(response){ self.html(selector ? $(document.createElement('div')).html(response).find(selector).html() : response); success && success(); }); return this; }; })(Zepto);