php - Backbone model save state -


i use backbone model save data server. handle success or error callback after save(post) , set wait:true. question why backbone model trigger error callback when server returns string? when server returns json or number, go success. problem if want return multiple error message , want put error message collection(json), never go error callback. sample code like

echo 'success'; //actually go error callback cuz return string(any string) echo json_encode($some_array); // go success echo 200/anynumber; // go sucess 

my solution if wanna return multiple message, can separate message delimiter, , use javascript native function split separate them. there better solution maybe can return state(indicate either success or error) , collection server?

model code looks like: somemodel.save({somedata},     {         success:function(a,b,c){},          error:function(b,c,d){},         wait: true     }); 

backbone expects json default response format. when integer, suspect, backbone using jquery $.parsejson() on number , returns valid json while not. if want return multiple errors messages, suggest put them separate fields of array , encode them send response backbone.

edit

just checked backbone source code , doesn't call on $.parsejosn() contrary guessed above.

edit 2

assume have following php code (as create framework agnostic example, possible things quicker , smoother using framework have picked slim).

when save model, backbone sends data server using post method. in slim translate following:

$app = new \slim\slim(); $app->post('/authors/', function() use($app) {     // request data backbone in $request     $request = $app->request()->getbody();     // decode them php object     $requestdata = json_decode($request);     // put response object in $response : allow set response data header values, etc     $response = $app->response();      // awesome stuff here , array containing data in $data //      // sample $data in case of success     $data = array(         'code' => 200,         'status' => 'ok',         'data' => array('id'=>1, 'name' => 'john doe')     );      // sample $data in case of error     $data = array(         'code' => 500,         'status' => 'internal server error',         'message' => 'we unable reach data server, please try again later'     );      // set content type     $app->contenttype('application/json');     // don't forget add http code, backbone.js call "success" if has 2xx http code     $app->response()->status( $data['code']);     // , send data     $response->write($data); 

i have used slim because gets job done , should read english.

as can see, backbone need response in json format. tested on 1 of websites, if send http code different 2xx, backbone call error instead of success. browser catch http code, watch out!

removed info

backbone parse function expecting json well! assume have collection:

var authorscollection = backbone.collection.extend({     initialize: function( models, options ) {         this.batch = options.batch     },      url: function() {         return '/authors/' + this.batch;     },      // parse here allows play response long "response" json object. otherwise, backbone automatically call "error" function on whatever, view, using collection.     parse: function( response ) {         return response.data;     } }); 

parse allows inspect response, not accept else json!


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -