Skip to content

Instantly share code, notes, and snippets.

@adardesign
Forked from anonymous/gist:aa74bd25b7436c90556e
Last active August 29, 2015 14:04
Show Gist options
  • Save adardesign/ee7909f40eae710cf31c to your computer and use it in GitHub Desktop.
Save adardesign/ee7909f40eae710cf31c to your computer and use it in GitHub Desktop.
adrma.states = {
list: [],
USStateList: [],
CAStateList: [],
get: function get(country) {
var self = this,
dfd = $.Deferred(),
stateList = adrma.storage.get("session", country+"StateList"),
if (stateList) {
self[country+"StateList"] = stateList;
return dfd.resolve();
}
adrma.fetchData({
url: "/api/getStates",
dataType: "JSON"
}).done(function (response) {
self.list = response.data;
self[country+"StateList"] = self.getStatesByCountry("US");
self.CAStateList = self.getStatesByCountry("CA");
adrma.storage.set("session", "USStateList", self.USStateList);
adrma.storage.set("session", "CAStateList", self.CAStateList);
dfd.resolve();
});
return dfd;
},
getStatesByCountry: function getStatesByCountry(countryCode) {
var self = this,
resultList;
resultList = $.grep(self.list, function (state) {
return state.countryCode === countryCode;
});
return resultList;
},
render: function render(config) {
config = config || {};
var selectedState = config.selected,
options = config.options,
selectedCountry = options.selectedCountry,
self = this,
dfd = $.Deferred(),
countriesStateSelectTmpl = "countriesStateSelectTmpl",
template = adrma.template,
renderedTmpl;
$.when(
self.get(),
template.get(countriesStateSelectTmpl)).done(function () {
var selected = self.getState(selectedState, selectedCountry),
data = {},
callbackData = {};
data.selected = selected;
//data.list = shippingOnly ? self.getShippingOnly() : self.list;
renderedTmpl = template.render(countriesStatesTemplate, data);
callbackData.selectedValue = selected[1];
callbackData.renderedOptions = renderedTmpl;
callback = $.isFunction(callback) ? callback(callbackData) : "";
dfd.resolve(callbackData);
});
return dfd;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment