백본 응용 프로그램에서 Codeigniter를 백엔드로 사용합니다. 자, 내 데이터베이스에서 데이터를 표시하려면 RESTful API를 사용하여 데이터를 가져 와서 Backbone에 JSON으로 전달하십시오. 가져 오려는 데이터는 A-Z의 밴드 이름과 항상 동일한 데이터 인 band_id와 band_name입니다. 그래서 그 목적을 위해 하나의 백본 모델을 만들어 컬렉션에 어떤 데이터를 처리해야하는지 알려야합니다.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.
?>
내 백본 모델과 같습니다 그래서 내 CodeIgniter의 설정에서 application
-folder->controllers
- 폴더에 나는 기본적으로이처럼 보이는 artist.php
- 파일을 만든 api
- 폴더를 생성 이 :
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
과 같은 API를 사용할 수 있습니까? 대안이 있습니까?