Skip to content

Instantly share code, notes, and snippets.

@nktpro
Created July 29, 2011 20:00
Show Gist options
  • Save nktpro/1114601 to your computer and use it in GitHub Desktop.
Save nktpro/1114601 to your computer and use it in GitHub Desktop.
apply(*) pattern
applyFoo: function(foo) {
var currentFooInstance = this.getFoo();
// If foo config is an instance of Foo class, destroy the current instance (if it exists) and replace with the new one
if (foo instanceof Foo) {
if (currentFooInstance) {
currentFooInstance.destroy();
}
currentFooInstance = foo;
}
// Otherwise:
// - If foo is not a valid object, throw an error during development mode
// - Otherwise:
// + If currentFooInstance exists, set the config for it
// + Otherwise create a new instance of Foo with that config
else {
//<debug error>
if (!Ext.isObject(foo)) {
Ext.logger.error("Invalid 'foo' config, must be either an instance of Foo or a config object");
}
//</debug>
if (currentFooInstance) {
currentFooInstance.setConfig(foo);
}
else {
currentFooInstance = new Foo(foo);
}
}
// currentFooInstance is always a valid instance of Foo here,
// and will be assigned to 'this.foo' immediately after this
return currentFooInstance;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment