-
-
Save adardesign/ee7909f40eae710cf31c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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