Tuesday, January 21, 2014

Forgive the ignorance - What is a JSON view?

Reading on response and such but what is a JSON view?

 

I am building an site which is 99% based on JSON responses and that gets pushed back to the js based on the result / success / fail and the data sent from the controller to the js updates the current view accordingly.

Not using any new views or anything after submit  /view / add just the standard html views / elements so I am wondering what is a JSON view?

 

For example I try to submit a form sent via ajax:

 

In error the response from the server sent to the js from the controller is like this::

 

{

"status":false,

                    "errors":{

                                        "title":["Please enter a title."],

                                        "from":["Please enter the from who."],

                                        "description":["Please enter a description."]},

                    "clear":false,

                    "token":"$2a$10$.3FRu2riTDzyXS3vXj5.LOVW3insbLfCyjpSfCadLEIOnuwu6uh02",

                    "message":{

                                         "title":"Not quite there!",

                                        "text":"Please correct the following errors and try again."}

}

 

 

JS SNIP:

.done(function(r){

                if (r.status === true) {

                                alertMessage('success', r.message);

                                errorUpdate(handler);

                } else {

                                if(r.illegal){

                                                securityError(r);

                                }

                                alertMessage('error', r.message, r.errors);

                                setErrors(r.errors, sel)

                }             

})

 

So the js will update the form automatically (by-passing the cake error notifications , added dynamically) adding the errors where they are, reset the CSRF token and the general fail message. No view is used / rendered so just wondering if I am doing this wrong?

 

And what would a JSON view look like anyways?

 

As always thanks in advance.

 

Dave Maharaj

Freelance Designer | Developer

www.movepixels.com  |  dave@movepixels.com  |  709.800.0852

 

No comments: