Skip to content

Instantly share code, notes, and snippets.

@campreb
Created November 5, 2012 00:25
Show Gist options
  • Save campreb/4014539 to your computer and use it in GitHub Desktop.
Save campreb/4014539 to your computer and use it in GitHub Desktop.
Use AddressFinder API with Sencha Touch 2
Ext.define('App.model.Address', {
extend: 'Ext.data.Model',
config: {
fields: [
{
name: 'a'
}, {
name: 'pxid'
}, {
name: 'v'
}
]
},
//Call this function to get extended data about the location (e.g when the user taps the result)
getInfo: function(callback) {
Ext.data.JsonP.request({
url: "http://addressfinder.co.nz/api/address/info",
callbackKey: "callback",
params: {
pxid: this.get("pxid"),
key: "ADDRESSFINDER_API_KEY",
secret: "ADDRESSFINDER_SECRET_KEY",
format: "json"
},
success: function(response) {
callback(response);
}
});
}
});
Ext.define("App.store.AddressStore", {
extend: "Ext.data.Store",
requires: ["App.model.Address", 'Ext.data.proxy.JsonP'],
config: {
model: "App.model.Address",
storeId: 'addressStore'
},
//Call this function on the "keyup" event
getResults: function(query, gr_callback) {
this.gr_callback = gr_callback;
this.address_ready = this.location_ready = false;
this.last_query = query;
if (query !== '') {
this.getAddresses(query);
this.getLocations(query);
} else {
this.gr_callback([]);
}
},
showResults: function() {
var results,
_this = this;
if (this.address_ready && this.location_ready) {
this.removeAll();
results = Ext.Array.merge(this.location_data, this.address_data).slice(0, 10);
Ext.Array.each(results, function(result) {
_this.add(result);
});
return this.gr_callback(results);
}
},
getAddresses: function(query) {
var _this = this;
Ext.data.JsonP.request({
url: "http://addressfinder.co.nz/api/address",
callbackKey: 'callback',
params: {
key: "ADDRESSFINDER_API_KEY",
secret: "ADDRESSFINDER_SECRET_KEY",
format: "json",
q: query
},
success: function(response) {
if (_this.last_query === query) {
_this.address_data = response.completions.slice(0, 10);
_this.address_ready = true;
_this.showResults();
}
}
});
},
getLocations: function(query) {
var _this = this;
Ext.data.JsonP.request({
url: "http://addressfinder.co.nz/api/location",
callbackKey: 'callback',
params: {
key: "ADDRESSFINDER_API_KEY",
secret: "ADDRESSFINDER_SECRET_KEY",
format: "json",
q: query
},
success: function(response) {
if (_this.last_query === query) {
_this.location_data = response.completions.slice(0, 10);
_this.location_ready = true;
_this.showResults();
}
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment