欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

YII2框架中使用yii.js實(shí)現(xiàn)的post請(qǐng)求

 更新時(shí)間:2017年04月09日 09:59:35   作者:yangtoude  
本文給大家介紹的是簡(jiǎn)單分析下用yii2的yii\helpers\Html類和yii.js實(shí)現(xiàn)的post請(qǐng)求的方法,非常的簡(jiǎn)單,有需要的小伙伴可以參考下

yii2提供了很多幫助類,比如Html、Url、Json等,可以很方便的實(shí)現(xiàn)一些功能,下面簡(jiǎn)單說下這個(gè)Html。用yii2寫view時(shí)時(shí)經(jīng)常會(huì)用到它,今天在改寫一個(gè)頁面時(shí)又用到了它。它比較好用的地方就在于,它不僅僅是生成一個(gè)簡(jiǎn)單的html標(biāo)簽,結(jié)合yii2自己的靜態(tài)資源文件yii.js可以很方便的實(shí)現(xiàn)一個(gè)post請(qǐng)求。

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

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

它會(huì)在頁面中生成下面一段html代碼:

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

點(diǎn)擊這個(gè)按鈕會(huì)彈出對(duì)話框,確認(rèn)刪除后會(huì)發(fā)送post請(qǐng)求。那么這個(gè)post請(qǐng)求是如何發(fā)送的呢?到現(xiàn)在為止可是一段js代碼都沒寫呢。

yii2框架隱藏了技術(shù)實(shí)現(xiàn)的細(xì)節(jié),post請(qǐng)求的實(shí)現(xiàn)在yii.js中。在yii.js中,定義了window.yii對(duì)象,并初始化了window.yii對(duì)象的initModule方法:

window.yii = (function ($) {
  var pub = {
    // 定義了處理事件的方法,比如下面這個(gè):
    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()會(huì)調(diào)用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);
  }

可以看到這個(gè)方法會(huì)獲取上面生成的a標(biāo)簽的data屬性值,然后交給handlerAction來處理。handlerAction通過動(dòng)態(tài)生成一個(gè)form來處理各種請(qǐng)求,最后通過觸發(fā)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:做項(xiàng)目用框架很方便,但是框架用的久了,就容易把基本的技術(shù)給忘掉了。還是要打好基礎(chǔ)呀,這樣不管用什么框架都不至于用得云里霧里的。

相關(guān)文章

最新評(píng)論