为Yii2提供的一些有用的库。例如,一个简化处理AJAX、模态框和从JavaScript代码显示消息的助手

v1.17.22 2024-07-17 18:17 UTC

This package is auto-updated.

Last update: 2024-09-17 18:41:56 UTC


README

不同的Yii2库,帮助你编写更少的代码并更好地组织。

JsHelper 用于简化处理AJAX、模态框和从JavaScript代码显示消息的常见任务。查看源代码以了解此库的目的。

请注意,此库主要用于我自己的使用,可能缺乏适当的文档和对事物整体解释,尽管我尽力以我的编码方式和写注释的方式记录一切。此外,请注意,即使我尽力保持代码向后兼容,功能也可能在没有适当通知的情况下更改。

安装

安装此扩展的首选方式是通过 composer

首先安装Yii2,然后

composer require winternet-studio/yii2-libs

使用方法

一旦扩展安装完成,只需在您的代码中通过

<?php
use winternet\yii2\JsHelper;

echo JsHelper::initAjax();  //call within <body>
echo JsHelper::standardModal(['id' => 'StandardModal2']);
?>

JavaScript代码

// To make an AJAX call:

appJS.ajax({
	url: '/your/url',
	type: 'POST',
	dataType: 'json',
	data: {var1: 'something', var2: 'something else'}
});

// To show a standard modal (either initAjax() or initModal() must have been called beforehand):

appJS.showModal('Here goes the <b>content</b> for your modal.');

// To show a modal, specifying more options:

appJS.showModal({
	title: 'Modal title goes here',
	html: 'Here goes the <b>content</b> for your modal.',
	customModalSelector: '#StandardModal2',
	openCallback: function(modalRef) {
		// code for before modal is being opened
		// NOTE: not reexecuted when a closed modal is shown again (set up standard Bootstrap modal events for that)
	},
	openedCallback: function(modalRef) {
		// code for after modal has been opened
		// NOTE: not reexecuted when a closed modal is shown again (set up standard Bootstrap modal events for that)
	},
	closeCallback: function(modalRef) {
		// code for before modal is being closed
		// NOTE: not reexecuted when a closed modal is shown again (set up standard Bootstrap modal events for that)
	},
	closedCallback: function(modalRef) {
		// code for after modal has been closed
		// NOTE: not reexecuted when a closed modal is shown again (set up standard Bootstrap modal events for that)
	}
});

查看源代码以了解如何以及为什么使用这些库。