我已经设置了一个 ember.js 客户端,将 ember-data 作为持久性处理程序。 使用此代码可以很好地从数据库中获取所有记录:
router.js
App.ProjectsRoute = Ember.Route.extend({
model: function() {
return this.store.find('project');
}
});
但是使用此代码添加新记录无法正常工作。它仅添加到模型中,不会保留。
index.html(示例)
{{input type="text" id="newTitle" value=Title}}
<button class="small" {{action 'createProject'}}><i class="icon-plus-sign"></i></button>
Controller.js
App.ProjectsController = Ember.ArrayController.extend({
actions: {
createProject: function () {
var project = this.store.createRecord('project', {
project_number: this.get('newProject_number'),
title: this.get('newTitle'),
client: this.get('newClient'),
comment: this.get('newComment'),
xmlfile: this.get('newXmlfile')
});
this.set('newProject_number', '');
this.set('newTitle', '');
this.set('newClient', '');
this.set('newComment', '');
project.save();
}
}
});
models.js
App.Project = DS.Model.extend({
title: DS.attr('string'),
client: DS.attr('string'),
comment: DS.attr('string'),
project_number: DS.attr('string'),
});
app.js
window.App = Ember.Application.create();
App.store = DS.Store.extend({
adapter: DS.RESTAdapter,
});
DS.RESTAdapter.reopen({
namespace: 'api/index.php',
headers: {
"API_KEY": "secret key",
"ANOTHER_HEADER": "Some header value"
}
});
框架版本
Ember : 1.2.0;
Ember Data : 1.0.0-beta.5+canary.e120006;
Handlebars : 1.1.2;
jQuery : 2.0.3;
我错过了什么配置?但是控制台中没有错误。 REST-Api 与 curl 配合良好。
最佳答案
问题是 JSON 的结构“POST”到 API:
已发送:
{project: {client: "test",comment: "test",project_number: "test",title: "test"}}
API 后端预期:
{client: "test",comment: "test",project_number: "test",title: "test"}
我想知道在哪里可以找到 ember-data 如何构建它的请求的信息。 (为了进一步发展)
关于javascript - Ember 数据:如何正确发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20885917/