Mujahid
Mujahid

Reputation: 1237

Access returned values in backbone js

In my backbone code, I'm getting the returned value to a variable and now I have to access one particular value with that variable.

Following is the returned value from the server

Object {
    readyState = 4, 
    responseText = '{"analysis":{"created_a...s"],"status":"active"}}', 
    status = 201, 
    more...
}

This is how I get the above result in console

attributes = {
    locale: $action.find('#locale').val(),
    title: $action.find('#title').val(),
    category: $action.find('#category').val(),
    status: $action.find('#analysis_status').val(),
    matrix: $action.find('#analysis_matrix').val(),
    predefined_barriers: barrierTexts,
    features: featureTexts
}

b = this.model.save(attributes);

What I want is to access the responseText

How can I access it with the variable b or is there any other good way to do it

Thanks

Upvotes: 1

Views: 93

Answers (2)

Bergi
Bergi

Reputation: 665000

The save method returns the jqXHR object that is returned by the internally used jQuery.ajax, so you can just add a callback to it as usual:

this.model.save(attributes).done(function(data) {
    // you could use
    this.responseText
    // but you probably will use the already parsed JSON
    data
});

Upvotes: 1

McGarnagle
McGarnagle

Reputation: 102783

It's an asynchronous call, so it will have to be a callback. You can pass a success callback to save --

b = this.model.save(attributes, { success: function(model, response) {

    console.log(b.responseText);
} });

Upvotes: 1

Related Questions