Permalink
Browse files

Fixed dpd.js in certain browsers (take a guess)

  • Loading branch information...
1 parent 5e110bd commit f71245c83477a2bf2ab25ce25cfb8aa13d6c06e9 @dallonf dallonf committed Sep 27, 2012
Showing with 33 additions and 42 deletions.
  1. +1 −1 clib/.jshintrc
  2. +5 −3 clib/ajax.js
  3. +27 −38 clib/dpd.js
View
@@ -9,7 +9,7 @@
"undef": true,
"browser": true,
"devel": true,
- "globals": {
+ "predef": {
"io": true
, "_dpd": true
}
View
@@ -33,9 +33,11 @@ function sendRequest(url,options) {
if (data)
req.setRequestHeader('Content-type', options.contentType || 'application/json');
if (typeof sendRequest.headers === 'object') {
- Object.keys(sendRequest.headers).forEach(function(k) {
- req.setRequestHeader(k, sendRequest.headers[k]);
- });
+ for (var k in sendRequest.headers) {
+ if (sendRequest.headers.hasOwnProperty(k)) {
+ req.setRequestHeader(k, sendRequest.headers[k]);
+ }
+ }
}
req.onreadystatechange = function () {
if (req.readyState != 4) return;
View
@@ -1,11 +1,11 @@
-/*global _dpd:true, io:true*/
-
(function (undefined) {
if (!window._dpd) window._dpd = {};
var root = window.location.origin;
+ var consoleLog = (typeof console !== 'undefined') && console.log;
+
// initial socket connection
var socket = io.connect(root);
@@ -35,33 +35,25 @@
}
return parts;
-}
-
- var normalizePath = function(path) {
- var isAbsolute = path.charAt(0) === '/',
- trailingSlash = path.slice(-1) === '/';
-
- // Normalize the path
- path = normalizeArray(path.split('/').filter(function(p) {
- return !!p;
- }), !isAbsolute).join('/');
+ }
- if (!path && !isAbsolute) {
- path = '.';
- }
- if (path && trailingSlash) {
- path += '/';
+ function filterArray(list, fn) {
+ if (Array.prototype.filter) return Array.prototype.filter.call(list, fn);
+ var newList = [];
+ for (var i = 0; i < list.length; i++) {
+ if (fn(list[i])) {
+ newList.push(list[i]);
}
-
- return (isAbsolute ? '/' : '') + path;
- };
-
+ }
+ return newList;
+ }
function joinPath() {
var paths = Array.prototype.slice.call(arguments, 0);
- return normalizePath(paths.filter(function(p, index) {
+ paths = paths.join('/').split('/');
+ return '/' + filterArray(paths, function(p, index) {
return p && typeof p === 'string';
- }).join('/'));
+ }).join('/');
}
function isComplex(obj) {
@@ -79,9 +71,11 @@
function createQueryString(query) {
var parts = [];
- Object.keys(query).forEach(function(k) {
- parts.push(encodeURIComponent(k) + "=" + encodeURIComponent(query[k]));
- });
+ for (var k in query) {
+ if (query.hasOwnProperty(k)) {
+ parts.push(encodeURIComponent(k) + "=" + encodeURIComponent(query[k]));
+ }
+ }
return parts.join('&');
}
@@ -95,12 +89,14 @@
function returnSuccess(fn) {
return function(data) {
+ if (fn === consoleLog) return console.log(data);
if (typeof fn === 'function') fn(data);
};
}
function returnError(fn) {
return function(data) {
+ if (fn === consoleLog) return console.error(data);
if (typeof fn === 'function') fn(null, data);
};
}
@@ -174,12 +170,12 @@
}
// query
- if (typeof args[i] === 'object' || !args[i]) {
+ if (args[i] !== consoleLog && typeof args[i] === 'object' || !args[i]) { // IE considers console.log to be an object.
settings.query = args[i];
i++;
}
- if (typeof args[i] === 'function') {
+ if (typeof args[i] === 'function' || args[i] === consoleLog) {
settings.fn = args[i];
}
@@ -197,19 +193,19 @@
}
// body
- if (typeof args[i] === 'object' || !args[i]) {
+ if (args[i] !== consoleLog && typeof args[i] === 'object' || !args[i]) {
settings.body = args[i];
i++;
}
// query - if this exists the LAST obj was query and the new one is body
- if (typeof args[i] === 'object') {
+ if (args[i] !== consoleLog && typeof args[i] === 'object') {
settings.query = settings.body;
settings.body = args[i];
i++;
}
- if (typeof args[i] === 'function') {
+ if (typeof args[i] === 'function' || args[i] === consoleLog) {
settings.fn = args[i];
}
@@ -276,12 +272,5 @@
socket.on.apply(socket, arguments);
};
- if (console && console.log) {
- var originalLog = console.log;
- _dpd.log = function() {
- originalLog.apply(console, arguments);
- };
- }
-
})();

0 comments on commit f71245c

Please sign in to comment.