定制 elefant/app-wires 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

elefant/app-wires

Composer 安装命令:

composer require elefant/app-wires

包简介

Wires app for the Elefant CMS auto-connects Alpine.js for easy single-page app development

README 文档

README

Inspired by Laravel Livewire, this app provides simple template extensions for the Elefant CMS that wires any handler and its view template up to Alpine.js, providing a few interesting features:

  • Server-side rendering of the initial request
  • Converts Elefant's {{var}} tags to Alpine.js <span x-text="var"> tags
  • Provides automatic server-side callbacks via a handful of custom template tags

Installation

Via composer:

composer require elefant/app-wires

Usage

Here's how it works:

In your handler (apps/myapp/handlers/example.php):

<?php

// Initialize and inject the controller
Wires::init ($this);

// Set your default values here
$defaults = [
	'fname' => 'First',
	'lname' => 'Last'
];

// Handle the request, providing an API callback handler
echo Wires::handle ($defaults, function ($response) {
	// Process API requests here
});

Then in your accompanying view template (apps/myapp/views/example.html):

<div {{_wire_}}>
	<p>{{lname}}, {{fname}}</p>
	
	<p>
		<button {{_wire_button_}} data-fname="John" data-lname="Doe">Doe, John</button>
		<button {{_wire_button_}} data-fname="First" data-lname="Last">Reset</button>
	</p>
</div>

Alternately, you can modify the response data on the server-side before returning it:

echo Wires::handle ($defaults, function ($response) {
	$response['fname'] = 'Jane';
	$response['lname'] = 'Doe';
	return $response;
});

And in case of an error on the server, you can use:

echo Wires::handle ($defaults, function ($response) {
	return Wires::error (500, 'Nope');
});

Behind the scenes, the <div {{_wire_}}> connects Alpine.js to the template data.

If you want to define your own functions for use with Alpine.js, you can initialize the component like this instead, using the {{_wire_data_}} tag:

<div x-data="myComponent()">
	<p x-text="name()"></p>
</div>

<script>
function myComponent() {
	return Object.assign ({{_wire_data_}}, {
		name() {
			return this.fname + ' ' + this.lname;
		}
	});
}
</script>

In addition to {{_wire_}} and {{_wire_data_}}, three custom tags can be used to connect buttons, inputs, and links to make API calls to the same endpoint:

  • {{_wire_button_}} - Wires a <button> element, passing its data-* attributes to the server.
  • {{_wire_input_}} - Wires an <input>, <select>, or <textarea> element to send live updates to the server as you type or make a selection, passing its name attribute and value to the server.
  • {{_wire_link_}} - Wires an <a> link, passing its data-* attributes to the server.

Other features of Alpine.js should work just the same too, although loops should use pure Alpine.js instead of Elefant's template syntax for looping. For example:

$default = [
	'list' => ['One', 'Two', 'Three']
];

Can be connected like this:

<ul>
	<template x-for="item in list">
		<li x-text="item"></li>
	</template>
</ul>

Modifying the list on either the client or the server will update the displayed list of elements.

Convenience methods

Wires defines two additional convenience methods for communicating with the server:

  • this.sync() - Post the component's current state to the server-side handler.
  • this.send(params) - Post the specified request data to the server-side handler.

The first serializes and sends the entire component state, while the second only sends the specific data that it's given.

These can be used to send the updated component state to the server for storage and additional processing after modifying it locally.

Example usage:

function toDoList() {
	return Object.assign ({{_wire_data_}}, {
		toggleToDoCompleted(index) {
			this.todos[index].completed = !this.todos[index].completed;
			this.sync(); // Send updated state to the server
		}
	});
}

Using this.send(params), you could also send just the item that changed, which you would also need to check for on the server-side.

function toDoList() {
	return Object.assign ({{_wire_data_}}, {
		toggleToDoCompleted(index) {
			this.todos[index].completed = !this.todos[index].completed;
			this.send({updated: index, todo: this.todos[index]});
		}
	});
}

Demos

Once you've installed this app, go to /wires/demos in your Elefant CMS installation and log in as a site administrator to see some working demos.

You can inspect the source code for the demos under apps/wires/handlers/demos and apps/wires/views/demos in your Elefant CMS installation.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固