mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-22 19:23:38 +08:00
make xhr post optional
This commit is contained in:
@@ -53,6 +53,10 @@ Browser.prototype = {
|
||||
},
|
||||
|
||||
xhr: function(method, url, post, callback){
|
||||
if (isFunction(post)) {
|
||||
callback = post;
|
||||
post = null;
|
||||
}
|
||||
var xhr = new this.XHR();
|
||||
xhr.open(method, url, true);
|
||||
xhr.onreadystatechange = function() {
|
||||
|
||||
@@ -77,7 +77,18 @@ function Compiler(textMarkup, attrMarkup, directives, widgets){
|
||||
Compiler.prototype = {
|
||||
compile: function(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){
|
||||
element = jqLite(element);
|
||||
var scope = parentScope && parentScope.$eval ?
|
||||
|
||||
@@ -192,6 +192,10 @@ angularService('$route', function(location, params){
|
||||
angularService('$xhr', function($browser){
|
||||
var self = this;
|
||||
return function(method, url, post, callback){
|
||||
if (isFunction(post)) {
|
||||
callback = post;
|
||||
post = null;
|
||||
}
|
||||
if (post && isObject(post)) {
|
||||
post = toJson(post);
|
||||
}
|
||||
@@ -213,6 +217,10 @@ angularService('$xhr.bulk', function($xhr){
|
||||
callbacks = [],
|
||||
scope = this;
|
||||
function bulkXHR(method, url, post, callback) {
|
||||
if (isFunction(post)) {
|
||||
callback = post;
|
||||
post = null;
|
||||
}
|
||||
requests.push({method: method, url: url, data:post});
|
||||
callbacks.push(callback);
|
||||
}
|
||||
@@ -240,6 +248,10 @@ angularService('$xhr.bulk', function($xhr){
|
||||
angularService('$xhr.cache', function($xhr){
|
||||
var inflight = {};
|
||||
function cache(method, url, post, callback){
|
||||
if (isFunction(post)) {
|
||||
callback = post;
|
||||
post = null;
|
||||
}
|
||||
if (method == 'GET') {
|
||||
var data;
|
||||
if (data = cache.data[url]) {
|
||||
|
||||
@@ -192,22 +192,21 @@ angularWidget('NG:INCLUDE', function(element){
|
||||
function incrementChange(){ changeCounter++;}
|
||||
this.$watch(srcExp, incrementChange);
|
||||
this.$watch(scopeExp, incrementChange);
|
||||
scope.$onEval(function(){
|
||||
if (childScope) childScope.$eval();
|
||||
});
|
||||
this.$watch(function(){return changeCounter;}, function(){
|
||||
var src = this.$eval(srcExp),
|
||||
useScope = this.$eval(scopeExp);
|
||||
if (src) {
|
||||
scope.$browser.xhr('GET', src, function(code, response){
|
||||
scope.$xhr.cache('GET', src, function(code, response){
|
||||
element.html(response);
|
||||
childScope = useScope || createScope(scope);
|
||||
compiler.compile(element)(element, childScope);
|
||||
childScope.$init();
|
||||
scope.$root.$eval();
|
||||
});
|
||||
}
|
||||
});
|
||||
scope.$onEval(function(){
|
||||
if (childScope) childScope.$eval();
|
||||
});
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
4
test.sh
4
test.sh
@@ -1,2 +1,2 @@
|
||||
# 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
|
||||
# java -jar lib/jstestdriver/JsTestDriver.jar --tests all --config jsTestDriver-jquery.conf
|
||||
|
||||
@@ -276,9 +276,8 @@ describe("widget", function(){
|
||||
scope.childScope = createScope();
|
||||
scope.childScope.name = 'misko';
|
||||
scope.url = 'myUrl';
|
||||
scope.$browser.xhr.expect('GET', 'myUrl').respond('{{name}}');
|
||||
scope.$xhr.cache.data.myUrl = {value:'{{name}}'};
|
||||
scope.$init();
|
||||
scope.$browser.xhr.flush();
|
||||
expect(element.text()).toEqual('misko');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user