我有一個Backbone應用程序,使用Codeigniter作爲後端。現在,要顯示來自我的數據庫的數據,我使用RESTful API獲取數據並將其作爲JSON傳遞給Backbone。我想要獲取的數據是來自A-Z的頻帶名稱以及始終相同的數據:band_id和band_name。因此,爲了這個目的,我想創建一個單一的骨幹模型,它應該告訴集合要處理的數據。所以在我笨的設置,在application
-folder->controllers
-folder我創建了一個api
-folder,我在那裏創建了一個artist.php
-file,基本上是這樣的:BackboneJS + Codeigniter - RESTful API - 如何創建一個單一的大API?
<?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.
?>
我的骨幹示範會是什麼樣子這樣的:
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;
});
所以我的問題是:我可以這樣做嗎?我可以像這樣做API,artists.php
嗎?有替代品嗎?