1

I'm building a complex application in JavaScript which needs to make several requests to the server. Sometimes that request cannot be completed as sent, and addition information is needed. I would like the ability to have the server inform the application in the request's response that more information is needed and to describe how to retrieve that information.

For example, say a user tries to perform an operation that his current permissions level does not allow. The server needs to ask the application for an override authorization code, basically meaning the application needs to pop up with a dialog asking for an admin passcode.

I'd like to have some kind of framework that abstracts all this. Possible a main "Request" or "Operation" class, which I can define sub-Request classes that define possible interpretations of requests. I'm not sure if something like this exists already or not.

So I guess my questions are: 1) Does a framework like this exists? and 2) Are there any articles on this topic (platform and language agnostic, I can learn how they work from any source). I know frameworks like Dojo and ExtJS use something like it for their data stores, but I'm not sure if it's exactly what i'm needing, or how it even works for that matter.

Any help leading me in the right direction is appreciated, Thank You.

EDIT:

A point should be made I am looking for something that is abstract from the technology used to actually send that data to the server. That way I could utilize the same framework on different ajax technologies.

Basically I'm looking for a framework or article that can help me figure out how to create a custom "Application Protocol". An example of this would be:

{
    type: 512,
    success: true,
    data: { some: "data" }
}

I know I have to design the protocol itself, but what I need help with is creating a "class" or something that interprets this protocol automatically instead of just making redundant onSuccess callbacks

LordZardeck
  • 7,953
  • 19
  • 62
  • 119

7 Answers7

2

I have worked on an open source project named Pomegranate Framework which does what you want (to some extent). Perhaps you can extend it in order to meet your needs. It comes with an application layer close to want you asked for but you need to implement your protocol as it fits. Here's its address:

Pomegranate Framework

I haven't found the time to document it yet but it comes with a bunch of examples that may be useful to you. You may also want to take a look at its example page:

Pomegranate Framework Examples

I think you would like to see the 021 example titled "Handling server errors in client". I hope it's what you are looking for.

Mehran
  • 15,593
  • 27
  • 122
  • 221
  • This isn't quite what I was looking for, because you can't define a global standard for the data structure (api, protocol, whatever you call it.) Though at least you somewhat get the idea whereas everyone just completely misses it. – LordZardeck Aug 07 '12 at 23:54
  • I told you, you need to implement your protocol yourself. It just has some idea on how to do so. I have implemented an error layer in it myself. By which you can get and handle server errors on client side. Perhaps you want to add a new layer handling application logic. Let me know if I can be of any more help. – Mehran Aug 08 '12 at 06:00
  • i'm going to accept and award this answer simply because it was the only one that actually fit the question. – LordZardeck Aug 09 '12 at 15:53
1

Use Dojo and jQuery's Deferred object for callbacks. It is an implementation of the Promise design pattern. Every action has a success callback chain and a failure callback chain and both chains can diverge or merge at various points along the chains and chains can branch off to create sub-deferreds.

Kernel James
  • 3,752
  • 25
  • 32
1

If you know the state of your application on the client (and there are only a few error causes and you do not need detailed information from the server), you can and should use HTTP status codes. As far as I know 200 is the only one with a body, so you can't (or shouldn't, there's always adding headers, but I'd stay clear of that path) transmit anything else - but every framework should provide you with the means to pass an error handler on sending a request. In the callback function you pass as an error handler, you just have to do whatever the respective status calls for.

It's supported by the protocol, independent of whether you pass HTML, JSON or anything else and error callbacks based on the status code are supported by every library worth using.

Arne
  • 1,884
  • 1
  • 15
  • 19
0

Since you're basically talking about "server-sided push events", you need some technique that allows your server script to send data to your clients.

There are some well knowns methods like COMET, Flash Sockets and the latest guy in town WebSockets around.

Since WebSockets is probably the most sophisticated stuff from all of these, you should aim for that. Unfortunately, its browser support is limited to the "latest version" for most browsers, if you're good with that, just use them right away. If you want some fallbacks for older browsers, the most used framework for that should be socket.IO.

But even socketIO only abstracts all the different communication techniques away for you. Anyway, it should be fairly easy to build a solid management framework around that by yourself. So my answer on that part is, I'm not aware of any library or framework which deals with that kind of stuff.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • Actually, this has NOTHING to do with what I'm asking. I'm not asking about the technology that actually sends the requests. I'm using XHR requests, as real-time is not a requirement. However, a point should be made I am looking for something that is abstract from the technology used to actually send that data to the server. That way I could utilize the same framework on different ajax technologies. – LordZardeck Jul 30 '12 at 18:45
  • @LordZardeck: ok then ignore all of the above. I got confused by "the server should inform the application", which is not possible by pure XHR. – jAndy Jul 30 '12 at 18:47
  • In the actual response sent by the server. The "Response Data" if you will – LordZardeck Jul 30 '12 at 18:48
0

There are various ways to achieve this using ExtJs.

The most bare-bone one is to use Ext.Ajax.request() providing url, params, method. Then in your success handler, check for the server response and if it requires an additional data from the user - display an extra credentials dialog, and send another request with the extra credentials details that will unlock the server side script.

Notice that the success hander of the request method gives you back the config object of the request in its options parameter, so you can quite easily call the same request again, only adding the extra credentials this time around.

Here's a jsfiddle code demonstrating this concept (I've shown both using success and a global handler for all calls - I hope you'll be able to work out how to take it from here). And a similar one, which I believe is more what you're after exactly.

I'll be happy to help further, just ask the questions.

Izhaki
  • 23,372
  • 9
  • 69
  • 107
0

If abstraction and testability is what you want, I highly recommend AngularJS. angular $q which is an implementation of Kris Kowal's Q. You can create services that will hide away how you call the server and will allow you to change server implentations in future will little grief.

Dan Doyon
  • 6,710
  • 2
  • 31
  • 40
0

It will look like problem of server push i.e. wherever change occured data will be pushed to client There are following options

Commercial WebSync

You can use long polling mechanism like this

(

function poll(){
    $.ajax({ url: "server", success: function(data){
         myobject.setValue(data.value);

    }, dataType: "json", complete: poll, timeout: 30000 });
})();

You will get more info at ajax push server

Community
  • 1
  • 1
Kamahire
  • 2,149
  • 3
  • 21
  • 50