File tree 5 files changed +123
-19
lines changed
grails-app/assets/javascripts
5 files changed +123
-19
lines changed Original file line number Diff line number Diff line change 1165
1165
var dataLoader = new ecodata . forms . dataLoader ( target . context , target . config ) ;
1166
1166
var dataLoaderConfig = target . get ( 'computed' ) ;
1167
1167
if ( ! dataLoaderConfig ) {
1168
- throw "This extender can only be used with the metadata extender" ;
1168
+ throw "This extender can only be used with the metadata extender and expects a computed property to be defined " ;
1169
1169
}
1170
1170
var dependencyTracker = ko . computed ( function ( ) {
1171
1171
return dataLoader . prepop ( dataLoaderConfig ) . done ( function ( data ) {
1172
1172
target ( data ) ;
1173
1173
} ) ;
1174
- } ) ;
1175
- //dependencyTracker.subscribe(function() { console.log("bananas")})
1174
+ } ) ; // This is a computed rather than a pureComputed as it has a side effect.
1176
1175
return target ;
1177
1176
}
1178
1177
Original file line number Diff line number Diff line change @@ -860,7 +860,6 @@ function orEmptyArray(v) {
860
860
}
861
861
}
862
862
else if ( metadata . constraints . type == 'pre-populated' ) {
863
- console . log ( "******************** Encountered pre-pop constraints for " + self . getName ( ) + "***************************************" )
864
863
var defaultConstraints = metadata . constraints . defaults || [ ] ;
865
864
var constraintsObservable = ko . observableArray ( defaultConstraints ) ;
866
865
if ( ! includeExcludeDefined ) {
@@ -915,14 +914,14 @@ function orEmptyArray(v) {
915
914
self . displayOptions = metadata . displayOptions ;
916
915
}
917
916
self . load = function ( data ) {
918
- console . log ( "Loading data for " + self . getName ( ) + " : " + data )
919
- self ( data ) ;
920
917
if ( constraintsInititaliser ) {
921
918
constraintsInititaliser . always ( function ( ) {
922
- console . log ( "Re-Loading data for " + self . getName ( ) + " : " + data )
923
919
self ( data ) ;
924
920
} )
925
921
}
922
+ else {
923
+ self ( data ) ;
924
+ }
926
925
927
926
}
928
927
} ;
Original file line number Diff line number Diff line change 91
91
return ( typeof root . modelAsJSON === 'function' ) ? root . modelAsJSON ( ) : ko . toJSON ( root ) ;
92
92
} ;
93
93
var _initialState = ko . observable ( getRepresentation ( ) ) ;
94
- console . log ( "****************** Initial state ******************" )
95
- console . log ( _initialState ( ) )
96
94
97
95
result . isDirty = ko . pureComputed ( function ( ) {
98
96
99
97
var dirty = _isInitiallyDirty ( ) || _initialState ( ) !== getRepresentation ( ) ;
100
- if ( dirty ) {
101
- console . log ( "******************* new state *******************" )
102
- console . log ( getRepresentation ( ) )
103
- }
104
98
return dirty ;
105
99
} ) ;
106
100
if ( rateLimit ) {
109
103
110
104
result . reset = function ( ) {
111
105
_initialState ( getRepresentation ( ) ) ;
112
- console . log ( "****************** Reset initial state ******************" )
113
- console . log ( _initialState ( ) )
114
106
_isInitiallyDirty ( false ) ;
115
107
} ;
116
108
146
138
147
139
//just for subscriptions
148
140
getRepresentation ( ) ;
149
- console . log ( "****************** Initial state ******************" )
150
- console . log ( getRepresentation ( ) )
141
+
151
142
//next time return true and avoid ko.toJS
152
143
_initialized ( true ) ;
153
144
154
145
//on initialization this flag is not dirty
155
146
return false ;
156
147
}
157
- console . log ( "****************** New state ******************" )
158
- console . log ( getRepresentation ( ) )
148
+
159
149
//on subsequent changes, flag is now dirty
160
150
return true ;
161
151
} ) ;
Original file line number Diff line number Diff line change
1
+ describe ( "dataLoader extender spec" , function ( ) {
2
+ var turf ;
3
+ beforeEach ( function ( ) {
4
+ jasmine . clock ( ) . install ( ) ;
5
+ } ) ;
6
+ afterEach ( function ( ) {
7
+ jasmine . clock ( ) . uninstall ( ) ;
8
+ } ) ;
9
+
10
+
11
+ it ( "should augment an observable with geojson type methods" , function ( ) {
12
+ var metadata = {
13
+ name :'item' ,
14
+ dataType :'text' ,
15
+ computed : {
16
+ source : {
17
+ "context-path" : "test"
18
+ }
19
+ }
20
+ } ;
21
+
22
+ var context = {
23
+ test :"test-value"
24
+ } ;
25
+ var config = { } ;
26
+
27
+ var dataItem = ko . observable ( ) . extend ( { metadata :{ metadata :metadata , context :context , config :config } } ) ;
28
+ var withDataLoader = dataItem . extend ( { dataLoader :true } ) ;
29
+ jasmine . clock ( ) . tick ( ) ;
30
+ expect ( withDataLoader ( ) ) . toEqual ( "test-value" ) ;
31
+
32
+ } ) ;
33
+
34
+
35
+ } ) ;
Original file line number Diff line number Diff line change @@ -75,4 +75,85 @@ describe("Pre-population Spec", function () {
75
75
expect ( result ) . toEqual ( { item1 :"test" } ) ;
76
76
} ) ;
77
77
} ) ;
78
+
79
+ it ( "Should should support computing the pre-pop params via an expression" , function ( ) {
80
+ var context = {
81
+ data : {
82
+ item1 : '1' ,
83
+ item2 : '2'
84
+ }
85
+ } ;
86
+
87
+ var prepopConfig = {
88
+ source : {
89
+ url :'test' ,
90
+ params : [ {
91
+ "type" :"computed" ,
92
+ "expression" :"2+2" ,
93
+ name :"p1" ,
94
+ } ]
95
+ } ,
96
+ mapping : [ ]
97
+ } ;
98
+
99
+ var config = {
100
+ prepopUrlPrefix :'/'
101
+ } ;
102
+
103
+ var url ;
104
+ var params ;
105
+ spyOn ( $ , 'ajax' ) . and . callFake ( function ( p1 , p2 ) {
106
+ url = p1 ;
107
+ params = p2 ;
108
+ return $ . Deferred ( ) . resolve ( context ) . promise ( ) ;
109
+ } ) ;
110
+
111
+ var dataLoader = ecodata . forms . dataLoader ( context , config ) ;
112
+ dataLoader . getPrepopData ( prepopConfig ) . done ( function ( result ) {
113
+ expect ( url ) . toEqual ( config . prepopUrlPrefix + prepopConfig . source . url ) ;
114
+ expect ( params . data [ 0 ] ) . toEqual ( { name :"p1" , value :4 } ) ;
115
+ expect ( params . dataType ) . toEqual ( 'json' ) ;
116
+
117
+ expect ( result ) . toEqual ( context ) ;
118
+ } ) ;
119
+ } ) ;
120
+
121
+ // This prevents making calls that will return errors
122
+ it ( "Should not make a remote call if required params are undefined" , function ( ) {
123
+ var context = {
124
+ data : {
125
+ item1 : '1' ,
126
+ item2 : '2'
127
+ }
128
+ } ;
129
+
130
+ var prepopConfig = {
131
+ source : {
132
+ url :'test' ,
133
+ params : [ {
134
+ "type" :"computed" ,
135
+ "expression" :"x" ,
136
+ name :"p1" ,
137
+ required :true
138
+ } ]
139
+ } ,
140
+ mapping : [ ]
141
+ } ;
142
+
143
+ var config = {
144
+ prepopUrlPrefix :'/'
145
+ } ;
146
+
147
+ var called = false ;
148
+
149
+ spyOn ( $ , 'ajax' ) . and . callFake ( function ( p1 , p2 ) {
150
+ called = true ;
151
+ } ) ;
152
+
153
+ var dataLoader = ecodata . forms . dataLoader ( context , config ) ;
154
+ dataLoader . getPrepopData ( prepopConfig ) . done ( function ( result ) {
155
+ } ) ;
156
+
157
+ expect ( called ) . toEqual ( false ) ;
158
+ } ) ;
78
159
} ) ;
You can’t perform that action at this time.
0 commit comments