Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 我在服务器的新记录id中找不到更新余烬模型的新记录_Ember.js_Ember Data - Fatal编程技术网

Ember.js 我在服务器的新记录id中找不到更新余烬模型的新记录

Ember.js 我在服务器的新记录id中找不到更新余烬模型的新记录,ember.js,ember-data,Ember.js,Ember Data,我有这个动作。数据来自createRecord之后的表单,并完美地保存在数据库中 App.ShowController = Ember.Controller.extend({ actions: { createShow: function() { var self = this; var onSuccess = function(res) { alert('CREATED OK ' + res.get('alias'));

我有这个动作。数据来自createRecord之后的表单,并完美地保存在数据库中

App.ShowController = Ember.Controller.extend({
actions: {
    createShow: function() {
        var self = this;

        var onSuccess = function(res) {
            alert('CREATED OK ' + res.get('alias'));
        };

        var onFail = function() {
            alert('err ' + res);
        };

       self.get('model').save().then(onSuccess, onFail);
    }
}
})

id是在数据库Postgres中生成的,我从Mojolicious{serverResponses:{last_id:500}}中生成的应用程序返回一个格式完美的json响应,并返回一个“200”状态

我可以在控制台中看到带有json数据的网络响应

但是如何在成功时访问回调函数中的最后一个\u id值

在res中,我有发送到服务器的原始数据,但显然它的id属性未定义

我的想法是在模型的id中设置从数据库返回的id

我已经看到了很多关于son返回格式、序列化问题等的问题,但我真正想知道的是,返回的数据在哪里,在哪个变量或对象中

显然,如果失败,我也会遇到同样的问题。我返回了一个格式完美的json,其根用于ember,但在回调函数onFail中找不到它

有人能给我指出正确的方向吗


关于

您需要正确配置您的模型,例如,如果您的控制器中有一个x模型,请创建显示操作

//create your model locally
//acquisition is for the name of your model
//product_id is just whatever attributes you declare in your model spec
var newAcquisition = this.store.createRecord('acquisition', {
  'product_id': this.get('id'),
});

//with save it calls to the server and creates the new model and retrieves a response
newAcquisition.save().then(function(response){
    console.log('success - json '+response);   
}, function(response){
    console.log('fail - err '+response); 
});
在此之后,您不需要捕获对put和id的响应,如果您的json响应正确,Ember将处理该响应并用该新id更新您新创建的对象

您可以在setupController功能中将数据保存到控制器的路由中

App.ShowRoute = Ember.Route.extend({
    setupController: function(controller, model){
        //setup form data
        controller.set('formData', data);
    },
    actions:{
    }
});

在服务器上创建记录时,可以返回记录的json,包括ID,与GET请求返回json的方式相同。然后,余烬数据将自动使用该响应更新其存储中的模型。然后,传递给onSuccess的参数将包含具有生成ID的更新模型


如果无法更改REST api,则必须研究扩展以从有效负载中提取id。

REST服务仅返回最后一个id,而不是整个记录。如果我在控制器中执行createRecord,则记录中没有表单数据。这就是我在routes中创建它的原因。如果我在本地创建记录,我不知道如何添加表单数据…您是否尝试在路由器内使用setupController进行设置?通过这种方式,您可以在ControllerAllo中访问它。因此,您返回的json必须是ember可以读取的格式,因此它可以自动处理响应并为您执行所有操作。如果不是这样,您可能需要为您的模型创建自定义序列化程序