Cycs
Cycs

Reputation: 239

Backbone model on save only returns form data

In a web application I am building, I save a model via an API - on success I return the model to the console, however the model only contains the data submitted from the form, but yet the API saves various other pieves of data created from the submitted form, how can I get the full model to be return in success callback?

here is my code,

MOdel save,

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 save

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);

        }

    }

}

What gets returned?

    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

The POST json

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

The response from the server

{
    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
}

The above is also getting updated in the DB but the model that gets returned just includes the original request json.

Upvotes: 0

Views: 329

Answers (1)

jsist
jsist

Reputation: 5253

You need to explicitly set the response data to model in success callback.

e.g.

that.model.set(that.response_json);

Upvotes: 0

Related Questions