Skip to content

Instantly share code, notes, and snippets.

@intoxopox
Last active October 12, 2018 17:14
Show Gist options
  • Save intoxopox/86a6be4a25f9b46b091437405d81a7ab to your computer and use it in GitHub Desktop.
Save intoxopox/86a6be4a25f9b46b091437405d81a7ab to your computer and use it in GitHub Desktop.
BaseController - Controller base class that uses DeepProxy for data binding and callbacks.
////////////////////////////////////////////////////////////////////////////////
// Copyright(C) 2018 David Hamiter
////////////////////////////////////////////////////////////////////////////////
'use strict';
import DeepProxy, { DeepProxyCallback } from "./DeepProxy";
export default abstract class BaseController<TModel extends object>
{
//----------------------------------------------------------------------
//
// Properties
//
//----------------------------------------------------------------------
private _className:string;
public get className():string {
return this._className || (<any>this).constructor.name;
}
private _dataStore: DeepProxy<TModel>;
public get dataStore():TModel {
return this._dataStore ? this._dataStore.data : null;
}
public set dataStore(val:TModel) {
this._dataStore = new DeepProxy(val);
if (this._dataStoreChange) this._dataStore.addOnChange(this._dataStoreChange);
}
public get dataStoreDeepProxy():DeepProxy<TModel> {
return this._dataStore;
}
private _dataStoreChange:DeepProxyCallback;
public get dataStoreChange():DeepProxyCallback {
return this._dataStoreChange;
}
public set dataStoreChange(val:DeepProxyCallback) {
// remove old dataStoreChange deepProxy bindings
if (this._dataStore && this._dataStoreChange) this._dataStore.removeOnChange(this._dataStoreChange);
// warn if callback has no binding for 'this', as could screw up scope in callback logic.
if (!val.name.includes("bound ")) console.warn("dataStoreChange", val.name, "has no binding. Scope may be incorrect.");
// set new dataStoreChange
this._dataStoreChange = val;
// set new dataStoreChange deepProxy bindings
if (this._dataStore && this._dataStoreChange) this._dataStore.addOnChange(this._dataStoreChange);
}
//----------------------------------------------------------------------
//
// Constructor
//
//----------------------------------------------------------------------
constructor(dataStore?:TModel, dataStoreChange?:DeepProxyCallback) {
if (!dataStoreChange) dataStoreChange = this.onDataStoreChange.bind(this);
this.dataStoreChange = dataStoreChange;
if (dataStore) this.dataStore = dataStore;
}
//----------------------------------------------------------------------
//
// Event Handlers
//
//----------------------------------------------------------------------
/**
* Default dataStoreChange callback. Convinient for overriding.
*/
protected onDataStoreChange(prop?:PropertyKey | string[], newVal?:any):void {
//console.log(this.toString(), "Datastore changed!", "prop:", prop, "newVal:", newVal);
}
//----------------------------------------------------------------------
//
// Methods
//
//----------------------------------------------------------------------
public toString():string {
return this.className;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment