zitec/js-data-bundle
Composer 安装命令:
composer require zitec/js-data-bundle
包简介
This bundle transports pieces of information from PHP to JS.
README 文档
README
This bundle transports pieces of information from PHP to JavaScript.
How to use
Inject the Zitec\JSDataBundle\Service\DataHandler service into your controller action and add the data.
<?php namespace App\Controller; use Zitec\JSDataBundle\Service\DataHandler; class SimpleController { public function indexAction(DataHandler $jsDataHandler) { $jsDataHandler->add('[Colors]', ['Red', 'Green', 'Blue']); return $this->render('index.html.twig'); } }
Render the data in the template.
<head> <head></head> <body> {{ zitec_js_data_get_all() }} </body> </html>
Bundle components
DataCollectorInterface
Represents an interface which all JS data collectors must implement. A data collector is nothing more than an object whose role is to collect data from different components. At a given point, the collected data will be fetched in a template and assigned to a JS variable. Basically, the data collector transports pieces of information from PHP to JS.
Methods
- getAll(): Fetches the collected data.
- add($path, $value): Sets a value at the given path in the data object.
- merge(array $data): Merges the given data set to the existing set.
DataCollectEvent
Represents an event which is triggered when data is demanded for the first time from the data handler service. Other bundles can listen to it in order to alter the JS data set.
DataHandler
A service which handles the data sent to the client's scripts. Bundles throughout the application may use this service to add their own or alter the information from the JS data set.
Properties
- dataCollector: the data collector used to collect JS data.
- eventDispatcher: the event dispatcher service.
- collected (bool): flag which marks if data was collected from other bundles.
Methods
- add($path, $value): adds the given value into the data collector.
- merge(array data): merges the given data set into the collector.
- getAll(): fetches the collected data.
- collect(): collects data from other bundles by dispatching the data_collect event. The event is triggered only once.
JSDataExtension:
Twig extension which makes the JS data handler available in the templates through functions which are proxies to its methods.
Properties
- $dataHandler: the data handler service.
Methods
- getFunctions()
- zitec_js_data_add call addFunction;
- zitec_js_data_merge call mergeFunction;
- zitec_js_data_get_all call getAllFunction;
- getName(): return ‘zitec_js_data_extensions’;
- addFunction($path, $value): call $dataHandler->add($path, $value);
- mergeFunction(array $data): call $dataHandler->merge($data);
- getAllFunction($jsonEncodeOptions):
- $jsonEncodeOptions: default value is 0;
- The data is outputted directly in the JSON format,so you can assign it to a JS variable.
License
This bundle is covered by the MIT license. See LICENSE for details.
zitec/js-data-bundle 适用场景与选型建议
zitec/js-data-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 06 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 zitec/js-data-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zitec/js-data-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-04