2014-10-28 69 views
0

綁定視圖與阿賈克斯我使用爾康框架,我有一個控制器「郵報」與「喜歡」的動作:Phalconphp使用伏模板

的意見/佈局/ post.volt

{%爲在後柱%}

<H1> {{post.content}} < ⁄ H1 >

<H2> {{post.like}} < ⁄ H2 >

< A HREF = 「#」 的onclick = 「像();」數據-ID = 「{{post.id}}」 >像< ⁄一個>

{%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 */ 

     }); 

< ⁄腳本>

控制器/ PostController.php

/* ... */

公共職能的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

'$這個 - >查看 - >崗位' 只在 '的indexAction' 設置。 當'likeAction'時它是空的。

+0

有什麼辦法可以處理?我只是想改變看法。我必須重新加載整個頁面,或者在從ajax捕獲響應數據後,通過javascript手動更改'post.like'? – hocvien6 2014-10-29 02:12:38

+0

[此代碼爲數據庫更新](http://pastebin.com/jM9vsk1g)。 如果需要更新到頁面,請在AJAX成功後使用javascript進行更新。 – alu 2014-10-29 06:46:56

+0

哇!我知道了!非常感謝:D ... – hocvien6 2014-10-29 07:27:22