Skip to content

Commit

Permalink
add note
Browse files Browse the repository at this point in the history
  • Loading branch information
arthinking committed Nov 7, 2012
1 parent 1f5854c commit 00d63c9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Chapter07-The-Factory-Pattern/7.03 - XHR factory example.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ SimpleHandler.prototype = {
continue;
}
// If we reach this point, method[i] worked.
this.createXhrObject = methods[i]; // Memoize the method.
return methods[i];
this.createXhrObject = methods[i]; // Memoize the method. 记住该方法
return methods[i](); // 原书中的代码少了一对括号,这里需要多加一对括号才可以正确返回XMLHttpRequeset
}

// If we reach this point, none of the methods worked.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ function getBeerById(e) {
});
}

// 把事件处理的语句封装到回调函数中,现在我们通过接口而不是实现进行编程
// getBeerById并没有和事件对象捆绑在一起。
function getBeerById(id, callback) {
// Make request for beer by ID, then return the beer data.
asyncRequest('GET', 'beer.uri?id=' + id, function(resp) {
// callback response
callback(resp.responseText);
});
}

// 桥接器的使用:事件监听器回调函数
addEvent(element, 'click', getBeerByIdBridge);
function getBeerByIdBridge (e) {
getBeerById(this.id, function(beer) {
Expand Down
62 changes: 62 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<html>
<head>

</head>
<body>
<script>
/* AjaxHandler interface. */

// var AjaxHandler = new Interface('AjaxHandler', ['request', 'createXhrObject']);

/* SimpleHandler class. */

var SimpleHandler = function() {}; // implements AjaxHandler
SimpleHandler.prototype = {
request: function(method, url, callback, postVars) {
var xhr = this.createXhrObject();
xhr.onreadystatechange = function() {
if(xhr.readyState !== 4) return;
(xhr.status === 200) ?
callback.success(xhr.responseText, xhr.responseXML) :
callback.failure(xhr.status);
};
xhr.open(method, url, true);
if(method !== 'POST') postVars = null;
xhr.send(postVars);
},
createXhrObject: function() { // Factory method.
var methods = [
function() { return new XMLHttpRequest(); },
function() { return new ActiveXObject('Msxml2.XMLHTTP'); },
function() { return new ActiveXObject('Microsoft.XMLHTTP'); }
];

for(var i = 0, len = methods.length; i < len; i++) {
try {
methods[i]();
}
catch(e) {
continue;
}
// If we reach this point, method[i] worked.
this.createXhrObject = methods[i]; // Memoize the method. 记住该方法
return methods[i]();
}

// If we reach this point, none of the methods worked.
throw new Error('SimpleHandler: Could not create an XHR object.');
}
};

/* Usage. */

var myHandler = new SimpleHandler();
var callback = {
success: function(responseText) { alert('Success: ' + responseText); },
failure: function(statusCode) { alert('Failure: ' + statusCode); }
};
myHandler.request('GET', 'script.php', callback);

</script>
</body>
</html>

0 comments on commit 00d63c9

Please sign in to comment.