backbone.js DELETE 请求和 codeigniter Restserver 的问题 (phils)

发布于 2024-12-13 13:21:54 字数 1014 浏览 0 评论 0原文

我确信这是我做错的事情,但我似乎无法弄清楚。我正在使用backbone.js 与我的休息服务器(Philip Sturgeon 的codeigniter Restserver)对话。我正在我的骨干集合模型之一上运行正常的 model.destroy() 。

//a basic example
tagCollection.at(5).destroy();

这会创建对 url 的正确调用,例如:

DELETE http://mydomain.com/ index.php/tags/tag/id/12

当我进入“tag_delete”php函数并执行以下操作时:

$this->delete('id');

这总是不返回任何内容。我认为这与backbone.js 发送请求的方式有关,但我没有发现任何问题。详细信息如下。

Backbone 正在发出“DELETE”请求。

我的 REST_Controller 方法中的相关代码:

function tag_delete () {
    //delete the tag
    $id = $this->delete('id'); //always empty

    $result = $this->tag_model->delete($id);

    if (! $result) {
        $this->response(array('status' => 'failed'), 400);  
    }

    $this->response(array('status' => 'success'), 200);
}

有什么想法吗?任何backbone.js专家在使用codeigniter和Philip Sturgeon的restserver时遇到过这个问题吗?

I'm sure this is something I'm doing wrong, but I can't seem to figure it out. I'm using backbone.js to talk to my rest server (Philip Sturgeon's codeigniter restserver). I am running a normal model.destroy() on one of my backbone collections model.

//a basic example
tagCollection.at(5).destroy();

This creates a proper call to a url like:

DELETE http://mydomain.com/index.php/tags/tag/id/12

When I get inside my "tag_delete" php function, and do:

$this->delete('id');

This always returns nothing. I assume this has something to do with the way backbone.js sends it's requests, but nothing is jumping out at me. Details below.

Backbone is issuing a "DELETE" request.

Relevant code from my REST_Controller method:

function tag_delete () {
    //delete the tag
    $id = $this->delete('id'); //always empty

    $result = $this->tag_model->delete($id);

    if (! $result) {
        $this->response(array('status' => 'failed'), 400);  
    }

    $this->response(array('status' => 'success'), 200);
}

Any ideas? Any backbone.js experts run into this when using codeigniter and Philip Sturgeon's restserver?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小伙你站住 2024-12-20 13:21:54

这应该是修复删除请求的一种廉价快速方法...

function tag_delete () {

     $id = $this->uri->segment(4);

     $result = $this->tag_model->delete($id);

     if (! $result) {
          $this->response(array('status' => 'failed'), 400);  
     }

     $this->response(array('status' => 'success'), 200);
}

但是,这就是我使用主干和 REST_Controller 组合构建请求的方式...

DELETE http://example.com/index.php/tags/12

(去掉网址的 /tag/id/ 段...它是暗示您要从'tags' 按 id 收集,附加 /tag/id 是不必要的)

function tag_delete ($id) {

     $result = $this->tag_model->delete($id);

     if (! $result) {
          $this->response(array('status' => 'failed'), 400);  
     }

     $this->response(array('status' => 'success'), 200);
}

对于集合:

Backbone.Collection.extend({
    url : '/tags'
});

tagCollection.at(5).destroy();

然后将类似的内容添加到您的路由中:

$route['tags/(:num)'] = 'tags/tag/$1';

这将设置 Restserver 控制器所需的结构......这样更易于管理如果您正在做大量的 Backbone 工作。

This should be a cheap quick way to fix your delete request...

function tag_delete () {

     $id = $this->uri->segment(4);

     $result = $this->tag_model->delete($id);

     if (! $result) {
          $this->response(array('status' => 'failed'), 400);  
     }

     $this->response(array('status' => 'success'), 200);
}

However, this is how I am structuring my requests using a combo of backbone and REST_Controller...

DELETE http://example.com/index.php/tags/12

(get rid of the /tag/id/ segment of the url... it's implied that you are deleting a 'tag' row from the 'tags' collection by id, appending /tag/id is unnecessary)

function tag_delete ($id) {

     $result = $this->tag_model->delete($id);

     if (! $result) {
          $this->response(array('status' => 'failed'), 400);  
     }

     $this->response(array('status' => 'success'), 200);
}

for the collection:

Backbone.Collection.extend({
    url : '/tags'
});

tagCollection.at(5).destroy();

Then add something like this to your routes:

$route['tags/(:num)'] = 'tags/tag/$1';

which will set up the structure necessary for the restserver controller... it is just much more manageable that way if you are doing a lot of Backbone work.

西瓜 2024-12-20 13:21:54

根据 tgriesser 的建议,最好的方法是使用集合上的 url 属性。我之前使用过以下内容,它的工作原理就像魅力(以下控制器使用 silex 框架 + paris 库实现用于数据访问):

// DELETE   /{resource}/{id}    Destroy
$app->delete('/api/todos/{id}', function ($id) use ($app) {
    $todo =  $app['paris']->getModel('Todo')->find_one($id);
    $todo->delete(); 

    return new Response('Todo deleted', 200);
});

在您的主干集合中,添加以下内容:

window.TodoList = Backbone.Collection.extend({
    model: Todo,

    url: "api/todos",

    ...
});

最近,我写了一篇关于如何执行 GET/POST 的教程/PUT/DELETE 使用 Backbone.js 和 PHP http://cambridgesoftware.co .uk/blog/item/59-backbonejs-%20-php-with-silex-microframework-%20-mysql,可能会有所帮助。

As per tgriesser's suggestion, the best way to do this is to use the url property on the collection. I have used the following before and it works like charm (the following controller implemented using silex framework + paris library for data access):

// DELETE   /{resource}/{id}    Destroy
$app->delete('/api/todos/{id}', function ($id) use ($app) {
    $todo =  $app['paris']->getModel('Todo')->find_one($id);
    $todo->delete(); 

    return new Response('Todo deleted', 200);
});

In your backbone collection, add the following:

window.TodoList = Backbone.Collection.extend({
    model: Todo,

    url: "api/todos",

    ...
});

Recently, I have written a tutorial on how to do GET/POST/PUT/DELETE with Backbone.js and PHP http://cambridgesoftware.co.uk/blog/item/59-backbonejs-%20-php-with-silex-microframework-%20-mysql, might be helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文