22/*global mx, define, require, browser, devel, console */
33/*mendix */
44
5- // Required module list. Remove unnecessary modules, you can always get them back from the boilerplate.
65define ( 'formatstring/widget/formatstring' , [ 'dojo/_base/declare' , 'mxui/widget/_WidgetBase' , 'dijit/_TemplatedMixin' ,
76 'mxui/dom' , 'dojo/dom' , 'dojo/dom-class' , 'dojo/_base/lang' , 'dojo/text' , 'dojo/json' ,
87 'dojo/_base/kernel' , 'dojo/_base/xhr' , 'dojo/text!formatstring/lib/timeLanguagePack.json' , 'dojo/text!formatstring/widget/template/formatstring.html'
98] , function ( declare , _WidgetBase , _TemplatedMixin , dom , dojoDom , domClass , lang , text , json , dojo , xhr , languagePack , widgetTemplate ) {
109 'use strict' ;
1110
12- // Declare widget's prototype.
1311 return declare ( 'formatstring.widget.formatstring' , [ _WidgetBase , _TemplatedMixin ] , {
14- // _TemplatedMixin will create our dom node using this HTML template.
1512 templateString : widgetTemplate ,
16- /**
17- * Internal variables.
18- * ======================
19- */
13+
2014 _wgtNode : null ,
2115 _contextGuid : null ,
2216 _contextObj : null ,
2317 _handles : [ ] ,
2418 _timeData : null ,
2519 attributeList : null ,
2620
27- /**
28- * Mendix Widget methods.
29- * ======================
30- */
31-
32- constructor : function ( ) {
33- this . _timeData = json . parse ( languagePack ) ;
34- } ,
35-
36- // DOJO.WidgetBase -> PostCreate is fired after the properties of the widget are set.
3721 postCreate : function ( ) {
38- // Setup widgets
39- this . _setupWidget ( ) ;
40-
41- // Setup events
22+ this . _timeData = json . parse ( languagePack ) ;
23+
4224 this . _setupEvents ( ) ;
4325
4426 this . attributeList = this . notused ;
4527 } ,
4628
47-
48- /**
49- * What to do when data is loaded?
50- */
51-
5229 update : function ( obj , callback ) {
5330
5431 this . _contextObj = obj ;
@@ -58,45 +35,22 @@ define('formatstring/widget/formatstring', ['dojo/_base/declare', 'mxui/widget/_
5835 callback ( ) ;
5936 } ,
6037
61- uninitialize : function ( ) {
62- // Clean up listeners, helper objects, etc. There is no need to remove listeners added with this.connect / this.subscribe / this.own.
63- } ,
64-
65-
66- /**
67- * Extra setup widget methods.
68- * ======================
69- */
70- _setupWidget : function ( ) {
71-
72- // To be able to just alter one variable in the future we set an internal variable with the domNode that this widget uses.
73- this . _wgtNode = this . domNode ;
74-
75- domClass . add ( this . _wgtNode , 'formatstring_widget' ) ;
76-
77- } ,
78-
79-
80- // Attach events to newly created nodes.
8138 _setupEvents : function ( ) {
82-
8339 if ( this . onclickmf ) {
84- this . connect ( this . _wgtNode , "onclick" , this . execmf ) ;
40+ this . connect ( this . domNode , "onclick" , this . execmf ) ;
8541 }
86-
8742 } ,
8843
89- /**
90- * Interaction widget methods.
91- * ======================
92- */
9344 _loadData : function ( ) {
9445 this . replaceattributes = [ ] ;
9546 var referenceAttributeList = [ ] ,
9647 numberlist = [ ] ,
9748 i = null ,
9849 value = null ;
99-
50+
51+ if ( ! this . _contextObj )
52+ return ;
53+
10054 for ( i = 0 ; i < this . attributeList . length ; i ++ ) {
10155 if ( this . _contextObj . get ( this . attributeList [ i ] . attrs ) !== null ) {
10256 value = this . _fetchAttr ( this . _contextObj , this . attributeList [ i ] . attrs , this . attributeList [ i ] . renderHTML , i ,
@@ -107,14 +61,13 @@ define('formatstring/widget/formatstring', ['dojo/_base/declare', 'mxui/widget/_
10761 value : value
10862 } ) ;
10963 } else {
110- //we'll jump through some hoops with this.
11164 referenceAttributeList . push ( this . attributeList [ i ] ) ;
11265 numberlist . push ( i ) ;
11366 }
11467 }
11568
11669 if ( referenceAttributeList . length > 0 ) {
117- //if we have reference attributes, we need to fetch them. Asynchronicity FTW
70+ //if we have reference attributes, we need to fetch them
11871 this . _fetchReferences ( referenceAttributeList , numberlist ) ;
11972 } else {
12073 this . _buildString ( ) ;
@@ -176,6 +129,10 @@ define('formatstring/widget/formatstring', ['dojo/_base/declare', 'mxui/widget/_
176129 var returnvalue = "" ,
177130 options = { } ,
178131 numberOptions = null ;
132+
133+ // Referenced object might be empty, can't fetch an attr on empty
134+ if ( ! obj )
135+ return emptyReplacement ;
179136
180137 if ( obj . isDate ( attr ) ) {
181138 if ( this . attributeList [ i ] . datePattern !== '' ) {
@@ -227,12 +184,12 @@ define('formatstring/widget/formatstring', ['dojo/_base/declare', 'mxui/widget/_
227184 _renderString : function ( msg ) {
228185 var div = null ;
229186
230- dojo . empty ( this . _wgtNode ) ;
187+ dojo . empty ( this . domNode ) ;
231188 div = dom . div ( {
232189 'class' : 'formatstring'
233190 } ) ;
234191 div . innerHTML = msg ;
235- this . _wgtNode . appendChild ( div ) ;
192+ this . domNode . appendChild ( div ) ;
236193
237194 } ,
238195
0 commit comments