-
-
Save mxriverlynn/8655816 to your computer and use it in GitHub Desktop.
switch to registry
This file contains hidden or 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
var employee = someEmployee; | |
var action = "view"; | |
switch (action) { | |
case "view": | |
showEmployee(someEmployee); | |
break; | |
case "edit": | |
editEmployee(someEmployee); | |
break; | |
} | |
function showEmployee(employee){ | |
var view = new ShowEmployeeForm({ | |
model: employee | |
}); | |
view.render(); | |
$("#wizard").html(view.$el); | |
} | |
function editEmployee(employee){ | |
var form = new EditEmployeeForm({ | |
model: employee | |
}); | |
form.render(); | |
$("#wizard").html(form.$el); | |
} |
This file contains hidden or 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
var viewRegistry = new Registry(); | |
viewRegistry.register("view", showEmployee); | |
viewRegistry.register("edit", editEmployee); | |
function showEmployee(employee){ | |
var view = new ShowEmployeeForm({ | |
model: employee | |
}); | |
view.render(); | |
$("#wizard").html(view.$el); | |
} | |
function editEmployee(employee){ | |
var form = new EditEmployeeForm({ | |
model: employee | |
}); | |
form.render(); | |
$("#wizard").html(form.$el); | |
} | |
var actionName = "view"; | |
var action = viewRegistry.getValue(actionName); | |
var employee = getSomeEmployee(); | |
action(employee); |
This file contains hidden or 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
function Registry(defaultValue){ | |
this._defaultValue = defaultValue; | |
this._values = Object.create(null); | |
} | |
Registry.prototype.register = function(name, value){ | |
this._values[name] = value; | |
}; | |
Registry.prototype.getValue = function(name){ | |
var value; | |
if (Object.prototype.hasOwnProperty.call(this._values, name)){ | |
value = this._values[name]; | |
} else { | |
value = this._defaultValue; | |
} | |
return value; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment