Ember.js REST更新(PUT)不起作用 [英] Ember.js REST update (PUT) not working
问题描述
我的RESTFUL GET,DELETE正在工作,但是PUT(编辑)奇怪的是无法更新数据库:
用于帖子编辑的句柄模板:
< script type =text / x-handlebarsid =post>
< h1>查看/更新帖子< / h1>
{{#if isEditing}}
< p>标题:{{input type =textvalue = title}}< / p>
< p>作者:{{input type =textvalue = author}}< / p>
< p>正文:{{textarea value = body}}< / p>
< button {{action'doneEditing'this}}>完成< / button>
{{else}}
< p>标题:{{title}}< / p>
< p>作者:{{author}}< / p>
< p>正文:{{body}}< / p>
< button {{action'edit'}}>编辑< / button>
{{/ if}}
PostController
App.PostController = Ember.ObjectController.extend({
isEditing:false,
actions:{
edit:function(){
this.set('isEditing',true);
},
doneEditing :function(post){
this.set('isEditing',false);
post.save(); //不工作 - 不更新数据记录!!!
}
}
});
REST适配器和数据模型
App.Post = DS.Model.extend({
title:DS.attr('string'),
author:DS.attr('string'),
body:DS.attr('string')
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace:'emberpostsrest / api'
});
Ember应用程序URL
http:// localhost / emberpostsrest
RESTFul服务器(使用SLIM PHP) / p>
http:// localhost / emberpostsrest / api
工作REST
http:// localhost / emberpostsrest / api / posts(GET所有)
http:// localhost / emberpostsrest / api / posts / 1(GET via id)
http:// localhost / emberpostsrest / api / posts / 1(DELETE via id)
用于编辑的RESTFul API已经使用PHP卷曲测试,并且工作正常:
// PUT - update
$ data = array(id=> 3,title=>3,author=> ;2,body=>1);
$ data_string = json_encode($ data);
$ ch = curl_init('http:// localhost:8080 / emberpostsrest / api / posts / 3');
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,PUT);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data_string);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_HTTPHEADER,array(
'Content-Type:application / json',
'Content-Length:'。strlen($ data_string))
);
苗条PHP代码
$ app-> put('/ posts /:id','updatePostByID'); // update post via id
function updatePostByID($ id)
{
$ request = \Slim\Slim :: getInstance() - > request();
$ body = $ request-> getBody();
$ post = json_decode($ body);
$ sql =UPDATE posts
SET title =:title,
author =:author,
body =:body
WHERE id =:id
try
{
$ db = getConnection();
$ stmt = $ db-> prepare($ sql);
$ stmt-> bindParam(id,$ post-> id);
$ stmt-> bindParam(title,$ post-> title);
$ stmt-> bindParam(author,$ post-> author);
$ stmt-> bindParam(body,$ post-> body);
$ stmt-> execute();
}
catch(PDOException $ e)
{
$ errorMessage = $ e-> getMessage();
}
}
感谢您的任何帮助:D
我怀疑如果您检查Ember中的传入数据,您会看到这些值在posts命名空间中键入。 p>
换句话说,而不是这样:
{
id:1,
title:my title,
author:me,
body:body
}
正如下所述:
{
posts:{
id:1,
title:my title,
author :我,
身体:身体
}
}
这是ember数据的约定,所以你可能想要更新你的PHP代码,而不是试图在Ember方面解决它。
My RESTFul GET, DELETE is working however the PUT (edit) strangely enough is fail to update the database:
Handlebar template for Post editing:
<script type="text/x-handlebars" id="post">
<h1>View/Update Posts</h1>
{{#if isEditing}}
<p>Title: {{input type="text" value=title}}</p>
<p>Author: {{input type="text" value=author}}</p>
<p>Body: {{textarea value=body}}</p>
<button {{action 'doneEditing' this}}>Done</button>
{{else}}
<p>Title : {{title}}</p>
<p>Author: {{author}}</p>
<p>Body : {{body}}</p>
<button {{action 'edit'}}>Edit</button>
{{/if}}
The PostController
App.PostController = Ember.ObjectController.extend({
isEditing: false,
actions: {
edit: function() {
this.set('isEditing', true);
},
doneEditing: function(post) {
this.set('isEditing', false);
post.save(); //NOT WORKING - NOT UPDATING THE DATABASE RECORD!!!
}
}
});
REST adapter and data model
App.Post = DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
body: DS.attr('string')
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'emberpostsrest/api'
});
Ember App URL
http://localhost/emberpostsrest
RESTFul server (using SLIM PHP)
http://localhost/emberpostsrest/api
Working REST
http://localhost/emberpostsrest/api/posts (GET all)
http://localhost/emberpostsrest/api/posts/1 (GET via id)
http://localhost/emberpostsrest/api/posts/1 (DELETE via id)
RESTFul API for edit already tested using PHP curl and is working fine:
//PUT - update
$data = array("id" => 3, "title" => "3", "author" => "2", "body" => "1");
$data_string = json_encode($data);
$ch = curl_init('http://localhost:8080/emberpostsrest/api/posts/3');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
Slim PHP code
$app->put('/posts/:id', 'updatePostByID'); //update post via id
function updatePostByID($id)
{
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
$post = json_decode($body);
$sql = "UPDATE posts
SET title = :title,
author = :author,
body = :body
WHERE id = :id";
try
{
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $post->id);
$stmt->bindParam("title", $post->title);
$stmt->bindParam("author", $post->author);
$stmt->bindParam("body", $post->body);
$stmt->execute();
}
catch(PDOException $e)
{
$errorMessage = $e->getMessage();
}
}
thank you for any help :D
I suspect if you inspect the incoming data from Ember, you'll see that the values are keyed within the "posts" namespace.
In other words, instead of this:
{
"id": "1",
"title": "my title",
"author": "me",
"body": "the body"
}
It's coming in as this:
{
"posts": {
"id": "1",
"title": "my title",
"author": "me",
"body": "the body"
}
}
This is the convention for ember-data so you'll probably want to update your PHP code, rather than try to work around it on the Ember side.
这篇关于Ember.js REST更新(PUT)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!