PDA

View Full Version : Grid not populated via json request using ScriptTagProxy



stodge
28 Mar 2011, 3:39 AM
I'm trying to use ScriptTagProxy to load a grid with data received from a Tornado web server. My original server is Django based. My code works if I request the json data from the originating Django server but it fails as soon as I switch to using cross domain scripting. Firebug shows the json response arrives from Tornado but the grid isn't populated.

My javascript:


RoomListPanel = Ext.extend(Ext.grid.GridPanel, {

initComponent: function() {

this.createRoomButton = new Ext.Button({
text: 'Create Room',
id: 'create-room-button',
listeners: {
click: function() {
alert('Creating room');
}
}
});

this.joinRoomButton = new Ext.Button({
text: 'Join Room',
id: 'join-room-button',
listeners: {
click: function() {
alert('Joining room');
}
}
});

Ext.apply(this, {
tbar: [
this.createRoomButton,
this.joinRoomButton
],

store: new Ext.data.JsonStore({
// store configs
autoLoad: true,
proxy: new Ext.data.ScriptTagProxy({
url: 'http://localhost:8888/'
//~ url: 'http://192.168.2.9:9000/chat/default/index.json'
}),

// reader configs
root: 'rooms',
fields: ['name', 'title', 'public', 'owner']
}),

});

RoomListPanel.superclass.initComponent.call(this, arguments);
},

colModel: new Ext.grid.ColumnModel({
defaults: {
width: 120,
sortable: true
},
columns: [
{header: 'Name', dataIndex: 'name'},
{header: 'Title', dataIndex: 'title'},
{header: 'Public', dataIndex: 'public'},
{header: 'Owner', dataIndex: 'owner'},
]
})
});

// register xtype to allow for lazy initialization
Ext.reg('roomlistpanel', RoomListPanel);

var chatPanel = new RoomListPanel({
title: 'Chat Rooms',
width: 600,
height: 300,
renderTo: document.body
});
});

And my Tornado code is:


import tornado.ioloop
import tornado.web
import tornado

class MainHandler(tornado.web.RequestHandler):
def get(self):
data = {
"response": True,
"rooms": [{
'name': 'support',
'title': 'Support Room',
'public': True,
'owner': 'Mike',
}]
}
resp = tornado.escape.json_encode(data)
print('Returning list of rooms')
self.write(resp)
self.finish()

application = tornado.web.Application([
(r"/", MainHandler),
])

if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

Any suggestions appreciated. Thanks

stodge
28 Mar 2011, 4:55 AM
Oops forgot to FULLY read the docs:

The content passed back from a server resource requested by a ScriptTagProxy must be executable JavaScript source code because it is used as the source inside a <script> tag.