Last active
September 2, 2015 15:53
-
-
Save michaelwehrley/2846f9aa7b45fe438916 to your computer and use it in GitHub Desktop.
refactoring 2 controllers into a log controller
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
this.CURRENTLY_SMOKING = 'currently_smoking'; | |
this.SMOKING_REASON = 'smoking_reason'; | |
this.URGE_STRENGTH = 'urge_strength'; | |
this.SMOKING_REASONS = ['Reduce craving', | |
'Soon going where I can’t smoke', | |
'Cope with negative emotion', | |
'Enhance positive emotion', | |
'Habit/automatic', | |
'Opportunity to socialize', | |
'Break from work/studying', | |
'Boredom/to kill time', | |
'other']; | |
this.STATE_TIMING = 'timing'; | |
this.STATE_REASON = 'reason'; | |
this.STATE_URGE = 'urge'; | |
this.STATE_FINAL = 'final'; | |
// Initizalization state | |
this.state = this.STATE_TIMING; | |
this.cigaretteLog = {}; | |
this.getCurrentlySmoking = function() { | |
return this.cigaretteLog[this.CURRENTLY_SMOKING]; | |
}; | |
this.saveCigaretteLog = function() { | |
cigaretteLogService.saveCigaretteLog(this.cigaretteLog); | |
this.cigaretteLog = {}; | |
$location.url(Routes.HOME); | |
}; | |
this.setTiming = function(currentlySmoking) { | |
this.cigaretteLog[this.CURRENTLY_SMOKING] = currentlySmoking; | |
this.state = this.STATE_REASON; | |
}; | |
this.setState = function(newState) { | |
this.state = newState; | |
} | |
} | |
// becomes... | |
this.CURRENTLY_SMOKING = 'currently_smoking'; | |
this.SMOKING_REASON = 'smoking_reason'; | |
this.STATE_INITIAL = 'timing'; | |
this.getCurrentlySmoking = function() { | |
return this._getLog([this.CURRENTLY_SMOKING]); | |
}; | |
this.saveLog = function() { | |
cigaretteLogService.saveCigaretteLog(this._getLog()); | |
}; | |
this.setTiming = function(currentlySmoking) { | |
this._setLog([this.CURRENTLY_SMOKING], currentlySmoking); | |
this.state = this.STATE_REASON; | |
}; | |
} |
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
// prototype methods on abstract LogController | |
this.STATE_FINAL = 'final'; | |
this.STATE_REASON = 'reason'; | |
this.URGE_STRENGTH = 'urge_strength'; | |
this.STATE_URGE = 'urge'; | |
this.REASONS = ['Reduce craving', | |
'Soon going where I can’t smoke', | |
'Cope with negative emotion', | |
'Enhance positive emotion', | |
'Habit/automatic', | |
'Opportunity to socialize', | |
'Break from work/studying', | |
'Boredom/to kill time', | |
'other']; | |
this.initialize = function() { | |
this.state = this._getInitialState(); | |
this._resetLog(); | |
} | |
this.saveLog = function() { | |
throw Error(message: 'saveLog must be defined'); | |
}; | |
this.save = function() { | |
this.saveLog(); | |
resetLog(); | |
$location.url(Routes.HOME); | |
}; | |
this.setState = function(nextState) { | |
this.state = nextState; | |
}; | |
function _getInitialState() { | |
if (this.INITIAL_STATE) { | |
return this.INITIAL_STATE; | |
} else { | |
throw Error(message: "INTIAL_STATE constant must be defined"); // this.STATE_TIMING or this.STATE_REASON | |
} | |
}; | |
function _getLog(key) { | |
return this.log[key]; | |
}; | |
function _resetLog() { | |
this.log = {} | |
}; | |
function _setLog(key, value) { | |
this.log[key] = 'value' | |
}; | |
this.initialize(); |
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
this.TEMPTATION_REASON = 'temptation_reason'; | |
this.URGE_STRENGTH = 'urge_strength'; | |
this.TEMPTATION_REASONS = ['Reduce craving', | |
'Soon going where I can’t smoke', | |
'Cope with negative emotion', | |
'Enhance positive emotion', | |
'Habit/automatic', | |
'Opportunity to socialize', | |
'Break from work/studying', | |
'Boredom/to kill time', | |
'other']; | |
this.STATE_REASON = 'reason'; | |
this.STATE_URGE = 'urge'; | |
this.STATE_FINAL = 'final'; | |
// Initizalization state | |
this.state = this.STATE_REASON; | |
this.temptationLog = {}; | |
this.saveTemptationLog = function() { | |
temptationLogService.saveTemptationLog(this.temptationLog); | |
this.temptationLog = {}; | |
$location.url(Routes.HOME); | |
}; | |
this.setState = function(nextState) { | |
this.state = nextState; | |
}; | |
// becomes... | |
this.STATE_INITIAL = 'timing'; | |
this.TEMPTATION_REASON = 'temptation_reason'; | |
this.saveLog = function() { | |
temptationLogService.saveTemptationLog(this.temptationLog); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Then instead of
saveCigaretteLog
you callsave