diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/AngularPublic.js | 5 | ||||
| -rw-r--r-- | src/Browser.js | 48 | ||||
| -rw-r--r-- | src/Resource.js | 3 |
3 files changed, 37 insertions, 19 deletions
diff --git a/src/AngularPublic.js b/src/AngularPublic.js index e2e576fd..7b093f88 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -1,7 +1,10 @@ var browserSingleton; angularService('$browser', function browserFactory(){ if (!browserSingleton) { - browserSingleton = new Browser(window.location, window.document); + browserSingleton = new Browser( + window.location, + jqLite(window.document), + jqLite(window.document.getElementsByTagName('head')[0])); browserSingleton.startUrlWatcher(); browserSingleton.bind(); } diff --git a/src/Browser.js b/src/Browser.js index 0552b3ae..3299540c 100644 --- a/src/Browser.js +++ b/src/Browser.js @@ -2,7 +2,7 @@ // Browser ////////////////////////////// -function Browser(location, document) { +function Browser(location, document, head) { this.delay = 50; this.expectedUrl = location.href; this.urlListeners = []; @@ -21,8 +21,9 @@ function Browser(location, document) { }; this.location = location; - this.document = jqLite(document); - this.body = jqLite(document.body); + this.document = document; + this.head = head; + this.idCounter = 0; } Browser.prototype = { @@ -58,21 +59,34 @@ Browser.prototype = { callback = post; post = null; } - var xhr = new this.XHR(), - self = this; - xhr.open(method, url, true); - this.outstandingRequests.count ++; - xhr.onreadystatechange = function() { - if (xhr.readyState == 4) { - try { - callback(xhr.status || 200, xhr.responseText); - } finally { - self.outstandingRequests.count--; - self.processRequestCallbacks(); + if (lowercase(method) == 'json') { + var callbackId = "angular_" + Math.random() + '_' + (this.idCounter++); + callbackId = callbackId.replace(/\d\./, ''); + var script = this.document[0].createElement('script'); + script.type = 'text/javascript'; + script.src = url.replace('JSON_CALLBACK', callbackId); + this.head.append(script); + window[callbackId] = function(data){ + delete window[callbackId]; + callback(200, data); + }; + } else { + var xhr = new this.XHR(), + self = this; + xhr.open(method, url, true); + this.outstandingRequests.count ++; + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + try { + callback(xhr.status || 200, xhr.responseText); + } finally { + self.outstandingRequests.count--; + self.processRequestCallbacks(); + } } - } - }; - xhr.send(post || ''); + }; + xhr.send(post || ''); + } }, processRequestCallbacks: function(){ diff --git a/src/Resource.js b/src/Resource.js index 1279dc54..f4f26ebd 100644 --- a/src/Resource.js +++ b/src/Resource.js @@ -28,6 +28,7 @@ Route.prototype = { query.push(encodeURI(key) + '=' + encodeURI(value)); } }); + url = url.replace(/\/*$/, ''); return url + (query.length ? '?' + query.join('&') : ''); } }; @@ -88,7 +89,7 @@ ResourceFactory.prototype = { throw "Expected between 0-3 arguments [params, data, callback], got " + arguments.length + " arguments."; } - var value = action.isArray ? [] : new Resource(data) + var value = action.isArray ? [] : new Resource(data); self.xhr( action.method, route.url(extend({}, action.params || {}, extractParams(data), params)), |
