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

首頁 > 語言 > PHP > 正文

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

2024-05-04 23:56:37
字體:
來源:轉載
供稿:網友

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

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

 // html代碼 <?= Html::a(   '刪除',   [     'delete',     'id' => $id,   ],   [     'data' => [       'confirm' => '你確定要刪除嗎?',       'method' => 'post',     ],   ] ) ?> // html代碼

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

<a href="delete?id=1" rel="external nofollow" data-confirm="你確定要退出嗎?" data-method="post">刪除</a>

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

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

window.yii = (function ($) {  var pub = {    // 定義了處理事件的方法,比如下面這個:    confirm: function (message, ok, cancel) {      if (window.confirm(message)) {        !ok || ok();      } else {        !cancel || cancel();      }    },    handleAction: function ($e, event) {      var $form = $e.attr('data-form') ? $('#' + $e.attr('data-form')) : $e.closest('form'),      method = !$e.data('method') && $form ? $form.attr('method') : $e.data('method'),      // 其他省略    },    // 其他省略  };  // 初始化模塊  initModule: function (module) {    if (module.isActive !== undefined && !module.isActive) {      return;    }    if ($.isFunction(module.init)) {      module.init();    }    $.each(module, function () {      if ($.isPlainObject(this)) {        pub.initModule(this);      }    });  },  // 初始化方法  init: function () {    initCsrfHandler();    initRedirectHandler();    initAssetFilters();    initDataMethods();  },  return pub;})(window.jQuery);window.jQuery(function () {  window.yii.initModule(window.yii);});

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

  function initDataMethods() {    var handler = function (event) {      var $this = $(this),        method = $this.data('method'),        message = $this.data('confirm'),        form = $this.data('form');      if (method === undefined && message === undefined && form === undefined) {        return true;      }      if (message !== undefined) {        $.proxy(pub.confirm, this)(message, function () {          pub.handleAction($this, event);        });      } else {        pub.handleAction($this, event);      }      event.stopImmediatePropagation();      return false;    };    // handle data-confirm and data-method for clickable and changeable elements    $(document).on('click.yii', pub.clickableSelector, handler)      .on('change.yii', pub.changeableSelector, handler);  }

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

// 其他省略$form = $('<form/>', {method: method, action: action});var target = $e.attr('target');if (target) {  $form.attr('target', target);}if (!/(get|post)/i.test(method)) {  $form.append($('<input/>', {name: '_method', value: method, type: 'hidden'}));  method = 'post';  $form.attr('method', method);}if (/post/i.test(method)) {  var csrfParam = pub.getCsrfParam();  if (csrfParam) {    $form.append($('<input/>', {name: csrfParam, value: pub.getCsrfToken(), type: 'hidden'}));  }}$form.hide().appendTo('body');

// 其他省略

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


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 宣恩县| 金溪县| 柳江县| 东乌珠穆沁旗| 七台河市| 峡江县| 邵阳市| 金坛市| 萍乡市| 甘谷县| 博爱县| 禄劝| 丰顺县| 平果县| 棋牌| 化德县| 延吉市| 来宾市| 灵武市| 龙井市| 宁河县| 科技| 同仁县| 马关县| 蓬溪县| 中牟县| 永泰县| 新营市| 鄢陵县| 英吉沙县| 织金县| 汪清县| 乐陵市| 长治县| 禹城市| 永丰县| 青浦区| 高清| 商丘市| 尼勒克县| 佛山市|