@@ -4,6 +4,12 @@ var SchemaLoader;
44 var STATE_NEW = 'new' ;
55 var STATE_CREATING = 'creating' ;
66 var STATE_DONE = 'done' ;
7+
8+ var KIND_ONE = 'one' ;
9+ var KIND_MANY = 'many' ;
10+
11+ var MODE_NORMAL = 'normal' ;
12+ var MODE_FRAGMENT = 'fragment' ;
713
814 SchemaLoader = Ember . Object . extend ( {
915
@@ -13,17 +19,24 @@ var SchemaLoader;
1319
1420 index : Ember . Map . create ( ) ,
1521
22+ /**
23+ * @method init
24+ */
1625 init : function ( ) {
1726 this . _super ( ) ;
1827 this . target = this . target || this ;
1928 } ,
2029
30+ /**
31+ * @method load
32+ */
2133 load : function ( schema ) {
2234 var loader = this ;
2335
2436 var classes = schema . EmberSchema . classes ;
2537 classes . forEach ( function ( def ) {
2638 def . state = STATE_NEW ;
39+ def . mode = loader . getClassMode ( def ) ;
2740 loader . index . set ( def . name , def ) ;
2841 } ) ;
2942
@@ -32,6 +45,10 @@ var SchemaLoader;
3245 } ) ;
3346 } ,
3447
48+ /**
49+ * @private
50+ * @method getClass
51+ **/
3552 getClass : function ( name ) {
3653 if ( ! this . target [ name ] ) {
3754 var def = this . index . get ( name ) ;
@@ -45,6 +62,10 @@ var SchemaLoader;
4562 return result ;
4663 } ,
4764
65+ /**
66+ * @private
67+ * @method registerClass
68+ **/
4869 registerClass : function ( def ) {
4970 Ember . assert ( "Circular dependency: " + name , def . state != STATE_CREATING ) ;
5071 if ( def . state == STATE_DONE ) return ;
@@ -59,51 +80,106 @@ var SchemaLoader;
5980 this . container . register ( 'model:' + alias , cls ) ;
6081 }
6182
62- // printClass(cls);
83+ printClass ( cls ) ;
6384 def . state = STATE_DONE ;
6485 } ,
6586
87+ /**
88+ * @private
89+ * @method createClass
90+ **/
6691 createClass : function ( def ) {
6792 var loader = this ;
6893 var props = {
6994 '$type' : DS . attr ( 'string' ) ,
7095 } ;
7196 def . props . forEach ( function ( prop ) {
7297 if ( loader . includeProperty ( def , prop ) ) {
73- props [ prop . name ] = loader . createProperty ( prop . type ) ;
98+ props [ prop . name ] = loader . createProperty ( def , prop ) ;
7499 }
75100 } ) ;
76101
77102 var baseType = loader . getBaseClass ( def ) ;
78103 return baseType . extend ( props ) ;
79104 } ,
80105
106+ /**
107+ * @private
108+ * @method getBaseClass
109+ **/
81110 getBaseClass : function ( def ) {
82111 var loader = this ;
83112
84- if ( def . name == 'Spec' ) {
85- return DS . Model ;
86- }
87113 if ( def . superType ) {
88114 var superType = loader . getClass ( def . superType ) ;
89115 Ember . assert ( 'Super type "' + def . superType + '" does not exist' , superType ) ;
90116
91117 return superType ;
92118 }
93- return DS . ModelFragment ;
119+
120+ if ( def . mode == 'fragment' ) {
121+ return DS . ModelFragment ;
122+ }
123+ else {
124+ return DS . Model ;
125+ }
94126 } ,
95127
96- includeProperty : function ( def , prop ) {
128+ /**
129+ * @protected
130+ * @method includeProperty
131+ **/
132+ getClassMode : function ( def ) {
133+ return MODE_NORMAL ;
134+ } ,
135+
136+ /**
137+ * @protected
138+ * @method includeProperty
139+ **/
140+ includeProperty : function ( owner , prop ) {
97141 return prop . name != 'id' ;
98142 } ,
99143
100- createProperty : function ( type ) {
144+ /**
145+ * @private
146+ * @method createProperty
147+ **/
148+ createProperty : function ( owner , prop ) {
149+ if ( prop . type . kind == 'attr' ) {
150+ return this . createAttributeProperty ( owner , prop ) ;
151+ }
152+ var typeDef = this . index . get ( prop . type . name ) ;
153+ if ( typeDef . mode == MODE_FRAGMENT ) {
154+ return this . createFragmentProperty ( owner , prop ) ;
155+ }
156+ else if ( typeDef . mode == MODE_NORMAL ) {
157+ return this . createDefaultProperty ( owner , prop ) ;
158+ }
159+ else {
160+ throw 'unsupported: ' + typeDef . mode ;
161+ }
162+ } ,
163+
164+ createAttributeProperty : function ( owner , prop ) {
165+ return DS . attr ( prop . type . name ) ;
166+ } ,
167+
168+ createDefaultProperty : function ( owner , prop ) {
169+ var opts = { } ; // polymorphic: true, typeKey: '$type' };
170+ switch ( prop . type . kind ) {
171+ case KIND_ONE : return DS . belongsTo ( prop . type . name , opts ) ;
172+ case KIND_MANY : return DS . hasMany ( prop . type . name , opts ) ;
173+ default : throw 'unsupported: ' + prop . type . kind ;
174+ }
175+ } ,
176+
177+ createFragmentProperty : function ( owner , prop ) {
101178 var opts = { polymorphic : true , typeKey : '$type' } ;
102- switch ( type . kind ) {
103- case 'attr' : return DS . attr ( type . name ) ;
104- case 'one' : return DS . hasOneFragment ( type . name , opts ) ;
105- case 'many' : return DS . hasManyFragments ( type . name , opts ) ;
106- default : throw 'unsupported: ' + type . kind ;
179+ switch ( prop . type . kind ) {
180+ case KIND_ONE : return DS . hasOneFragment ( prop . type . name , opts ) ;
181+ case KIND_MANY : return DS . hasManyFragments ( prop . type . name , opts ) ;
182+ default : throw 'unsupported: ' + prop . type . kind ;
107183 }
108184 } ,
109185 } ) ;
0 commit comments