我正在按照本教程创建一个简单的 OctoberCMS 插件。以下是/acme/demo/components/todo/default.htm
页面内容:
{% set tasks = __SELF__.tasks %}
<form data-request="{{ __SELF__ }}::onAddItem" data-request-success="$('#inputItem').val('success')">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Tasks assigned to: {{__SELF__.name}} </h3>
</div>
<div class="panel-body">
<div class="input-group">
<input name="task" type="text" id="inputItem" class="form-control" value=""/>
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">Add</button>
</span>
</div>
</div>
<ul class="list-group" id="result">
{% for task in tasks %}
<li class="list-group-item">
{{ task }}
<button class="close pull-right">×</button>
</li>
{% endfor %}
</ul>
</div>
</form>
这里是内容/acme/demo/components/Todo.php
:
<?php namespace Acme\Demo\Components;
use Cms\Classes\ComponentBase;
use Acme\Demo\Models\Task;
class Todo extends ComponentBase
{
/**
* This is a person's name.
* This variable will be available in the page as a twig variable
* @var string
*/
public $name;
/**
* The collection of tasks.
* @var array
*/
public $tasks;
public function componentDetails()
{
return [
'name' => 'Todo Component',
'description' => 'A database driven TODO list'
];
}
public function defineProperties()
{
return [];
}
public function init()
{
// This will execute when the component is first initialized, including AJAX events.
}
public function onRun()
{
$this->name = 'Meysam';
$this->tasks = Task::lists('title');
}
public function onAddItem()
{
$taskName = post('task');
$task = new Task();
$task->title = $taskName;
$task->save();
}
}
问题是onAddItem
从未调用过。添加新项目时,表单似乎未正确提交。有人知道我该如何解决这个问题吗?我想也许缺少 ajax 库,所以我也包括在内{% framework %}
,这又没有帮助:
{% set tasks = __SELF__.tasks %}
{% framework %}
<form data-request="{{ __SELF__ }}::onAddItem" data-request-success="$('#inputItem').val('success')">
请注意,我的模型正在运行并$this->tasks = Task::lists('title');
返回任务列表。