So I have this one CakePHP web application that I’m trying to refactor.
There is a large portion of code that usually works like this:
E.g. “Flag”:
User clicks flag button
Redirect the user to the flag action
Flag action flags post
Redirect user to post that was just flagged.
I am also trying to enhance it with JavaScript like so:
User clicks flag button
Javascript makes AJAX request to that action
Call returns with a status that JS inteprets
Is there some way to streamline this sort of flow so that it is easily testable and can be covered with a javascript that doesn’t need redirection?
Lookup jQuery’s
.post()function. That lets you post to a URL and handle (in a callback function) whatever results the server returns. The server can return some simple data to indicate success or failure, or it can return an HTML fragment to be placed somewhere on the page (under control of your Javascript code), or really anything else you want.