国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

實例:YII2框架中使用yii.js實現的post請求

2024-05-04 21:50:42
字體:
來源:轉載
供稿:網友

yii2提供了很多幫助類,比如Html、Url、Json等,可以很方便的實現一些功能,下面簡單說下這個Html。用yii2寫view時時經常會用到它,今天在改寫一個頁面時又用到了它。它比較好用的地方就在于,它不僅僅是生成一個簡單的html標簽,結合yii2自己的靜態資源文件yii.js可以很方便的實現一個post請求。

yii2將這些功能都做好了封裝,只要在合適的地方調用它的方法就可以了,可以說yii2是個可以開箱即用的框架,你可以用它很快的實現一個需要的功能:比如在頁面中放置一個刪除按鈕,點擊按鈕發送post請求,彈出確認對話框。如果沒有yii/helpers/Html類和yii.js,那么你需要寫大量的js/jquery來實現這個功能。如果用yii2的話,下面的代碼就可以實現:

  1. <?= Html::a( 
  2.  
  3.   '刪除'
  4.  
  5.   [ 
  6.  
  7.     'delete'
  8.  
  9.     'id' => $id
  10.  
  11.   ], 
  12.  
  13.   [ 
  14.  
  15.     'data' => [ 
  16.  
  17.       'confirm' => '你確定要刪除嗎?'
  18.  
  19.       'method' => 'post'
  20.  
  21.     ], 
  22.  
  23.   ] 
  24.  
  25.  
  26. ?> 

它會在頁面中生成下面一段html代碼:

刪除:

點擊這個按鈕會彈出對話框,確認刪除后會發送post請求。那么這個post請求是如何發送的呢?到現在為止可是一段js代碼都沒寫呢。

yii2框架隱藏了技術實現的細節,post請求的實現在yii.js中。在yii.js中,定義了window.yii對象,并初始化了window.yii對象的initModule方法:

  1. window.yii = (function($) { 
  2.  
  3.   varpub = { 
  4.  
  5.     // 定義了處理事件的方法,比如下面這個: 
  6.  
  7.     confirm:function(message, ok, cancel) { 
  8.  
  9.       if(window.confirm(message)) { 
  10.  
  11.         !ok || ok(); 
  12.  
  13.       }else
  14.  
  15.         !cancel || cancel(); 
  16.  
  17.       } 
  18.  
  19.     }, 
  20.  
  21.     handleAction:function($e, event) { 
  22.  
  23.       var$form = $e.attr('data-form') ? $('#'+ $e.attr('data-form')) : $e.closest('form'), 
  24.  
  25.       method = !$e.data('method') && $form ? $form.attr('method') : $e.data('method'), 
  26.  
  27.       // 其他省略 
  28.  
  29.     }, 
  30.  
  31.     // 其他省略 
  32.  
  33.   }; 
  34.  
  35.   // 初始化模塊 
  36.  
  37.   initModule:function(module) { 
  38.  
  39.     if(module.isActive !== undefined && !module.isActive) { 
  40.  
  41.       return
  42.  
  43.     } 
  44.  
  45.     if($.isFunction(module.init)) { 
  46.  
  47.       module.init(); 
  48.  
  49.     } 
  50.  
  51.     $.each(module,function() { 
  52.  
  53.       if($.isPlainObject(this)) { 
  54.  
  55.         pub.initModule(this); 
  56.  
  57.       } 
  58.  
  59.     }); 
  60.  
  61.   }, 
  62.  
  63.   // 初始化方法 
  64.  
  65.   init:function() { 
  66.  
  67.     initCsrfHandler(); 
  68.  
  69.     initRedirectHandler(); 
  70.  
  71.     initAssetFilters(); 
  72.  
  73.     initDataMethods(); 
  74.  
  75.   }, 
  76.  
  77.   returnpub; 
  78.  
  79. })(window.jQuery); 
  80.  
  81.    
  82.  
  83. window.jQuery(function() { 
  84.  
  85.   window.yii.initModule(window.yii); 
  86.  
  87. }); 

其中上面的initDataMethods()會調用pub.handleAction方法:

  1. functioninitDataMethods() { 
  2.  
  3.   varhandler =function(event) { 
  4.  
  5.     var$this= $(this), 
  6.  
  7.       method = $this.data('method'), 
  8.  
  9.       message = $this.data('confirm'), 
  10.  
  11.       form = $this.data('form'); 
  12.  
  13.     if(method === undefined && message === undefined && form === undefined) { 
  14.  
  15.       returntrue; 
  16.  
  17.     } 
  18.  
  19.     if(message !== undefined) { 
  20.  
  21.       $.proxy(pub.confirm,this)(message,function() { 
  22.  
  23.         pub.handleAction($this, event); 
  24.  
  25.       }); 
  26.  
  27.     }else
  28.  
  29.       pub.handleAction($this, event); 
  30.  
  31.     } 
  32.  
  33.     event.stopImmediatePropagation(); 
  34.  
  35.     returnfalse; 
  36.  
  37.   }; 
  38.  
  39.   // handle data-confirm and data-method for clickable and changeable elements 
  40.  
  41.   $(document).on('click.yii', pub.clickableSelector, handler) 
  42.  
  43.     .on('change.yii', pub.changeableSelector, handler); 
  44.  

可以看到這個方法會獲取上面生成的a標簽的data屬性值,然后交給handlerAction來處理。handlerAction通過動態生成一個form來處理各種請求,最后通過觸發submit事件來提交。

  1. $form = $(' 
  2.  
  3. ', {method: method, action: action}); 
  4.  
  5. vartarget = $e.attr('target'); 
  6.  
  7. if(target) { 
  8.  
  9.   $form.attr('target', target); 
  10.  
  11.  
  12. if(!/(get|post)/i.test(method)) { 
  13.  
  14.   $form.append($('', {name:'_method', value: method, type:'hidden'})); 
  15.  
  16.   method ='post' 
  17.  
  18.   $form.attr('method', method); 
  19.  
  20.  
  21. if(/post/i.test(method)) { 
  22.  
  23.   varcsrfParam = pub.getCsrfParam(); 
  24.  
  25.   if(csrfParam) { 
  26.  
  27.     $form.append($('', {name: csrfParam, value: pub.getCsrfToken(), type:'hidden'})); 
  28.  
  29.   } 
  30.  
  31.  
  32. $form.hide().appendTo('body'); 

其他省略:

PS:做項目用框架很方便,但是框架用的久了,就容易把基本的技術給忘掉了。還是要打好基礎呀,這樣不管用什么框架都不至于用得云里霧里的。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 方山县| 邹城市| 绥宁县| 揭东县| 禄丰县| 洪泽县| 扎囊县| 清丰县| 怀来县| 容城县| 阿尔山市| 吉木乃县| 永丰县| 阿拉善盟| 平安县| 青田县| 金平| 常州市| 桃园市| 龙川县| 赤城县| 共和县| 阿拉善盟| 象州县| 青岛市| 内黄县| 达州市| 静安区| 个旧市| 青河县| 河北区| 三江| 祁东县| 遂宁市| 饶平县| 凤台县| 库尔勒市| 太仓市| 南靖县| 班玛县| 栖霞市|