加载中...

外观模式


外观模式

正如我们早前在书中提过的, 没面模式为一个庞大的(可能更复杂的)代码结构提供了一个更简单的抽象接口。

门面在jQuery库中能够经常见到,它们为开发者处理DOM节点,动画或者令人特别感兴趣的跨域Ajax提供了简单的实现入口。

下面的代码是jQuery $.ajax()方法的门面:

  1. $.get( url, data, callback, dataType );
  2. $.post( url, data, callback, dataType );
  3. $.getJSON( url, data, callback );
  4. $.getScript( url, callback );

这些方法背后真正执行的代码是这样的:

  1. // $.get()
  2. $.ajax({
  3. url: url,
  4. data: data,
  5. dataType: dataType
  6. }).done( callback );
  7. // $.post
  8. $.ajax({
  9. type: "POST",
  10. url: url,
  11. data: data,
  12. dataType: dataType
  13. }).done( callback );
  14. // $.getJSON()
  15. $.ajax({
  16. url: url,
  17. dataType: "json",
  18. data: data,
  19. }).done( callback );
  20. // $.getScript()
  21. $.ajax({
  22. url: url,
  23. dataType: "script",
  24. }).done( callback );

更有趣的是,上面代码中的门面实际上是它们自身具有的能力,它们隐藏了代码背后很多复杂的操作。

这是因为jQuery.ajax()在jQuery核心代码中的实现是一段不平凡的代码,至少是这样的。至少它规范了XHR(XMLHttpRequest)之间的差异而且让我们能够简单的执行常见的HTTP动作(比如:get、post等),以及处理延迟等等。

由于显示与上面所讲的门面相关的代码将会占据整个章节,这里仅仅给出了jQuery核心代码中规划化XHR的代码:

  1. // Functions to create xhrs
  2. function createStandardXHR() {
  3. try {
  4. return new window.XMLHttpRequest();
  5. } catch( e ) {}
  6. }
  7. function createActiveXHR() {
  8. try {
  9. return new window.ActiveXObject( "Microsoft.XMLHTTP" );
  10. } catch( e ) {}
  11. }
  12. // Create the request object
  13. jQuery.ajaxSettings.xhr = window.ActiveXObject ?
  14. /* Microsoft failed to properly
  15. * implement the XMLHttpRequest in IE7 (can't request local files),
  16. * so we use the ActiveXObject when it is available
  17. * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
  18. * we need a fallback.
  19. */
  20. function() {
  21. return !this.isLocal && createStandardXHR() || createActiveXHR();
  22. } :
  23. // For all other browsers, use the standard XMLHttpRequest object
  24. createStandardXHR;
  25. ...

下面的代码也处于实际的jQuery XHR(jqXHR)实现的上层,它是我们实际上经常打交道的方便的门面:

  1. // Request the remote document
  2. jQuery.ajax({
  3. url: url,
  4. type: type,
  5. dataType: "html",
  6. data: params,
  7. // Complete callback (responseText is used internally)
  8. complete: function( jqXHR, status, responseText ) {
  9. // Store the response as specified by the jqXHR object
  10. responseText = jqXHR.responseText;
  11. // If successful, inject the HTML into all the matched elements
  12. if ( jqXHR.isResolved() ) {
  13. // Get the actual response in case
  14. // a dataFilter is present in ajaxSettings
  15. jqXHR.done(function( r ) {
  16. responseText = r;
  17. });
  18. // See if a selector was specified
  19. self.html( selector ?
  20. // Create a dummy div to hold the results
  21. jQuery("
  22. <div>
  23. ")
  24. // inject the contents of the document in, removing the scripts
  25. // to avoid any 'Permission Denied' errors in IE
  26. .append(responseText.replace(rscript, ""))
  27. // Locate the specified elements
  28. .find(selector) :
  29. // If not, just inject the full result
  30. responseText );
  31. }
  32. if ( callback ) {
  33. self.each( callback, [ responseText, status, jqXHR ] );
  34. }
  35. }
  36. });
  37. return this;
  38. }
  39. </div>

还没有评论.