Created
July 18, 2014 15:10
-
-
Save anonymous/aa74bd25b7436c90556e 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() { | |
var self = this, | |
dfd = $.Deferred(), | |
USStateList = adrma.storage.get("session", "USStateList"), | |
CAStateList = adrma.storage.get("session", "CAStateList"); | |
if (USStateList && CAStateList) { | |
self.USStateList = USStateList; | |
self.CAStateList = CAStateList; | |
return dfd.resolve(); | |
} | |
adrma.fetchData({ | |
url: "/api/getStates", | |
dataType: "JSON" | |
}).done(function (response) { | |
self.list = response.data; | |
self.USStateList = 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