Skip to content

Instantly share code, notes, and snippets.

@pstjvn
Created October 19, 2015 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pstjvn/d6f275a3833ad18cea22 to your computer and use it in GitHub Desktop.
Save pstjvn/d6f275a3833ad18cea22 to your computer and use it in GitHub Desktop.
example of @template
goog.scope(function() {
var ngmodel = pstj.ds.ngmodel;
/**
* @define {number} Defins the maximum number of NG cache instances to be
* used in the application. Attempts touse more cache instances (i.e. more
* bound elements/roots) will result in error. When defining the limit take into
* account the fact that using large number might result in slower response
* times and overwhole application lags.
*/
goog.define('pstj.ds.ngmodel.MAX_CACHE_SIZE', 200);
/**
* Logger instance - helps debugging the template resolutions.
* @type {goog.log.Logger}
* @private
*/
ngmodel.logger_ = goog.log.getLogger('pstj.ds.ngmodel');
/**
* Provides cache mecanizm for elements and their ng attributes
* in order to query them only once per element and cache the result.
*
* This class is considered strictly internal and should not be accessed
* anywhere else.
*
* @private
*/
ngmodel.Cache_ = goog.defineClass(null, {
constructor: function() {
/**
* List the nodes that we need to potentially update on model update.
* @type {!Array<!Node>}
* @private
*/
this.nodes_ = [];
/**
* The length of items to consider. We need this in order to not clear the
* other cache lists (only null them out) and thus remain as static as
* possible.
* @type {!number}
* @private
*/
this.length_ = 0;
/**
* List of symbol names to match from the live model.
* @type {!Array<!string>}
* @private
*/
this.modelNames_ = [];
/**
* List of lists of filter functions to apply to the resolved value of the
* model look-up by symbol. Filter functions should be stateless.
* @type {!Array<!function(string): string>}
* @private
*/
this.filters_ = [];
/**
* Cache for the application model type: i.e. how do we apply the resolved
* data to the actual HTML.
* @type {!Array<ngmodel.CACHE_TYPE>}
* @private
*/
this.applyTypes_ = [];
/**
* Cache for previously / last applied value.
* Note that the use of this should become optional as it might increase
* dramatically the memory footprint of your application in case you have
* a large number of bindings.
*
* In future version caching the values will become inactive by default and
* will be activated behind a compile time flag.
* @type {!Array<string|number|boolean|undefined|null>}
* @private
*/
this.values_ = [];
},
/**
* Updates the view the cache is currently refering to.
* @param {Object|Array|null} model
* @private
*/
update_: function(model) {
for (var i = 0; i < this.length_; i++) {
var barevalue = ngmodel.getNestedProperty_(model, this.modelNames_[i]);
var rawvalue = barevalue.toString();
if (!goog.isNull(this.filters_[i])) {
rawvalue = this.filters_[i](rawvalue);
}
if (this.values_[i] != rawvalue) {
this.values_[i] = rawvalue;
switch (this.applyTypes_[i]) {
case ngmodel.CACHE_TYPE.IMAGE:
this.nodes_[i].src = rawvalue;
break;
case ngmodel.CACHE_TYPE.SHOW:
goog.style.setElementShown(goog.asserts.assertElement(
this.nodes_[i]), !!barevalue);
break;
case ngmodel.CACHE_TYPE.HTML:
this.nodes_[i].innerHTML = rawvalue.toString();
break;
case ngmodel.CACHE_TYPE.TEXT:
goog.dom.setTextContent(this.nodes_[i], rawvalue.toString());
break;
default:
goog.log.error(ngmodel.logger_, 'Attempting to use unknown ' +
'application type: ' + this.applyTypes_[i]);
}
}
}
}
});
/**
* Provides pool of cache objects. This allows for object reuse and
* assures that we do not go over a pre-defined limit of instances as to
* assure low impact on application level.
*
* The class is considered internal for this implementation and should not be
* accessed anywhere else.
*
* @private
* @template T
*/
ngmodel.Pool_ = goog.defineClass(goog.structs.Pool, {
constructor: function() {
goog.structs.Pool.call(this, 1, pstj.ds.ngmodel.MAX_CACHE_SIZE);
},
/**
* @override
* @return {T}
*/
createObject: function() {
return new ngmodel.Cache_();
},
/** @override */
objectCanBeReused: goog.functions.TRUE,
/** @override */
disposeObject: function(obj) {
goog.array.clear(obj.modelNames_);
goog.array.clear(obj.filters_);
goog.array.clear(obj.values_);
goog.array.clear(obj.applyTypes_);
obj.nodes_ = null;
},
/** @override */
releaseObject: function(obj) {
for (var i = 0; i < obj.length_; i++) {
obj.modelNames_[i] = '';
obj.filters_[i] = [];
obj.values_[i] = '';
}
obj.nodes_ = null;
obj.length_ = 0;
return goog.base(this, 'releaseObject', obj);
}
});
/**
* Instance to use as pool for ng-cache objects.
* @type {!ngmodel.Pool_<!ngmodel.Cache_>}
* @private
*/
ngmodel.pool_ = new ngmodel.Pool_();
/**
* Enumerates the ways we know how to apply data on the DOM.
* @enum {number}
*/
ngmodel.CACHE_TYPE = {
IMAGE: 0,
SHOW: 1,
TEXT: 2,
HTML: 3
};
/**
* Cache of elemen IDs to cache instances.
* @type {Object<string, ngmodel.Cache_>}
* @private
*/
ngmodel.cache_ = {};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment