PDA

View Full Version : SOLVED: Google geocode and doRequest with Ext.data.proxy.Ajax?



jnw
2 Dec 2012, 1:03 PM
hello,

I'm having trouble porting over this small piece of code that performs a google geocode request in an extended ComboBox (from GeoExt). In Ext JS 3.4 this works perfectly:


var geocoder = new google.maps.Geocoder();



.....
.....
tbar: [{
xtype: "gx_geocodercombo",
width: 250,
layer: locationLayer,
displayField: "formatted_address",
store: new Ext.data.JsonStore({
root: null,
fields: [
"formatted_address",
{name: "lonlat", convert: function(v, rec) {
var latLng = rec.geometry.location;
return [latLng.lng(), latLng.lat()];
}},
{name: "bounds", convert: function(v, rec) {
var ne = rec.geometry.viewport.getNorthEast(),
sw = rec.geometry.viewport.getSouthWest();
return [sw.lng(), sw.lat(), ne.lng(), ne.lat()];
}}
],
proxy: new (Ext.extend(Ext.data.DataProxy, {
doRequest: function(action, rs, params, reader, callback, scope, options) {
geocoder.geocode({address: operation.params.q}, function(results, status) {
var readerResult = reader.readRecords(results);
console.log(readerResult);
callback.call(scope, readerResult, options, !!readerResult);
});
}
}))({api: {read: true}})
})
}]


In 4.1 Ext.data.DataProxy is gone so I was trying this using Ext.data.proxy.Ajax:




proxy: new (Ext.extend(Ext.data.proxy.Ajax, {
doRequest: function(operation, callback, scope) {
geocoder.geocode({address: operation.params.q}, function(results, status) {
var readerResult = scope.proxy.reader.readRecords(results);
console.log(readerResult);
callback.call(scope, readerResult, options, !!readerResult);
});
}
}))({api: {read: true}})
})



readerResult returns a constructor containing the correct json but I'm also getting the error:


Uncaught TypeError: Object [object Object] has no method 'getResultSet' Store.js 1148

Any idea what I'm doing wrong? I am totally on the wrong track here?

Thanks in advance.

mitchellsimoens
4 Dec 2012, 7:34 AM
You shouldn't override a method when creating an instance, you should make a new class using Ext.define.

jnw
6 Dec 2012, 1:41 PM
I'm wondering now do I even need a new class? I simplified things to this:



proxy: {
type: 'ajax',
api: {
read: true
},
reader: {
type: 'json',
root: null
},

doRequest: function(operation, callback, scope) {

geocoder.geocode({address: operation.params.q}, function(results, status) {

var readerResult = scope.proxy.reader.readRecords(results);
console.log(readerResult); //geocode data looks ok here

callback.call(scope, readerResult, operation);

});

}
}


I still have the issue:

Uncaught TypeError: Object [object Object] has no method 'getResultSet'

Am I simplifying this too much and I do need a new class? I haven't found many examples of the callback from doRequest. Is this off track?

Thanks much!

jnw
24 Jan 2013, 10:05 AM
Not sure I've done this correctly but it is working for me now.
I didn't find much out there on how to do this so here is what I came up with. Hope it helps someone.



proxy: {
type: 'jsonp',
api: {read: true},

doRequest: function(operation, callback, scope) {
geocoder.geocode({address: operation.params.q}, function(results, status) {

var readerResult = scope.proxy.reader.readRecords(results);
scope.loadRecords(readerResult.records, operation);
scope.loading = false;
scope.fireEvent('load', this, readerResult.records, true);
//console.log(readerResult);
});
}
}