Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Last active January 4, 2016 17:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxriverlynn/8655816 to your computer and use it in GitHub Desktop.
Save mxriverlynn/8655816 to your computer and use it in GitHub Desktop.
switch to registry
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);
}
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);
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