dougou5844 2015-11-27 09:24
浏览 21
已采纳

OctoberCMS表单请求未提交

I'm following this tutorial to create a simple OctoberCMS plugin. Here is content of /acme/demo/components/todo/default.htm page:

{% 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">&times;</button>
                </li>
            {% endfor %}
        </ul>
    </div>
</form>

And here is the content of /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();
    }

}

The problem is that onAddItem is never called. It seems that the form is not submitted properly when I add a new item. Does anybody know how I can fix this? I thought maybe the ajax libraries are missing, so I included {% framework %} as well, which again didn't help:

{% set tasks = __SELF__.tasks %}
{% framework %}

<form data-request="{{ __SELF__ }}::onAddItem" data-request-success="$('#inputItem').val('success')">

Please note that my model is working and $this->tasks = Task::lists('title'); returns the list of tasks.

  • 写回答

1条回答 默认 最新

  • doumu1951 2015-12-01 02:25
    关注

    The problem was that I should have included the jquery file as well:

    <script src="{{ [
        'assets/javascript/jquery.js',
    ]|theme }}"></script>
    
    {% framework %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应