我正在学习基于模型、视图、控制器范式的 Laravel PHP 框架。我一直在尝试将 AJAX 合并到我的简单入门应用程序中……一个电话记录器。这是我通常放弃的地方。但我拒绝!
所以我有我的电话模型:
class Phonecall extends Eloquent
{
// Creates an instance of the database object
}
这是我的电话表:
mysql> desc phonecalls;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| who | varchar(200) | NO | | NULL | |
| what | varchar(200) | NO | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| initiator | varchar(200) | NO | | NULL | |
| info | text | NO | | NULL | |
+------------+------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
我的视图列出了数据库中当前的所有呼叫(由谁和什么):
<!doctype html>
<html>
<head>
<title>Title</title>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">
</script>
</head>
<body>
<h1>Welcome</h1>
<p>Here's a list of recent phonecalls</p>
<ul class="call-list">
@foreach($phonecalls as $call)
<li>{{ $call->who }} - {{ $call->what }} -
<a href="phonecalls/show/{{ $call->id }}">Show</a> |
{{ HTML::link('phonecalls/delete/'.$call->id, 'Delete') }} |
{{ HTML::link('phonecalls/update/'.$call->id, 'Update') }}
</li>
</ul>
{{-- Placeholder for AJAX content --}}
<div id="call-info">
</div>
</body>
</html>
这是我的简单控制器:
class Phonecalls_Controller extends Base_Controller {
// Use $this within a method the same way you
// would use the object name outside of the class
public function get_index()
{
$phonecalls = Phonecall::all();
return View::make('phonecalls.index')->with('phonecalls', $phonecalls);
}
// ************************************
// DISPLAY CALL INFO
public function get_show($call_id)
{
$call = Phonecall::find($call_id);
// WHAT GOES HERE?
}
我希望用户能够单击视图中的“显示”并在...中显示呼叫信息
<div id="call-info">
</div>
视图中的标签。
更新:
这就是它的作用...
我的 get_show() 方法
// ************************************
// SHOW CALL INFO
public function get_show($call_id)
{
$call = Phonecall::find($call_id);
return $call->info;
}
我的 Javascript
//************************************
// Phonecall AJAX Example
//************************************
$(document).ready(function() {
$('.call-list > li > a').click(function(e) { // e=event
e.preventDefault();
var id = $(this).attr('id');
$.get(BASE+'/phonecalls/show/'+id, function(data) {
$("#call-info").html(data);
})
});