PDA

View Full Version : DwrProxy



Axel
29 Nov 2007, 10:36 AM
Hello all,

Here is a DwrProxy that works better than anything else currently out there on the forums.

Differences to the other ones:
-NO use of global objects
-properly passes a variable number of arguments
-proper error handling
-fires events



Ext.data.DWRProxy = function (f) {
Ext.data.DWRProxy.superclass.constructor.call(this);
this.func = f;
};

Ext.extend(Ext.data.DWRProxy, Ext.data.DataProxy, {
load : function(params, reader, loadCallback, scope, arg) {
var dataProxy = this;
dataProxy.fireEvent("beforeload", dataProxy, params);
var args = [];
for (var param in params) {
args[args.length] = params[param];
}
args[args.length] = {
callback: function(response) {
dataProxy.fireEvent("load", dataProxy, response, loadCallback);
var records = reader.read(response);
loadCallback.call(scope, records, arg, true);
},
exceptionHandler: function(message) {
dataProxy.fireEvent("loadexception", dataProxy, response, loadCallback, e);
loadCallback.call(scope, null, arg, false);
}
};

this.func.apply(this, args);
}
});


Cheers,
Axel

adamsherman
3 Dec 2007, 12:12 PM
Do you happen to have some basic code that shows the arrangement of Reader/Proxy/View, particularly how to handle committing changes to data? I'm just starting with Ext & DWR and that would be a huge help.

A.

Axel
4 Dec 2007, 10:21 AM
Hello,

Here is some example code to create a ComboBox:



var store = new Ext.data.Store({
proxy: new Ext.data.DWRProxy(MyDwrJS.myMethod),
reader: new Ext.data.MapReader()
});

var comboExt = new Ext.form.ComboBox({
transform: 'myField',
displayField: 'value',
valueField: 'key',
store: store
});


It is uses a MapReader I posted an other thread.

The java code method takes one param (the text currently in the combo) and returns a map<String elementValue, String elementLabel>. The selected value is then sent together with the form on post.

Cheers,
Axel

vashah
28 Dec 2007, 4:07 PM
Hi,

I am a newbie with Ext. I have tried using DWRProxy + Map reader combination for my ComboBox. Here's the code:


var mapDs = new Ext.data.Store({
proxy: new Ext.ux.data.DWRProxy(this.serverInterface.getPossibleBusinessGroups),
reader: new Ext.ux.data.MapReader(),
baseParams: {
launchIdentifier: this.launchIdentifier
}
});
var comboExt = new Ext.form.ComboBox({
fieldLabel: 'Business Group',
displayField: 'value',
valueField: 'key',
store: mapDs
});

DWR works as intented and returns me a map of possible business groups but the combo box hangs in the "Loading..." state.

Help appreciated. Thanks,
Vivek

davewolfs
30 Dec 2007, 3:03 PM
Could someone please explain in more detail what this code is doing.

I am finding code such as Ext.data.DWRProxy.superclass.constructor.call(this) kinda hard to follow.

I assume that this is how Ext is extended? Perhaps some of you could chime in on exactly what this proxy is doing.

Thanks,

Dave

qaz
3 Jan 2008, 5:48 AM
Can some one, create one example, with one form work with dwr 2.0.2, with code?

one example with a table, sould be nice too. :)

Juvs
4 Jan 2008, 3:24 PM
Something I notice when using the extension, when you pass an object as parameter the actual code interact into the attributes of the object and creates an array with the attributes not with the object it self, so I did the next modification:


Ext.data.DWRProxy = function (f) {
Ext.data.DWRProxy.superclass.constructor.call(this);
this.func = f;
};

Ext.extend(Ext.data.DWRProxy, Ext.data.DataProxy, {
load : function(params, reader, loadCallback, scope, arg) {
var dataProxy = this;
dataProxy.fireEvent("beforeload", dataProxy, params);
var args = [];
if (typeof(params) == 'Array') {
for (var param in params) {
args[args.length] = params[param];
}
} else {
args[args.length] = params;
}
args[args.length] = {
callback: function(response) {
dataProxy.fireEvent("load", dataProxy, response, loadCallback);
var records = reader.read(response);
loadCallback.call(scope, records, arg, true);
},
exceptionHandler: function(message) {
dataProxy.fireEvent("loadexception", dataProxy, response, loadCallback, e);
loadCallback.call(scope, null, arg, false);
}
};

this.func.apply(this, args);
}
});

When this situation comes out, well, lets say you have a Java Object on the server that receives an object as parameter:


public class CustomerService extends Service {

//more methods...
//...

public IResponse getAllCustomers(GridPagedRequest pagedRequest) {
//some code here....
return response;
}
}

The parameter object:


public class GridPagedRequest implements Serializable {
private int start;
private int limit;

public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
}

Then in the client (javascript) I defined a DataStore as follows:



// create the Data Store
this.ds = new Ext.data.Store({
proxy: new Ext.data.DWRProxy(CustomerService.getAllCustomers), //Here is the DWR Object call
reader: new Ext.data.MapReader()
});


and load the datastore:


this.ds.load({
params: {start: 0, limit: 15}
});

Before the modification I always get an error on the server, because the DWRProxy creates an array with two values, start and limit, and the server object expects to get an object with two attributes, not two parameters.

In case you need to pass this two parameters, the only change you have to do is:


public class CustomerService extends Service {

//more methods...
//...

public IResponse getAllCustomers(int start, int limit) { //Now, is two parameters
//some code here....
return response;
}
}

and


this.ds.load({
params: [0,15]
});


I hope this help...

benjam72
4 Jan 2008, 9:01 PM
Thanks Juvs, that helps to illuminate how to pass parameters to the Java class and ensuring the correct data type. However, one of the things I still don't understand is where the callback function is defined, when it's passed to DWRProxy, and when it's invoked. Can you help me out?

TheItalianJob
7 Jan 2008, 1:06 AM
Hello Axel,

i'm a newbie in extjs, can you write for our the complete code of this class?

What is the structure of the object IResponse? :-/



public class CustomerService extends Service {

//more methods...
//...

public IResponse getAllCustomers(GridPagedRequest pagedRequest) {
//some code here....
return response;
}
}



Thanks a lot!

hatchetman82
14 Jan 2008, 6:57 AM
When you're firing the "beforeLoad" event, shouldnt you make the event "vetoable" ?
(as in, a negative return value from firing the event should abort the process)

davewolfs
15 Jan 2008, 7:00 AM
Could someone please explain what the following code is doing:

Ext.data.DWRProxy = function (f) {
Ext.data.DWRProxy.superclass.constructor.call(this);
this.func = f;
};

Ext.data.MapReader = function(){
Ext.data.MapReader.superclass.constructor.call(this, null, [
{name: 'key', mapping: 'key'},
{name: 'value', mapping: 'value'}
]);
};

The Class.superclass.constructor.call is confusing me.

hatchetman82
16 Jan 2008, 9:19 AM
as with a lot of other inherited classes, both the Reader and Proxy extend other classes, and so their constructors must call their ancestor's constructors (to set up all the properties they inherit).
the *.superclass.constructor field is created by a call to Ext.extend

davewolfs
16 Jan 2008, 8:38 PM
as with a lot of other inherited classes, both the Reader and Proxy extend other classes, and so their constructors must call their ancestor's constructors (to set up all the properties they inherit).
the *.superclass.constructor field is created by a call to Ext.extend

So is it simply calling the parent constructor? Sorry but I am still confused by your explanation.

AndrewS
16 Jan 2008, 10:05 PM
Hi Davewolfs:

Does this article help? It's for Ext 1.1 but I'm pretty sure it all still holds for Ext 2.x.

http://extjs.com/learn/Manual:Intro:Inheritance

hatchetman82
18 Jan 2008, 2:53 AM
yes, thats what i was saying :-)

kaki
18 Jan 2008, 9:37 PM
Thanks a lot!

SmartChow
21 Jan 2008, 6:02 PM
Hello
Could someone give me MapReader's code please.
Thanks,

shane.fox
21 Jan 2008, 6:09 PM
Hello
Could someone give me MapReader's code please.
Thanks,

I think this is it:
http://extjs.com/forum/showthread.php?t=19522

loeppky
22 Jan 2008, 2:53 PM
I have made some improvements to Axel's original DWRProxy. The have been put into this thread: http://extjs.com/forum/showthread.php?t=23884.

ka3S
7 Feb 2008, 5:46 AM
What is the relationship between DWRProxy and http://getahead.org/dwr/download ?

loeppky
7 Feb 2008, 9:16 AM
Are you asking about what the relationship is between DWR and DWRProxy? If that's the case, DWRProxy is a user-provided Ext extension for having Ext to delegate AJAX work to DWR verses handling it through it's own Ajax classes. Does that answer your question?

ka3S
8 Feb 2008, 5:33 AM
I wanted to know if I should to use DWR framework when I want to use DWRProxy.

loeppky
8 Feb 2008, 10:28 AM
kaS3: DWRProxy has no value outside of using it with the DWR framework that I can think of. So yes, only use DWRProxy if you're using DWR.

krazy
11 Mar 2008, 7:24 PM
Sorry~
Why i can not find dwrProxy,i'am new to ext js.

loeppky
12 Mar 2008, 8:30 AM
krazy: DWRProxy is an user-built extension that you must add in addition to Ext. It is not part of the normal Ext distribution. You should copy and paste DWRProxy from the improved version: http://extjs.com/forum/showthread.php?t=23884