From 0db23e7488f361c8cf903cd07750f6602a79537a Mon Sep 17 00:00:00 2001 From: DallonF Date: Mon, 14 May 2012 12:22:59 -0700 Subject: [PATCH] Added dpd.js loader proxy --- clib/dpd-loader.js | 11 ++++ clib/dpd.js | 133 ++++++++++++++++++++++++++++----------------- lib/clib.js | 42 ++++++++++---- 3 files changed, 126 insertions(+), 60 deletions(-) create mode 100644 clib/dpd-loader.js diff --git a/clib/dpd-loader.js b/clib/dpd-loader.js new file mode 100644 index 0000000..501f3c3 --- /dev/null +++ b/clib/dpd-loader.js @@ -0,0 +1,11 @@ +var url = /*rootUrl*/ + '/dpd.js'; + +if (!document.readystate || document.readystate === "complete") { + var head = document.getElementsByTagName('head')[0], + script = document.createElement('script'); + + script.src = url; + head.appendChild(script); +} else { + document.write('') +} \ No newline at end of file diff --git a/clib/dpd.js b/clib/dpd.js index b2d9b30..f3a2e53 100644 --- a/clib/dpd.js +++ b/clib/dpd.js @@ -1,12 +1,25 @@ (function ($) { - if(!$) throw 'dpd.js depends on jQuery.ajax - you must include it before loading dpd.js'; + if(!$) throw Error('dpd.js depends on jQuery.ajax - you must include it before loading dpd.js'); // global namespace window.dpd = {}; var r , resources = /*resources*/ - , contentType = 'application/json'; + , contentType = 'application/json' + , rootUrl = /*rootUrl*/; + + function errorCallback(fn) { + return function (err) { + fn && fn(null, JSON.parse(err.responseText)); + } + } + + function successCallback(fn) { + return function (res) { + fn && fn(res); + } + } while(r = resources.shift()){ if(~r.type.indexOf('Collection')) { @@ -22,76 +35,106 @@ var q = (query && ('?q=' + JSON.stringify(query))) || ''; return $.ajax({ - url: r.path + q, + url: rootUrl + r.path + q, type: 'GET', contentType: contentType, success: function (res) { - fn && fn(res); + fn && fn(res || []); }, - error: function (err) { - fn && fn(null, JSON.parse(err)); - } + error: errorCallback(fn) }); } })(r), - first: (function (r) { + getOne: (function (r) { return function (query, fn) { if(!fn) { fn = query; query = undefined; } - var q = (query && ('?q=' + JSON.stringify(query))) || ''; + var q = ''; + + if (typeof query === 'string') { + q = '/' + query; //Id + } else if (query) { + q = '?q=' + JSON.stringify(query); + } return $.ajax({ - url: r.path + q, + url: rootUrl + r.path + q, type: 'GET', contentType: contentType, success: function (res) { if(res && res.length) { fn && fn(res[0]); - } else { + } else if (typeof res.length !== 'undefined') { fn && fn(null, {message: 'not found', status: 404}); + } else { + fn && fn(res); } }, - error: function (err) { - fn && fn(JSON.parse(err)); - } + error: errorCallback(res) }); } })(r), save: (function (r) { return function (obj, fn) { + if (typeof obj === 'string') { throw Error("save() does not take an id. Did you mean to use put()?"); } return $.ajax({ - url: r.path + (obj._id ? ('/' + obj._id) : ''), + url: rootUrl + r.path + (obj._id ? ('/' + obj._id) : ''), type: 'POST', contentType: contentType, data: JSON.stringify(obj), - success: function (res) { - fn && fn(res); - }, - error: function (err) { - fn && fn(null, err); - } + success: successCallback(fn), + error: errorCallback(fn) + }); + } + })(r), + post: (function(r) { + return function (obj, fn) { + if (obj && obj._id) { throw Error("Cannot post() an object with an _id. Did you mean to use save() or put()?"); } + return $.ajax({ + url: rootUrl + r.path, + type: 'POST', + contentType: contentType, + data: JSON.stringify(obj), + success: successCallback(fn), + error: errorCallback(fn) + }); + } + })(r), + put: (function(r) { + return function (id, obj, fn) { + if (typeof id !== 'string') { + obj = arguments[0]; //reorder parameters + fn = arguments[1]; + id = obj && obj._id; + } + if (!id) { throw Error("id must be provided!"); } + return $.ajax({ + url: rootUrl + r.path + '/' + id, + type: 'PUT', + contentType: contentType, + data: JSON.stringify(obj), + success: successCallback(fn), + error: errorCallback(fn) }); } })(r), del: (function (r) { - return function (query, fn) { - if(!fn) fn = query; + return function (id, fn) { + if (typeof id !== 'string') {throw Error("id must be provided!");} - var q = query && ('?q=' + JSON.stringify(query)); + var q = '/' + id; return $.ajax({ - url: r.path + q, + url: rootUrl + r.path + q, type: 'DELETE', contentType: contentType, - success: function (res) { - fn && fn(res); + success: function(res) { + fn && fn(true); }, - error: function (err) { - fn && fn(null, err); - } + error: errorCallback(fn) }); } })(r) @@ -102,16 +145,12 @@ resource.login = (function (r) { return function (credentials, fn) { return $.ajax({ - url: r.path + '/login', + url: rootUrl + r.path + '/login', type: 'POST', contentType: contentType, data: JSON.stringify(credentials), - success: function (res) { - fn && fn(res); - }, - error: function (err) { - fn && fn(null, err); - } + success: successCallback(fn), + error: errorCallback(fn) }); } })(r); @@ -119,15 +158,13 @@ resource.logout = (function (r) { return function (fn) { return $.ajax({ - url: r.path + '/logout', + url: rootUrl + r.path + '/logout', type: 'POST', contentType: contentType, - success: function (res) { - fn && fn(res); + success: function(res) { + fn && fn(true); }, - error: function (err) { - fn && fn(null, err); - } + error: errorCallback(fn) }); } })(r); @@ -135,15 +172,11 @@ resource.me = (function (r) { return function (fn) { return $.ajax({ - url: r.path + '/me', + url: rootUrl + r.path + '/me', type: 'GET', contentType: contentType, - success: function (res) { - fn && fn(res); - }, - error: function (err) { - fn && fn(null, err); - } + success: successCallback(fn), + error: errorCallback(fn) }); } })(r); diff --git a/lib/clib.js b/lib/clib.js index 9a0c73b..4651604 100644 --- a/lib/clib.js +++ b/lib/clib.js @@ -6,17 +6,39 @@ var resources = require('./collections/resources') , fs = require('fs'); module.exports = function (req, res, next) { - resources.get(function (err, resources) { - if(err) return next(err); - - resources = resources || []; - - fs.readFile(__dirname + '/../clib/dpd.js', function (err, data) { + + function loadAndSendFile(fileName, callback) { + fs.readFile(__dirname + fileName, function (err, data) { var src = data.toString(); res.header('Content-Type', 'text/javascript'); - res.send( - src.replace('/*resources*/', JSON.stringify(resources)) - ); + + if (callback) { src = callback(src); } + + var rootUrl = process.url.href; + if (rootUrl.lastIndexOf('/') === rootUrl.length - 1) { rootUrl = rootUrl.slice(0, -1); } + + src = src.replace('/*rootUrl*/', '"' + rootUrl + '"'); + res.send(src); }) - }); + } + + if (req.param('proxy')) { + loadAndSendFile('/../clib/dpd-loader.js', function(src) { + res.header('Content-Disposition', 'attachment; filename=dpd.js') + return src; + }); + } else { + resources.get(function (err, resources) { + if(err) return next(err); + + resources = resources || []; + + loadAndSendFile('/../clib/dpd.js', function(src) { + return src.replace('/*resources*/', JSON.stringify(resources)); + }); + }); + + } + + } \ No newline at end of file