Trunk - how does our model know if the server is sending an error message using PHP

I have a model that has a url property. When I save the model using the Save method, it sends a request to the server. If the message is successful, the success () method is run, and if there is no communication failure, the "error" function is triggered. But what if there is no communication problem, but when the server sends an "error message" due to some server checks or other reasons? Since due to server side (or others) validation, the model is not saved to the server. Thus, it also does not need to be saved in the base model. What should I do to tell the model not to save when the server sends "Database update failed" and tell it to save when I receive "Successfully updated", I am sending below response from the server:

    $this->response(
        array(
             'model'=>$model,
             'status'=>$status,
             'metamodel'=>array(),
             'message'=>$msg  (either success or fail)(my custom message)
            )
          ); 

      

on the client side, I keep this.

        model.save(attr{
          success:(model,res)->
            // to do when communication success
          error:(model,res)->
            //to do when communication fails
         })

      

So how can I check based on message or status if the server was successful or not? And how can I get the model not to save or discard changes when the server says no (NOT AN INTERNAL SERVER ERROR, my custom bcoz validation error, or other reasons).

Also how can I use the Backbone.Sync method (a practical example linked to my code please) and what does it do? (plz mentions clearly if there is a communication issue or custom error or internal server error)

I can access the "status" and "message" in the method "Success", but how can I use them to return changes to the model. Thanks to

+3


source to share


1 answer


Overwrite the method parse(response)

on the model and check the error flag. http://documentcloud.github.com/backbone/#Model-parse



+2


source







All Articles