Ahojte,
uz jsem to tu jednou resil, ale ted uz si s tim fakt nevim rady tak zkusim
napsat sem. Zkousel jsem bezpocet ruznych for a bez vysledku. I ruzne typy
dwr proxy.
problem:
1. funguje - zobrazim si v pohodce grid pomoci extjs
2. funguje - data se jednoznacne ze serveru nactou, coz vidim ve
firebugu.
3. nefunguje - nezobrazi se data v gridu. Ale neni vyhozena zadna chyba!
prilozene zdrojove kody a objekty vracene pres dwr proxy:
1. Tridy pouzivane pro prenos dat
2. Trida reprezentujici service
3. Zdrojovy kod dwr proxy
4. Zdrojovy kod rozhrani
5. Odpoved dwr zobrazena firebugem
Tridy pouzivane pro prenos dat:
-------------------------------
package test.test.chat.entity;
import java.io.Serializable;
public class ChatItem implements Serializable {
protected String chatRoomUuid="";
protected long lineId=0;
protected String sender="";
protected String text="";
public void setChatRoomUuid(String chatRoomUuid) { this.chatRoomUuid =
chatRoomUuid; }
public String getChatRoomUuid() { return chatRoomUuid; }
public void setLineId(long lineId) { this.lineId = lineId; }
public long getLineId() { return lineId; }
public void setSender(String sender) { this.sender = sender; }
public String getSender() { return sender; }
public void setText(String text) { this.text = text; }
public String getText() { return text; }
}
Trida reprezentujici service:
-----------------------------
package test.test.chat.services;
import java.util.ArrayList;
import java.util.List;
import test.test.chat.entity.ChatItem;
public class ChatService {
public List<ChatItem> getItems() {
List<ChatItem> chatItemsList = new ArrayList<ChatItem>();
ChatItem item1 = new ChatItem();
item1.setLineId(1);
item1.setChatRoomUuid("test1");
item1.setSender("Pet");
item1.setText("text of test1");
chatItemsList.add(item1);
ChatItem item2 = new ChatItem();
item1.setLineId(2);
item1.setChatRoomUuid("test1");
item1.setSender("PanPoutko");
item1.setText("text of test1");
chatItemsList.add(item2);
return chatItemsList;
}
}
Zdrojovy kod dwr proxy:
-----------------------
/**
* http://extjs.com/forum/showthread.php?t=19529
*/
Ext.namespace("Ext.ux.data");
/**
* @class Ext.ux.data.DWRProxy
* @extends Ext.data.DataProxy
* @author loeppky
* An implementation of Ext.data.DataProxy that uses DWR to make a remote
call.
* @constructor
* @param {Object} config A configuration object.
*/
Ext.ux.data.DWRProxy = function(config){
Ext.apply(this, config); // necessary since the superclass doesn't call
apply
Ext.ux.data.DWRProxy.superclass.constructor.call(this);
};
Ext.extend(Ext.ux.data.DWRProxy, Ext.data.DataProxy, {
/**
* @cfg {Function} dwrFunction The DWR function for this proxy to call
during load.
* Must be set before calling load.
*/
dwrFunction: null,
/**
* @cfg {String} loadArgsKey Defines where in the params object passed to
the load method
* that this class should look for arguments to pass to the
"dwrFunction".
* The order of arguments passed to a DWR function matters.
* Must be set before calling load.
* See the explanation of the "params" parameter for the load function
for further explanation.
*/
loadArgsKey: 'dwrFunctionArgs',
/**
* Load data from the configured "dwrFunction",
* read the data object into a block of Ext.data.Records using the passed
[EMAIL PROTECTED] Ext.data.DataReader} implementation,
* and process that block using the passed callback.
* @param {Object} params An object containing properties which are to be
used for the request to the remote server.
* Params is an Object, but the "DWR function" needs to be called with
arguments in order.
* To ensure that one's arguments are passed to their DWR function
correctly, a user must either:
* 1. call or know that the load method was called explictly where the
"params" argument's properties were added in the order expected by DWR OR
* 2. listen to the "beforeload" event and add a property to params
defined by "loadArgsKey" that is an array of the arguments to pass on to
DWR.
* If there is no property as defined by "loadArgsKey" within "params",
then the whole "params" object will be used as the "loadArgs".
* If there is a property as defined by "loadArgsKey" within "params",
then this property will be used as the "loagArgs".
* The "loadArgs" are iterated over to build up the list of arguments to
pass to the "dwrFunction".
* @param {Ext.data.DataReader} reader The Reader object which converts
the data object into a block of Ext.data.Records.
* @param {Function} callback The function into which to pass the block
of Ext.data.Records.
* The function must be passed <ul>
* <li>The Record block object</li>
* <li>The "arg" argument from the load function</li>
* <li>A boolean success indicator</li>
* </ul>
* @param {Object} scope The scope in which to call the callback
* @param {Object} arg An optional argument which is passed to the
callback as its second parameter.
*/
load: function(params, reader, loadCallback, scope, arg){
var dataProxy = this;
if (dataProxy.fireEvent("beforeload", dataProxy, params) !==
false) {
var loadArgs = params[this.loadArgsKey] || params; // the Array or
Object to build up the "dwrFunctionArgs"
var dwrFunctionArgs = []; // the arguments that will be passed to the
dwrFunction
if (loadArgs instanceof Array) {
// Note: can't do a foreach loop over arrays because Ext added the
"remove" method to Array's prototype.
// This "remove" method gets added as an argument unless we explictly
use numeric indexes.
for (var i = 0; i < loadArgs.length; i++) {
dwrFunctionArgs.push(loadArgs[i]);
}
} else { // loadArgs should be an Object
for (var loadArgName in loadArgs) {
dwrFunctionArgs.push(loadArgs[loadArgName]);
}
}
dwrFunctionArgs.push({
callback: function(response){
// call readRecords verses read because read will attempt to decode
the JSON,
// but as this point DWR has already
decoded the JSON.
var records =
reader.readRecords(response);
dataProxy.fireEvent("load", dataProxy,
response, loadCallback);
loadCallback.call(scope, records, arg,
true);
},
exceptionHandler: function(message, exception){
// the event is supposed to pass the response, but since DWR doesn't
provide that to us, we pass the message.
dataProxy.fireEvent("loadexception", dataProxy, message,
loadCallback, exception);
loadCallback.call(scope, null, arg,
false);
}
});
this.dwrFunction.apply(Object, dwrFunctionArgs); // the scope for
calling the dwrFunction doesn't matter, so we simply set it to Object.
} else { // the beforeload event was vetoed
callback.call(scope || this, null, arg, false);
}
}
});
Zdrojovy kod rozhrani:
----------------------
/**
* Application Layout
* by Jozef Sakalos, aka Saki
* http://extjs.com/learn/Tutorial:Application_Layout_for_Beginners
*/
// reference local blank image
Ext.BLANK_IMAGE_URL = "/dwrextjs/ext2js/resources/images/default/s.gif";
// create namespace
Ext.namespace('myNameSpace');
// Just to allow this tutorial to work for 1.1 and 2.
Ext.Ext2 = (Ext.version && (Ext.version.indexOf("2") == 0));
var GridUI = function() {
var ds = new Ext.data.Store({
proxy: new Ext.ux.data.DWRProxy({
dwrFunction: ChatService.getItems,
listeners: {
'beforeload': function(dataProxy, params){
params[dataProxy.loadArgsKey] = ['query'];
}
}
}),
reader: new Ext.data.JsonReader({ }, Ext.data.Record.create([
{ name: "lineId", type: "String"},
{ name: "sender", type: "String"},
{ name: "text", type: "String"}
]))
});
ds.load();
var grid = new Ext.grid.GridPanel({
el: "mygrid",
autoScroll: true,
autoHeight: true,
autoWidth: true,
ds: ds,
cm: new Ext.grid.ColumnModel([
{ header: 'lineId', dataIndex: 'lineId', width: 250,
sortable: true },
{ header: 'sender', dataIndex: 'sender', width: 250,
sortable: true },
{ header: 'text', dataIndex: 'text', width: 250,
sortable: true }
]),
sm: new Ext.grid.RowSelectionModel({
singleSelect: true,
listeners: {
rowselect: function(smObj, rowIndex, record) {
selRecordStore = record;
}
}
}),
autoSizeColumns: true,
trackMouseOver: true,
tbar: [ new Ext.Toolbar.Button({
text: 'Add', handler: function(){
//Load New Form
alert("add item");
}
}), new Ext.Toolbar.Button({
text: 'Edit', handler: function(){
//Load New Form
//Load Data
form.loadRecord(selRecordStore);
}
}), new Ext.Toolbar.Button({
text: 'Delete', handler: function(){
//Call for Delete for the record with this identifier
var id = selRecordStore["field1"]
}
})],
bbar: new Ext.PagingToolbar({
pageSize: 10,
store: ds,
displayInfo: true,
displayMsg: "Displaying Records {0} - {1} of {2}",
emptyMsg: "No Records to display",
items: ['-']
})
});
return {
init : function() {
grid.render();
}, getStore: function() {
return ds;
}
}
}();
Ext.onReady(GridUI.init, GridUI, true);
Odpoved dwr zobrazena firebugem:
--------------------------------
//#DWR-INSERT
//#DWR-REPLY
var s0={};var
s1={};s0.chatRoomUuid="test1";s0.lineId=2;s0.sender="PanPoutko";s0.text="text
of test1"
;
s1.chatRoomUuid="";s1.lineId=0;s1.sender="";s1.text="";
dwr.engine._remoteHandleCallback('0','0',[s0,s1]);
--
Petr Burdik
mail: [EMAIL PROTECTED]
www: http://www.petujek.net/