make xhr post optional

This commit is contained in:
Misko Hevery 2010-04-30 12:22:07 -07:00
parent 549ff73a9b
commit ac1d02d065
6 changed files with 35 additions and 10 deletions

View file

@ -53,6 +53,10 @@ Browser.prototype = {
}, },
xhr: function(method, url, post, callback){ xhr: function(method, url, post, callback){
if (isFunction(post)) {
callback = post;
post = null;
}
var xhr = new this.XHR(); var xhr = new this.XHR();
xhr.open(method, url, true); xhr.open(method, url, true);
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {

View file

@ -77,7 +77,18 @@ function Compiler(textMarkup, attrMarkup, directives, widgets){
Compiler.prototype = { Compiler.prototype = {
compile: function(rawElement) { compile: function(rawElement) {
rawElement = jqLite(rawElement); rawElement = jqLite(rawElement);
var template = this.templatize(rawElement, 0, 0) || new Template(); var index = 0,
template,
parent = rawElement.parent();
if (parent && parent[0]) {
parent = parent[0];
for(var i = 0; i < parent.childNodes.length; i++) {
if (parent.childNodes[i] == rawElement[0]) {
index = i;
}
}
}
template = this.templatize(rawElement, index, 0) || new Template();
return function(element, parentScope){ return function(element, parentScope){
element = jqLite(element); element = jqLite(element);
var scope = parentScope && parentScope.$eval ? var scope = parentScope && parentScope.$eval ?

View file

@ -192,6 +192,10 @@ angularService('$route', function(location, params){
angularService('$xhr', function($browser){ angularService('$xhr', function($browser){
var self = this; var self = this;
return function(method, url, post, callback){ return function(method, url, post, callback){
if (isFunction(post)) {
callback = post;
post = null;
}
if (post && isObject(post)) { if (post && isObject(post)) {
post = toJson(post); post = toJson(post);
} }
@ -213,6 +217,10 @@ angularService('$xhr.bulk', function($xhr){
callbacks = [], callbacks = [],
scope = this; scope = this;
function bulkXHR(method, url, post, callback) { function bulkXHR(method, url, post, callback) {
if (isFunction(post)) {
callback = post;
post = null;
}
requests.push({method: method, url: url, data:post}); requests.push({method: method, url: url, data:post});
callbacks.push(callback); callbacks.push(callback);
} }
@ -240,6 +248,10 @@ angularService('$xhr.bulk', function($xhr){
angularService('$xhr.cache', function($xhr){ angularService('$xhr.cache', function($xhr){
var inflight = {}; var inflight = {};
function cache(method, url, post, callback){ function cache(method, url, post, callback){
if (isFunction(post)) {
callback = post;
post = null;
}
if (method == 'GET') { if (method == 'GET') {
var data; var data;
if (data = cache.data[url]) { if (data = cache.data[url]) {

View file

@ -192,22 +192,21 @@ angularWidget('NG:INCLUDE', function(element){
function incrementChange(){ changeCounter++;} function incrementChange(){ changeCounter++;}
this.$watch(srcExp, incrementChange); this.$watch(srcExp, incrementChange);
this.$watch(scopeExp, incrementChange); this.$watch(scopeExp, incrementChange);
scope.$onEval(function(){
if (childScope) childScope.$eval();
});
this.$watch(function(){return changeCounter;}, function(){ this.$watch(function(){return changeCounter;}, function(){
var src = this.$eval(srcExp), var src = this.$eval(srcExp),
useScope = this.$eval(scopeExp); useScope = this.$eval(scopeExp);
if (src) { if (src) {
scope.$browser.xhr('GET', src, function(code, response){ scope.$xhr.cache('GET', src, function(code, response){
element.html(response); element.html(response);
childScope = useScope || createScope(scope); childScope = useScope || createScope(scope);
compiler.compile(element)(element, childScope); compiler.compile(element)(element, childScope);
childScope.$init(); childScope.$init();
scope.$root.$eval();
}); });
} }
}); });
scope.$onEval(function(){
if (childScope) childScope.$eval();
});
}; };
} }
}); });

View file

@ -1,2 +1,2 @@
# java -jar lib/jstestdriver/JsTestDriver.jar --tests all java -jar lib/jstestdriver/JsTestDriver.jar --tests all
java -jar lib/jstestdriver/JsTestDriver.jar --tests all --config jsTestDriver-jquery.conf # java -jar lib/jstestdriver/JsTestDriver.jar --tests all --config jsTestDriver-jquery.conf

View file

@ -276,9 +276,8 @@ describe("widget", function(){
scope.childScope = createScope(); scope.childScope = createScope();
scope.childScope.name = 'misko'; scope.childScope.name = 'misko';
scope.url = 'myUrl'; scope.url = 'myUrl';
scope.$browser.xhr.expect('GET', 'myUrl').respond('{{name}}'); scope.$xhr.cache.data.myUrl = {value:'{{name}}'};
scope.$init(); scope.$init();
scope.$browser.xhr.flush();
expect(element.text()).toEqual('misko'); expect(element.text()).toEqual('misko');
}); });
}); });