Tuesday, January 21, 2014

RE: Forgive the ignorance - What is a JSON view?

After looking at your code,

 

$.ajax({

    type: 'get',

    url: '<?php echo $this->Html->url(array('action' => 'favorites', 'ext' => 'json')); ?>',

    beforeSend: function(xhr) {

        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

    },

    success: function(response) {

        if (response.error) {

            alert(response.error);

            console.log(response.error);

        }

        if (response.content) {

            $('#target').html(response.content);

        }

    },

    error: function(e) {

        alert("An error occurred: " + e.responseText.message);

        console.log(e);

    }

});

 

 

Whats the difference than just the js as normal?

 

You have a selector / element to trigger something the js will know so why the view to do the work? Seems like an added step thats not needed.

If the snip above was in your site.js on click do this why the need for the view. If a common js fuction (add2Fav) for post / blog /user why call a view when js will automatically say ok this guy like this post, this blog this user, save it………..

 

Whats the point of the view other than a js function? Hide the js from the end user?

 

Just to clarify.

 

Thanks

 

Dave Maharaj

Freelance Designer | Developer

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

 

From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf Of euromark
Sent: Tuesday, January 21, 2014 9:13 PM
To: cake-php@googlegroups.com
Subject: Re: Forgive the ignorance - What is a JSON view?

 

http://book.cakephp.org/2.0/en/views/json-and-xml-views.html



Am Dienstag, 21. Januar 2014 17:15:40 UTC+1 schrieb advantage+:

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
Description: header_logo

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

 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: