我有一个Backbone应用程序,使用Codeigniter作为后端。现在,要显示我的数据库中的数据,我使用RESTful API获取数据并将其作为JSON传递给Backbone。我想要获取的数据是来自A-Z的Bandnames,它始终是相同的数据:band_id和band_name。因此,为此目的,我想创建一个单独的Backbone Model,它应该告诉Collections要处理哪些数据。所以在我的Codeigniter设置中,在application
- folder-> controllers
- 文件夹中我创建了一个api
- 文件夹,在那里我创建了一个artist.php
- 文件,基本上看起来像这样:
<?php
require(APPPATH.'libraries/REST_Controller.php');
class artists extends REST_Controller{
// BANDS WITH LETTER A
public function a_get()
{
$this->load->database();
$sql = "SELECT band_id, band_name FROM `bands` WHERE bands_name LIKE 'A%' LIMIT 100";
$query = $this->db->query($sql);
$data = $query->result();
if($data) {
$this->response($data, 200);
} else {
$this->response(array('error' => 'Couldn\'t find any artists with letter a!'), 404);
}
}
public function a_put()
{
// Update
}
public function a_post()
{
// Post/insert
}
public function a_delete()
{
// delete
}
// BANDS WITH LETTER B
public function b_get()
{
$this->load->database();
$sql = "SELECT band_id, band_name FROM `bands` WHERE band_name LIKE 'B%' LIMIT 10";
$query = $this->db->query($sql);
$data = $query->result();
if($data) {
$this->response($data, 200);
} else {
$this->response(array('error' => 'Couldn\'t find any artists with letter b!'), 404);
}
}
public function b_put()
{
// Update
}
public function b_post()
{
// Post/insert
}
public function b_delete()
{
// delete
}
... LETTER C code
... LETTED D code etc. etc.
?>
My Backbone Model看起来像这样:
function($, Backbone) {
var BandModel = Backbone.Model.extend({
url: "../myproejct/index.php/api/bands/",
defaults: {
"band_id": ' ',
"band_name": ' '
}
});
return BandModel;
});
一个集合看起来像这样:
function(Backbone, BandModel) {
var BandCollectionA = Backbone.Collection.extend({
model: BandModel,
url: "../myproject/index.php/api/bands/a"
});
return BandCollectionA;
});
所以我的问题是:我可以这样做吗?我可以使用artists.php
这样的API吗?还有其他选择吗?