novotna/async-control

异步控制渲染的特质

v1.1.0-RC1 2019-03-19 15:27 UTC

This package is not auto-updated.

Last update: 2024-09-25 08:50:32 UTC


README

异步控制渲染的特质。

Downloads total Build Status Latest Stable Version

用于从外部源加载数据或任何耗时控件非常有用。

AsyncControl

要求

安装

安装 PeckaDesign/AsyncControl 的最佳方式是使用 Composer

$ composer require pd/async-control

使用方法

特质

<?php
class CommentsControl extends Nette\Application\UI\Control {

	use Pd\AsyncControl\UI\AsyncControlTrait;

	public function render() {
		//control rendering
	}
}

如果您想调用除 render 之外的方法,请设置自定义渲染回调

<?php
$this->setAsyncRenderer([$this, 'customRender']);
//or
$this->setAsyncRenderer(function () {
	//control rendering
});

模板

{control comments:async}

或使用自定义消息

{control comments:async 'Show comments'}

前端

没有任何javascript,此特质将只显示加载按钮。使用简单的脚本,您可以在页面加载时自动加载所有异步组件,使用 nette ajax 扩展 src/assets/async.nette.ajax.js。或者,您可以实现自己的处理,例如在滚动页面时加载组件。如果您有很长的页面,这很有用。您不需要等到所有数据都加载完毕,您可以更快地显示主要数据。根据您的JS实现,附加数据将在页面加载后或按需加载。

配置

您可以在 bootstrap.php 或应用程序设置中设置用于加载链接的默认消息和属性

<?php
Pd\AsyncControl\UI\AsyncControlLink::setDefault('Load content', ['class' => ['btn', 'ajax']]);

services:
	application:
		setup:
			- Pd\AsyncControl\UI\AsyncControlLink::setDefault('Load content', {class: [btn, ajax]})

搜索引擎

要允许爬虫索引您的网站,您需要在您的页面上添加元标签。

<meta name="fragment" content="!" n:if="$presenter->getParameter('_escaped_fragment_') === NULL">

如果您在页面中放置 www.example.com,爬虫将临时将此URL映射到 www.example.com?_escaped_fragment_= 并从您的服务器请求此内容。然后,您的服务器应返回对应于 www.example.com 的HTML快照

当url中存在 _escaped_fragment_ 参数时,AsyncControlTrait 总是渲染其内容。

如果您想为禁用JS的页面访问者实现相同的行为,在noscript标签内添加额外的元标签

<noscript n:if="$presenter->getParameter('_escaped_fragment_') === NULL">
	<meta http-equiv="refresh" content="0;url=?_escaped_fragment_="/>
</noscript>

为了避免在您的服务器和爬虫上产生额外负载,仅在包含具有 AsyncControlTrait 特质的控件的页面上使用这些标签。