2012-07-02 31 views
3

我有一個標準的CI Web應用程序,但我決定使用主幹來獲取混亂的JavaScript。我有一堆序列化的表單/ jQuery AJAX請求到各種控制器方法:認證,change_password,register_member,request_new_password等,並不完全理解REST如何工作。我正在使用Phil Sturgeon的CI的REST庫Backbone JS和CodeIgniter REST服務器

每個骨幹模型都應該有不同的api url嗎?我應該怎麼稱呼控制器方法?

<?php 
    require(APPPATH.'/libraries/REST_Controller.php'); 
    class RestApi extends REST_Controller 
    { 
     function get() 
     { 

但它只是404s。

我只是沒有得到如何取代路由到基於少數的HTTP方法我的舊方法的五十。骨幹模型的名稱是否需要匹配服務器端的某些內容?

回答

1

url - 模型的屬性應該匹配服務器端的'url',它返回組成模型屬性的JSON。 Backbone.js具有默認的功能,即將模型的集合url與它的id屬性進行匹配。通過覆蓋urlRoot-函數可以放棄收集URL的要求,以便在集合之外操作模型。

如果你想成爲獨立的ID -attribute的,以及,你重寫前人的精力在url -attribute /功能,能夠爲自己的URL匹配到模型中的服務器上,這樣的:

url: 'path/to/my/model' 

url: function() { // Define the url as a function of some model properties 
    var path = this.model_root + '/' + 'some_other_url_fragment/' + this.chosen_model_identifier; 
    return path; 
} 
4

您必須爲您的函數命名爲index_HTTPMETHOD。在你的例子中它將是:

class RestApi extends REST_Controller { 
    // this will handle GET http://.../RestApi 
    function index_get() { 
    } 

    // additionally this will handle POST http://.../RestApi 
    function index_post() { 
    } 
    // and so forth 

    // if you want to POST to http://.../RestApi/somefunc 
    function somefunc_post() { 
    } 

}