保存的骨干模型仅返回表单数据

时间:2014-08-05 07:38:54

标签: javascript backbone.js laravel

在我正在构建的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。

1 个答案:

答案 0 :(得分:0)

您需要在成功回调中明确将响应数据设置为模型。

e.g。

that.model.set(that.response_json);