How to override Dojo's xhrGet and xhrPost? - ajax

We are extensively using Dojo's xhrGet and xhrPost in our application. This has been used across multiple JavaScript files. Now we need a uniform way in which we handle the exceptions that are returned from the server in case of an AJAX call. We don't want to handle this in all places where we are using Dojo's xhrGet or xhrPost. Is it possible to do that without disturbing any of the existing code? For example, when some exception is sent from the server as part of the ajax response, I need to display some message in a consistent way across the application.
Could you please suggest me a solution for this? Kindly let me know if any more information is required.

Use IO Pipeline Topics as I described in Generic Loading Icon in Dojo and you won't have to change your code at all.

did you look at the dojo/aspect or dojo/on ? You can define functions that get executed after a function was called (or before) with aspect.
Take a look at that:
http://dojotoolkit.org/reference-guide/1.8/dojo/aspect.html#dojo-aspect-after
Why dont you create a custom xhrArgs class using dojo/declare that has the same error function for all his children ?
http://dojotoolkit.org/reference-guide/1.8/dojo/_base/declare.html#dojo-base-declare
Lucian

Related

In which scope my ajax data send from view to handler is stored in coldbox

Please let me know answer if any one knows about it.In which scope my ajax data send from view to handler in coldbox
When you're making an ajax POST, it gets treated as a brand new request. This means you'll need a separate route and handler for that request.
Within your new handler (let's call it /handlers/data.cfc) you'll want to format your response appropriately for your code. ColdBox comes with some nifty tools to help you do this. One way would be to use renderData() within your handler or view.
Rough example:
event.renderData( type="json", data=yourData );
Once set up correctly, the ajax calling code should receive the formatted data from your new handler as expected.
Side note: I recommend including code samples when asking questions on StackOverflow. It will help those that want to provide assistance understand exactly what you are trying to do.

Zend Framework 1 Ajax

Could somebody please suggest me a good beginner tutorial of using Ajax with Zend 1.I have been searching the net for some of this tutorials, but couldn't find an understandable one.In some they say you have to create a .json.phtml file for json response, the others don't.I am very confused about all these ajax calls with Zend Framework 1. Would be very grateful.
Well there really are some basic things.
Get your data (from DB, file, in-code array, whatever)
Get the controller helper
Send the JSON response
And that's it. OK, not exactly but basically yes!
Provided you have the data in $data:
$this->_helper->json($data, true);
will return a JSON response. The documentation is here.
Now there is the other notion of a Context Switch and AjaxContentHelper which:
The ContextSwitch action helper is intended for facilitating returning different response formats on request. The AjaxContext helper is a specialized version of ContextSwitch that facilitates returning responses to XmlHttpRequests.
To enable either one, you must provide hinting in your controller as to what actions can respond to which contexts. If an incoming request indicates a valid context for the given action, the helper will then:
Disable layouts, if enabled.
Set an alternate view suffix, effectively requiring a separate view script for the context.
Send appropriate response headers for the context desired.
Optionally, call specified callbacks to setup the context and/or perform post-processing.
Something like this:
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch->setDefaultContext('json');
$contextSwitch->addActionContext('index', array('xml','json'))->initContext();
$contextSwitch->addActionContext('get', array('xml','json'))->initContext();
$contextSwitch->addActionContext('post', array('xml','json'))->initContext();
$contextSwitch->addActionContext('put', array('xml','json'))->initContext();
$contextSwitch->addActionContext('delete', array('xml','json'))->initContext();
$contextSwitch->addActionContext('head', array('xml','json'))->initContext();
You don't really need a tutorial I think. All you need is a good basic knowledge of how the web works internally and to read the Zend Documentation. Anyway here is some tutorial on ContextSwitch.

What is the canonical way to handle errors during Ajax-requests?

For normal requests we can simple register an <error-page> in web.xml. However, this does not apply to Ajax-requests. By default errors during an Ajax-request will result in a little pop-window in the browser that shows the exception.
The main example I am struggling with is handling a ViewExpiredException in a uniform way. For standard requests, I redirect to a page that explains that the user is not logged in and provides a link to the login-page. I would like to do the same for Ajax-requests. There seem to be several ways:
I could write a javascript function that handles the error on the client-side and redirects to the error-page. I would then have to add this function every <f:ajax>-tag on all pages using the onerror-attribute. Is there a way to tell JSF that I want to have this javascript-function as the default error-handler for all <f:ajax>-tags?
I could use a custom exception-handler, as described in this blog. This seems to do what I want, but I wonder if it is overkill. Is there no simpler solution?
So my question is, how is this supposed to be solved? Which of the approaches I listed should be used? Is there another approach that I do not know of?
You can use jsf.ajax.addOnError() to set the default error handler. E.g.
jsf.ajax.addOnError(function(data) {
alert(data.responseText);
});
See also chapter 13.3.6.2 of the JSF2 spec. You can find all properties of data object in table 14-4 of the JSF2 spec.

querying info from an existing dojo.itemfilereadstore

I'll start off by mentioning I'm a dojo noob. That said,
I'm having trouble reading data from a itemfilereadstore. When my page is loaded, I perform a .fetch() on a itemfilereadstore. The ajax call retrieves some data from the server and my onComplete handler is executed. My problem is after this store is populated (and after my onComplete handler is executed), I don't see a way to read its contents (enumerate its items) again without hitting the server.
How can I fetch() against this existing datastore without it trying to hit my webserver again? Surely I can look up information that already exists in this object somehow? The dojo documentation doesn't seem to have any obvious answers, what am I missing?
I believe what you're looking for is itemfilereadstore.query. If all you need is to get all the elements, just a plain old query should do the trick. If you need to filter, then you'll have to read up on the query syntax : http://dojotoolkit.org/reference-guide/dojo/data/ItemFileReadStore.html#query-syntax

Wicket and a rich ajax website: easiest way to do it?

I want to use Wicket to build an application, but I have some designers that would like to write/maintain the javascript, and they basically expect 1 JS-segment per page, and a global JS-file.
I think the most natural way to add javascript in wicket is to add it per component (not per page), which would create problems for those designers (fractioned javascript, and having to write it in java-files). Is there a better way to solve this?
(of course, I expect things to work after a partial refresh.)
And a second (related) thing they'd like (and I'd like actually) is the possibility to request information in JSON-format through a static link , is this possible in Wicket?
I started with JSON by making my wicket pages return the JSON, but quickly realized there are better tools for the job, especially if you will have a full web services layer. If you just need a little JSON here and there, always via a GET, then sure, just make a Wicket page.
I ended up using Jersey with Jackson alongside of Wicket. Jersey simplifies the configuration of URLs that can do different things with different http methods (GET/POST/PUT/DELETE), as well as easily parsing query strings, etc. I'd consider going this route for heavier JSON needs.
You can easily run both Wicket and Jersey in the same web application with a little web.xml configuration.
Wicket's built in AJAX support is always stateful and thus accessed with changing URLs. If your designers aren't planning to use Wicket's JS library, it's pretty straightforward to mount a JSON page:
public class JsonReturningPage extends WebPage {
public JsonReturningPage(PageParameters params) {
String json = "{foo: 4711}";
IRequestTarget t = new StringRequestTarget("application/json", "UTF-8", json);
getRequestCycle().setRequestTarget(t);
}
}
Alternatively, you could also implement your own AbstractRequestTargetUrlCodingStrategy to directly return an IRequestTarget from IRequestTarget decode(RequestParameters params) and mount it in your application.
Regarding JS files, I'd try to educate them to use one file per component. This certainly has the advantage of less copy-paste code and simpler maitenance. Additionally, I'd certainly discourage JS in Java code. It's normally only needed to pass data or config to JS , either as variable definitions or method calls. As this data is typically in Java and JS is written by designers, it's time for designers and programmers to team up.
Quick answer to your second question is yes it is possible. Use bookmarkable links to access a resource that returns JSON data.
You can easily use the following code to dynamically communicate with Wicket:
AbstractDefaultAjaxBehavior callme = new AbstractDefaultAjaxBehavior(){
#Override
protected void respond(AjaxRequestTarget target) {
}
};
page.add(callme);
//From any ajaxrequesttarget you can simply append the following code:
target.appendJavascript("wicketAjaxGet('"+callme.getCallbackUrl()+");");
This way you can have an ajaxlink etc... that will transfer the ajaxrequest to the Wicket side. If you want to pass data (though a static link doesn't sound like that) do the following:
"wicketAjaxGet('"+callme.getCallbackUrl()+"&x='+value_to_pass_back''";
//to Read the value in the respond:
String x = RequestCycle.get().getRequest().getParameter("x");
So the url to the callback is dynamically generated (as the callback url is specific to the session) but it is formed like any other url....
To me this is 10 times simpler than building a JSON system on top of wicket instead of using the one built into it.... I use this all the time and it works great for me at least. If your solution is different/better I would like to know why perhaps.

Resources