나는 똑같은 문제에 직면했다. DELETE, PUT, PATCH 메소드는 아직 브라우저/HTML/서버에서 완전히 지원되지 않는다. Are the PUT, DELETE, HEAD, etc methods available in most web browsers?
이 간단한 해결책은 다음과 백본 라인 1191의 methodMap
을 변경하는 것입니다 : 당신은이 스택 오버 플로우 문제를보고 할 수 있습니다
// Map from CRUD to HTTP for our default `Backbone.sync` implementation.
var methodMap = {
'create': 'POST',
'update': 'POST', //'PUT',
'patch': 'POST', //'PATCH',
'delete': 'POST', //'DELETE',
'read': 'GET'
};
다음의 속성과 행동 유형을 포함 모델 이제, 모델을 저장할 때
var Person = Backbone.Model.extend({
defaults:{
action_type : null,
/*
* rest of the attributes goes here
*/
},
url : 'index.php/person'
});
는
var person = new Person({ action_type: 'create' });
person.set(attribute , value); // do this for all attributes
person.save();
를 다음을 수행 다음과 같은 방법을 가지고 당신이 컨트롤러가 REST_Controller을 확장 사람이라는 클래스
person.php
라고 했어야
application/controllers
폴더에
:
class Person extends REST_Controller {
function index_get() { /* this method will be invoked by read action */ }
/* the reason those methods are prefixed with underscore is to make them
* private, not invokable by code ignitor router. Also, because delete is
* might be a reserved word
*/
function _create() { /* insert new record */ }
function _update() { /* update existing record */ }
function _delete() { /* delete this record */ }
function _patch() { /* patch this record */ }
function index_post() {
$action_type = $this->post('action_type');
switch($action_type){
case 'create' : $this->_create(); break;
case 'update' : $this->_update(); break;
case 'delete' : $this->_delete(); break;
case 'patch' : $this->_patch(); break;
default:
$this->response(array('Action '. $action_type .' not Found' , 404));
break;
}
}
}
,이 솔루션은 못생긴 하나라고 말했다 가졌어요. 당신은 백본 구현에서 스크롤하면 라인 1160에 다음 코드를 찾을 수 있습니다 :
// For older servers, emulate HTTP by mimicking the HTTP method with `_method`
// And an `X-HTTP-Method-Override` header.
if (options.emulateHTTP && (type === 'PUT' || type === 'DELETE' || type === 'PATCH')) {
params.type = 'POST';
당신이 백본 구성의 에뮬레이션 옵션을 설정해야합니다 의미합니다.그 효과를 테스트하기 위해 귀하의 main.js
Backbone.emulateHTTP = true;
Backbone.emulateJSON = true;
에 다음 줄을 추가, 나는 간단한 모델을 생성하고 여기에 결과가 applications/controllers
폴더라는 API 컨트롤러를 필요
이 있습니다
api.php
라는 이름의 파일에
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH.'/libraries/REST_Controller.php';
class Api extends REST_Controller
{
function index_get()
{
$this->response(array("GET is invoked"));
}
function index_put()
{
$this->response(array("PUT is invoked"));
}
function index_post()
{
$this->response(array("POST is invoked"));
}
function index_patch()
{
$this->response(array("PATCH is invoked"));
}
function index_delete()
{
$this->response(array("DELETE is invoked"));
}
}
하고 js/models
폴더에
,
api_model.js
라는 모델을 생성
나는 패치하는 법을 모르지만 이것이 도움이되기를 바랍니다.
편집
나는 코드 점화기의 REST 구현에 포함되지 않은 패치를 수행하는 방법을 발견했다. REST_Controller 라인 (39)에서는
protected $allowed_http_methods = array('get', 'delete', 'post', 'put');
당신이 그 일을 한 후,
/**
* The arguments for the PATCH request method
*
* @var array
*/
protected $_patch_args = array();
또한 당신을이 코드를 추가, 또한,이 방법을 적용, 마지막에 'patch'
를 추가 할 필요가 다음을 찾을 수 있습니다 패치 인수를 구문 분석하려면 다음 코드를 추가해야합니다 : 당신이를 호출 할 때 백본 문서에 따르면, 지금
/**
* Parse PATCH
*/
protected function _parse_patch()
{
// It might be a HTTP body
if ($this->request->format)
{
$this->request->body = file_get_contents('php://input');
}
// If no file type is provided, this is probably just arguments
else
{
parse_str(file_get_contents('php://input'), $this->_patch_args);
}
}
을, 당신은, 패치 방법을 보낼 {patch: true}
을 통과해야 다음 줄을 따라 패치를 실행하십시오 :
api.save({age:20},{patch: true, success: function(r,s) { console.log(s); } });
// PATCH is invoked
누구든지 나를 도울 수 있습니까 ?? – eded