@@ -4,7 +4,7 @@ define(function (require) {
4
4
GenericUriModel = require ( 'entities/api/call/genericUri.model' ) ,
5
5
SampleUriModel = require ( 'entities/api/call/sampleUri.model' ) ,
6
6
GetUriModel = require ( 'entities/api/call/tryUri.model' ) ,
7
- HasNestedModel = require ( 'lib/hasNestedModel.mixin ' ) ,
7
+ HasNestedModel = require ( 'lib/hasNestedModel' ) ,
8
8
appChannel = require ( 'app.channel' ) ,
9
9
ApiModel ;
10
10
@@ -20,59 +20,64 @@ define(function (require) {
20
20
tryUri : null
21
21
} ,
22
22
23
- constructor : function ( attrs , options ) {
24
- var apiAttributes = _ . pick ( attrs , [ 'serviceKey' , 'endpointKey' ] ) ;
25
-
26
- _ . extend ( attrs , {
27
- genericOutput : new GenericOutputModel ( apiAttributes ) ,
28
- genericUri : new GenericUriModel ( apiAttributes ) ,
29
- sampleUri : new SampleUriModel ( apiAttributes ) ,
30
- tryUri : new GetUriModel ( )
31
- } ) ;
23
+ computed : {
24
+ apiName : {
25
+ depends : [ 'serviceKey' , 'endpointKey' ] ,
26
+ get : function ( ) {
27
+ var serviceName = appChannel . reqres . request (
28
+ 'lookup:serviceName' , this . get ( 'serviceKey' ) ) ,
29
+ endpointName = appChannel . reqres . request (
30
+ 'lookup:endpointName' , this . get ( 'serviceKey' ) , this . get ( 'endpointKey' ) ) ,
31
+ apiName = serviceName + ' ' + endpointName ;
32
+ return apiName ;
33
+ }
34
+ }
35
+ } ,
32
36
37
+ constructor : function ( ) {
38
+ // Attach nested model functionality
33
39
new HasNestedModel ( this ) ;
34
40
35
- ApiModel . __super__ . constructor . call ( this , attrs , options ) ;
41
+ // Attach computed field functionality
42
+ new Backbone . ComputedFields ( this ) ;
43
+
44
+ ApiModel . __super__ . constructor . apply ( this , arguments ) ;
36
45
} ,
37
46
38
47
initialize : function ( ) {
39
- var self = this ;
48
+ var self = this ,
49
+ apiAttributes = {
50
+ serviceKey : this . get ( 'serviceKey' ) ,
51
+ endpointKey : this . get ( 'endpointKey' )
52
+ } ;
53
+
54
+ // Initialize submodels
55
+ this . set ( {
56
+ genericOutput : new GenericOutputModel ( apiAttributes ) ,
57
+ genericUri : new GenericUriModel ( apiAttributes ) ,
58
+ sampleUri : new SampleUriModel ( apiAttributes ) ,
59
+ tryUri : new GetUriModel ( )
60
+ } ) ;
40
61
41
62
this . on ( 'change' , function ( ) {
42
- var serviceKey ,
43
- endpointKey ;
44
-
63
+ var apiAttributes ;
64
+
65
+ // When serviceKey or endpointKey are changed, pass them down to submodels
45
66
if ( self . hasChanged ( 'serviceKey' ) || self . hasChanged ( 'endpointKey' ) ) {
46
- serviceKey = self . get ( 'serviceKey' ) ;
47
- endpointKey = self . get ( 'endpointKey' ) ;
67
+ apiAttributes = {
68
+ serviceKey : self . get ( 'serviceKey' ) ,
69
+ endpointKey : self . get ( 'endpointKey' )
70
+ } ;
48
71
49
72
_ . invoke ( [
50
73
self . get ( 'genericOutput' ) ,
51
74
self . get ( 'genericUri' ) ,
52
75
self . get ( 'sampleUri' )
53
- ] , 'set' , { serviceKey : serviceKey , endpointKey : endpointKey } ) ;
54
-
55
- self . trigger ( 'change:apiName' , self , self . get ( 'apiName' ) ) ;
76
+ ] , 'set' , apiAttributes ) ;
56
77
}
57
78
} ) ;
58
79
} ,
59
80
60
- get : function ( key ) {
61
- var serviceName ,
62
- endpointName ,
63
- apiName ;
64
-
65
- if ( key === 'apiName' ) {
66
- serviceName = appChannel . reqres . request ( 'lookup:serviceName' , this . get ( 'serviceKey' ) ) ;
67
- endpointName = appChannel . reqres . request ( 'lookup:endpointName' ,
68
- this . get ( 'serviceKey' ) , this . get ( 'endpointKey' ) ) ;
69
- apiName = serviceName + ' ' + endpointName ;
70
- return apiName ;
71
- } else {
72
- return ApiModel . __super__ . get . apply ( this , arguments ) ;
73
- }
74
- } ,
75
-
76
81
clear : function ( ) {
77
82
this . set ( {
78
83
apiName : null ,
@@ -101,6 +106,10 @@ define(function (require) {
101
106
102
107
fetchTryUri : function ( options ) {
103
108
this . get ( 'tryUri' ) . fetch ( { data : $ . param ( options ) } ) ;
109
+ } ,
110
+
111
+ toJSON : function ( ) {
112
+ return _ . clone ( this . attributes ) ;
104
113
}
105
114
} ) ;
106
115
0 commit comments