我正在使用Phalcon框架,我有一个控制器' Post'与行动'喜欢':
视图/布局/ post.volt
{%发布帖子%}
< H1> {{post.content}}< /h1>
< H2> {{post.like}}< /h2>
< a href ="#"的onclick ="像();" data-id =" {{post.id}}" >像< /a>
{%endfor%}
<! - - >
<脚本>
var like = function() { var id = /* ... */; // get id of post which raise 'like' event $.ajax({ dataType: 'json', url: 'post/like/' + id }).done(function() { /* Do something meaningful */ });
< /script>
控制器/ PostController.php
/ * ... * /
public function indexAction(){
$this->view->posts = /* ... */; // read from database to show the view
}
公共函数likeAction($ id){
// change 'like' of corresponding post with certain 'id' foreach($this->view->posts as post) { if($post->id == $id) $post->like += 1; /* ... */ } // write to database, and re-render entire post view $this->view->posts = /* ... */; // I just assign 'view' again and hope it works :( $result = array('msg' => 'success'); $response = new Response(); return $response->setJsonContent($result);
}
/ * ... * /
但点击按钮后,一切都没有改变!我做错什么了吗?任何人都可以帮助我......提前感谢!
P / S:抱歉我的英语不好...... ^^答案 0 :(得分:0)
' $这 - >视图 - >帖子'仅在' indexAction'中设置。 当“喜欢行动”时,它是空的。