mmir.env.view.ViewLoader
Members
-
private,constantCONFIG_DEFAULT_LAYOUT_NAMEString
-
The name of the configuration field that holds the name for the default layout.
-
private,constantCONFIG_PRECOMPILED_VIEWS_MODEString
-
Name of the configuration property that specifies whether or not to use pre-compiled views, i.e. whether to use generated JavaScript files instead of parsing & compiling the "raw" templates (eHTML files).
NOTE: the configuration value, that can be retrieved by querying this configuration-property has is either a Boolean, or a String representation of a Boolean value:
[true|false|"true"|"false"]
NOTE2: There may be no value set at all in the configuration for this property. In this case you should assume that it was set tofalse
.Example
var isUsePrecompiledViews = mmir.conf.getBoolean("usePrecompiledViews");
-
privatedefaultLayoutNameString
-
Name for the default layout, that will be loaded. If NULL, no default layout will be loaded (see below configurationManager.get(CONFIG_DEFAULT_LAYOUT_NAME...))
-
privatedeferPromise
-
Deferred / promise for loading views.
-
protectedisUsePreCompiledViewsBoolean
-
Flag for determining if pre-compiled views (*.js) should be used Reads property
CONFIG_PRECOMPILED_VIEWS_MODE
. If the property is not set,false
is used by default, i.e. no pre-compiled views are used.- Default Value:
- false: use templates files (*.ehtml) and compile them (freshly) on-the-fly
-
privateloggermmir.tools.Logger
-
The logger for the PresentationManager.
Methods
-
staticmmir.env.view.ViewLoader.loadViews(_instance, _layouts, _views, _partials, createViewKey, createPartialKey){Promise}
-
Loads views (layouts and partials): either compiled views (i.e. JS files), or raw views (i.e. eHTML files). The loader checks, if compiled views are up-to-date (using the corresponding checksum file). If a compiled view is not up-to-date (or its checksum file is missing), then the raw view will be loaded and compiled. If raw views are loaded, the parsing-module will loaded for compiling the views (i.e. if only compiled views are loaded, the dependency for the template parser and renderer is not required).
Configuration (configuration.json)
The configuration value"usePrecompiledViews"
(Boolean) allows the determine, if views should always be compiled from the eHTML files (even if up-to-date compiled views are present). For example, configuration"usePrecompiledViews": true
will use compiled views, while"usePrecompiledViews": false
will always compile the eHTML files. If the configuration value formmir.PresentationManager.CONFIG_DEFAULT_LAYOUT_NAME
is set toNULL
no default layout will be loaded. Ifmmir.PresentationManager.CONFIG_DEFAULT_LAYOUT_NAME
is a non-empty string, then the corresponding layout will be used as default layout, instead ofmmir.PresentationManager.DEFAULT_LAYOUT_NAME
.Name Type Description _instance
PresentationManager the instance of the PresentationManager _layouts
Map.<string, Layout> the layout collection of the PresentationManager for adding loaded layouts _views
Map.<string, View> the view collection of the PresentationManager for adding loaded views _partials
Map.<string, Partial> the partials collection of the PresentationManager for adding loaded partials createViewKey
function the PresentationManager's helper function for creating keys to be used when adding views to _views
:
createViewKey(ctrl: {mmir.ctrl.Controller|String}, view: {mmir.view.View|String}) : {String}
createPartialKey
function the PresentationManager's helper function for creating keys to be used when adding partials to _partials
:
createPartialKey(partial: {mmir.view.Partial|String}, view: {mmir.view.View|String}) : {String}
Returns:
Type Description Promise a deferred promise that gets resolved when the views (layouts, and partials) are loaded -
privatecheckCompletion(status)
-
HELPER for checking the loading status. As long as the Deferred
status.loader
is still pending, the loading status will be checked: Depending onstatus.currentLoadCount
andstatus.remainingCtrlCount
the completion of the loading process is checked. If loading is completed, the Deferredstatus.loader
will be resolved. If OPTIONALstatus.loader
(Function) exists, intead of resolvingstatus.loader
, this function is invoked in case of completion withstatus
as argument.Name Type Description status
PlainObject the object for managing the laoding status. -
privatecheckResolved()
-
Helper: called each time a loading-function finishes. Checks if all other loading-functions have finished, and if so, resolves the init-promise.
-
async,privatedoLoadTemplateFile(controller, templateInfo, createConfig, loadStatus)
-
HELPER that loads a single template file asynchronously and creates a corresponding template-class instance (depending on
createConfig
). Thestatus
is updated on successful loading or on error (seeupdateLoadStatus
).Name Type Description controller
mmir.ctrl.Controller the controller to which the template files belong. May be null
: seedoParseTemplate
.templateInfo
PlainObject the JSON-like object containing information for the template (e.g. name
,file-path
etc.; seemmir.ControllerManager#getControllerResources
for more information).createConfig
PlainObject configuration that is used to create a corresponding template-object for the loaded template-contents. The created object will be added to createConfig.collection
(Map; with controller's name as key).loadStatus
PlainObject Object for managing the loading-status. The status is updated and used to determine, if all templates (e.g. from a list) have been (asynchronously) loaded. Example
//EXAMPLE for createConfig for loading template contents into a Partial var theCreateConfig = { constructor: Partial, // the class constructor that takes the loaded template data typeName: 'Partial', // the name of the class that will be created collection: _partials, // the map/dictionary to which the created class-instance will be added keyGen: createPartialKey, // the function for creating the lookup-key (for the dictionary) accessorName: 'getPartials' // the accessor-function's name for accessing the info-objects on the controller-instance }; doLoadTemplateFiles(theCreateConfig).then(function(){ //do something that depends on loading of the template files... }); //EXAMPLE for createConfig for loading template contents into a Layout var theCreateLayoutConfig = { constructor: Layout, // the class constructor that takes the loaded template data typeName: 'Layout', // the name of the class that will be created collection: _layouts, // the map/dictionary to which the created class-instance will be added }; doLoadTemplateFiles(theCreateLayoutConfig).then(function(){ //do something that depends on loading of the template files... }); //for createConfig for loading template contents into a Partial
-
privatedoParseTemplate(controller, templateName, createConfig, status)
-
HELPER: creates a template-object (e.g. a View or a Partial) for the raw template conent. If necessary, the parser-classes (module 'mmirf/parseUtils') are loaded, which are necessary to process the raw template content.
Name Type Description controller
mmir.ctrl.Controller the controller to which the template files belong. May be null
: in this case, this argument will be omitted when creating the template object and creating the lookup-key (e.g. in case of a Layout).templateName
String the name for the template (e.g. file-name without extension) createConfig
PlainObject configuration that is used to create the template-object for the template-contents: createConfig.constructor
: the constructor function IF controller IS NOT null:(controller, templateName, templateContent)
(e.g. View, Partial) IF controller IS null:(templateName, templateContent)
(e.g. Layout)createConfig.collection
: the Map to which the created template-object will be addedcreateConfig.keyGen
: a generator function for creating the lookup-key when adding the template-object to the collection. This function is invoked with(controller.getName(), templateName)
, that is(String, String)
. NOTE: if controller IS null, the keyGen function will not be used, and instead the template-object will be added with the created template-object's name as lookup-key.status
PlainObject the object for managing the loading status. After creating and adding the template-object to the collection, the loading status will be updated via updateLoadStatus
-
async,privatedoProcessTemplateList(createConfig){Promise}
-
Generic helper for loading a list of template files (*.ehtml) that correspond to a specific template type (e.g.
View
s, orPartial
s). If compiled representations of the template file exist AND is up-to-date AND the configuration is set to load the compiled files rather than the raw template file (seeisUsePreCompiledViews
), then the compiled template is used. This function uses an asynchronous method for loading the template-files.
If you want to make sure, that all templates have indeed been loaded, before proceeding with the subsequent program flow, you should have a look at the functionmmir.ControllerManager#foundControllersCallBack
: use the returned Deferred.Promise for executing code that depends on the templates being loaded.Uses
doloadTemplateFile
for loading single template files.Name Type Description createConfig
PlainObject configuration object that determines which templates are loaded, and how the loaded data is processed. - See:
Returns:
Type Description Promise a deferred promise, that resolves after all partials have been loaded NOTE: loading failures will generate a warning message (on the console) but will not cause the Promise to fail. -
privatefailPromise()
-
Helper: called if an error occured in one of the loading-functions: rejects/fails the init-promise.
-
privateisUpToDate(viewContent, preCompiledViewPath, callback)
-
Read the checksum file that was created when the pre-compiled view was created: it contains the view's template size (the length of its String representation) and MD5 hash. -> by calculating the viewContent's size and MD5 hash, we can determine, if it has changed by comparing it with the data of the checksum file.
Name Type Description viewContent
String the content of the view template (i.e. loaded eHTML file) preCompiledViewPath
String the path to the corresponding pre-compiled view file callback
function callback that will be invoked with the result callback(upToDate: Boolean)
-
privateloadLayouts(){Promise}
-
This function loads the layouts for every controller and puts the name of the layouts into the _layouts array.
Returns:
Type Description Promise a deferred promise that gets resolved upon loading all layouts; fails/is rejected, if not at least 1 layout was loaded -
async,privateloadPartials(){Promise}
-
This function actually loads the partials for every controller, creates an instance of a partial class and puts the partial instance in the _partials array.
It uses a asynchronous way of loading the partials-files one after another.
If you want to make sure, that all partials are indeed loaded, before proceeding with the subsequent instructions, you could look at the functionmmir.ControllerManager#foundControllersCallBack
for reference of a function which loads the files one after another - not asynchronously.Returns:
Type Description Promise a deferred promise, that resolves after all partials have been loaded NOTE: loading failures will generate a warning message (on the console) but will not cause the Promise to fail. -
async,privateloadPrecompiledView(rawViewData, targetPath, success, fail)
-
Checks if a pre-compiled view is up-to-date: loads the view, if it is current. If the pre-compiled view is not current, or loading-errors occur, the fail-callback will be triggered (the callback argument may contain information about the cause).
Name Type Description rawViewData
String the text content of the view template (i.e. content of a eHTML file "as is") targetPath
String the path to the pre-compiled view file success
function callback that will be triggered, if pre-compiled view file was loaded NOTE: the JS code of the loaded file may not have been fully executed yet! fail
function callback that will be triggered, if pre-compiled view is not up-to-date or an error occurs while loading the file -
async,privateloadViews(){Promise}
-
This function actually loads the views for every controller, creates an instance of a view class and puts the view instance in the _views array.
- See:
-
- doProcessTemplateList
Returns:
Type Description Promise a deferred promise that gets resolved upon loading all views -
privateupdateLoadStatus(status, hasLoadingFailed)
-
HELPER for updating the loading status. Invokes
checkCompletion
withstatus
as argument.Name Type Description status
PlainObject the object for managing the laoding status: status.currentLoadCount
(Integer): this property will be decreased by 1. This value should initially be set to the count of files, that will / should be loaded. OPTIONALstatus.extLoadStatusFunc
(Function): if this property is set, the function will be invoked withstatus
andhasLoadingFailed
as arguments.hasLoadingFailed
Boolean optional OPTIONAL if present and true
: this indicates that the loading process for the current template file (*.ehtml) has failed. NOTE that this is NOT used, when loading of a _compiled_ template file (*.js) fails!