保存时的主干模型只返回表单数据

Backbone model on save only returns form data

本文关键字:返回 表单 数据 模型 保存      更新时间:2023-09-26

在我正在构建的web应用程序中,我通过API保存模型-在成功时,我将模型返回到控制台,但是模型只包含从表单提交的数据,但是API保存了从提交的表单创建的各种其他数据,我如何才能在成功回调中返回完整的模型?

这是我的代码,

模型保存,

saveBasicProject: function(e) {
    e.preventDefault();
    var that = this;
    console.log("saving basic project");
    var projectData = $('.create-new-project').serializeJSON();
    //var projectModel = new app.Project(projectData);
    this.model.save(projectData, {
        success: function(model, response) {
            console.log(model);
            that.response_json = JSON.parse(response);
            that.collection.add(that.model);
        },
        error: function(model, response) {
            var error_json = response.responseJSON;
            $(".create-new-project").before( response.responseJSON.msg );
        }
    });
}, 

API保存

public function save()
{
    $rules = array(
        'name' => 'required',
        'description' => 'required',
        'cost' => 'numeric',
        'start_date' => 'required | date',
        'end_date' => 'required | date'
    );  
    $validation = Validator::make(Input::all(), $rules);
    if($validation->fails()) {
        return Response::json( $validation->messages()->first(), 500);
    } else {
        $project = new Project;
        $project->name = Input::get('name');
        $project->description = Input::get('description');
        $project->total_cost = Input::get('cost');
        $project->start_date = Input::get('start_date');
        $project->finish_date = Input::get('end_date');
        $project->run_number_days = $this->get_days_between_two_dates(Input::get('start_date'), Input::get('end_date'));
        $project->num_days_from_year_start = $this->get_days_between_two_dates("2014-01-01", Input::get('start_date'));
        $project->color = $this->project_rbg_to_project_hex();
        $project->user_id = ResourceServer::getOwnerId();
        if( $project->save() ) {
            return Response::json($project, 200);
        } else {
            return Response::json(array( 'error' => 'Something has gone wrong!' ), 500);
        }
    }
}

返回什么?

    child {cid: "c60", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
_changing: false
_events: Object
_pending: false
_previousAttributes: Object attributes: Object cost: "18000" description: "Description" end_date: "2014/01/30" name: "Simon 18" start_date: "2014/01/01"
__proto__: Object changed: Object cid: "c60" collection: child
__proto__: Surrogate

POST json

{cost: "23000"
description: "Description"
end_date: "2014/01/30"
name: "Project #23"
start_date: "2014/01/01"}

服务器的响应

{
    name: "Project#23",
    description: "Description",
    total_cost: 23000,
    start_date: "2014/01/01",
    finish_date: "2014/01/30",
    run_number_days: 30,
    num_days_from_year_start: 1,
    color: "#757da3",
    user_id: 1,
    updated_at: "2014-08-0510: 14: 15",
    created_at: "2014-08-0510: 14: 15",
    id: 105
}

上述内容也在DB中得到更新,但返回的模型仅包括原始请求json。

您需要在成功回调中显式设置要建模的响应数据。

that.model.set(that.response_json);