Skip to content

Instantly share code, notes, and snippets.

@derickbailey derickbailey/1.js
Last active Jan 4, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.