Javascript utilities: mvcct.utils

Requires MVCControlToolkit.Controls.x.x.x.js or the smaller MVCControlToolkit.Controls.Core.x.x.x.js, and MvcControlToolkit.Utils-x.x.x.min.js.

Attention: MvcControlToolkit.Utils-x.x.x.min.js. works properly also with datastructures containing knockout observables, because it is able to detect if knockout.js is installed. Therefore, put it affter knockout.js if it is used in the page.

The mvcct.utils namespace includes some tools usefull to build application vased on Client Blocks,  knockout and/or single-page applications.

mvcct.utils.cache: it creates a new cache object., name, [wholeObservable=false])

Access the property of the object item specified by the string expression name. name has the format prop.subprop1.subprop2... Any knockout observable encountered in the path is unwrapped. If wholeObservable is false (the default) it always returns an unwrapped value. If wholeObservable is true and the last property of the path is an observable it returns the observable. Setting wholeObservable to true is usefull if one needs to read the value and possibly to change it, too.

mvcct.utils.propertySet(item, name, value)

Set the property of the object item specified by the string expression name to the value, valuename has the format along the whole path maybe also knockout observables. If a property along the path doesn't exist it is created and an empty object is assigned to it, till the leaf of the expression is reached.

mvcct.utils.arrayDiff(arr1, arr2, [keyExpression=null], [keyIsComplex=false])

Return the result of subtracting arr2 from arr1. Both arr2 and arr1 can be siple arrays or observable arrays. The difference is computed efficiently, with the help of an hash table. If the elements of arr1 and arr2 are complex objects two elements are considered identical if they have the same key; the name of the property containing the key is specified through keyExpression with the format "property1.subptoperty1...". If keyIsComplex is set to true, the algorithm assumes that the key is, in turn, a complex object, and transforms each key in json before performing key comparisons.

 mvcct.utils.visitModel(source, callBack, [visitArrays=false], [onlyObjects=false])

it visits recursively a hierarchy of javascript objects rooted in source. On each visited property callback is invoked and passed: 1) the string expression representing the current property, 2) the value of the current property, 3) a string representint the type of the previous value obtained by applying mvcct.utils.classof, (see below). 

if onlyObjects is true the callback is invoked just on properties containing javasctipt objects ({....} types).

If and only if visitArray is true all elements of arrays are recursively visited. 

mvcct.utils.cloneDeep(source, [cf=false])

it returns a deep copy of the javascript object source. If cf is true also funtion definitions are copied into the new object.


Return a string that represents the type of o. Possible return values are: 'string', 'boolean', 'number', 'date',  'array',  'function',  'object'.

mvcct.utils.isStringmvcct.utils.isGuidmvcct.utils.isDatemvcct.utils.isArraymvcct.utils.isObject, and mvcct.utils.isFunction

 Check a specific type.  mvcct.utils.isGuid checks if a values is a string with the format of a guid (guids are represented this way in javascript)



Returns the id that is automatically associated by the Mvc helpers to an input field whose name is name. For instance, if name is  Adsress.Street it returns Adsress_Street.


mvcct.utils.cloneEntity(x, [visitRelation=false], [cloneArray=false])

Creates a deep clone of an the object x. Properties that are ko observables are cloned as observables.

If visitRelation is true arrays are cloned too but their elements are copied by reference (not cloned). If visitRelation is false arrays are not copied into the new object. If both visitRelation and cloneArray are true also all elements of arrays are cloned (attention! No recursion check is performed).

Functions are copied by reference.


mvcct.utils.restoreEntity (x, y, [visitRelation=false])

Copies all leaf values of all properties that are defined in both x and y from x to y.  Sub-object contained in properties of both x and y are traversed recursively to find leaf properties. If visitRelation is false arrays are not traversed, thus arrays of y will not be modified.  If visitRelation is true arrays of x are copied by reference into y if y has corresponding arrays with the same property names. This function handle ko observables, too.


mvcct.utils.Track (x, [visitRelation=false], [cloneArray=false])

Starts changes tracking on object x. A clone of x is created with mvcct.utils.cloneEntity(x, visitRelation, cloneArray)  and stored for a possible undo and for possible changes checks. 



Undoes all changes done on the tracked object x, restoring all initial values.



Returns true iff the tracked object x has changed.



Destroys the clone of the tracked object x, so it passes in the unchanged state again. To start changes tracking on x again we must call mvcct.utils.Track again. Attention calling mvcct.utils.Track(x) on a tracked object x has no effect if one has not called mvcct.utils.accept(x) before.

Last edited Jun 22, 2014 at 10:45 AM by frankabbruzzese, version 9


No comments yet.