2121
2222/**
2323 * @module FrameworkBuilders
24- * @version 2.7 .0
24+ * @version 2.8 .0
2525 */
2626
2727'use strict' ;
@@ -862,6 +862,9 @@ SchemaBuilderEntity.prototype.save = function(model, options, callback, controll
862862 return ;
863863 }
864864
865+ if ( model && ! controller && model . $$controller )
866+ controller = model . $$controller ;
867+
865868 var builder = new ErrorBuilder ( ) ;
866869 self . resourceName && builder . setResource ( self . resourceName ) ;
867870 self . resourcePrefix && builder . setPrefix ( self . resourcePrefix ) ;
@@ -1278,11 +1281,12 @@ SchemaBuilderEntity.prototype.default = function() {
12781281 item [ property ] = [ ] ;
12791282 } else {
12801283 var tmp = this . find ( type . raw ) ;
1281- if ( ! tmp ) {
1284+ if ( tmp ) {
1285+ item [ property ] = tmp . default ( ) ;
1286+ } else {
12821287 F . error ( new Error ( 'Schema: "' + property + '.' + type . raw + '" not found in "' + this . parent . name + '".' ) ) ;
12831288 item [ property ] = null ;
1284- } else
1285- item [ property ] = tmp . default ( ) ;
1289+ }
12861290 }
12871291 break ;
12881292 // enum + keyvalue
@@ -1794,6 +1798,9 @@ SchemaBuilderEntity.prototype.hook = function(name, model, options, callback, sk
17941798 return self ;
17951799 }
17961800
1801+ if ( model && ! controller && model . $$controller )
1802+ controller = model . $$controller ;
1803+
17971804 var $type = 'hook' ;
17981805
17991806 if ( skip === true ) {
@@ -1924,6 +1931,9 @@ SchemaBuilderEntity.prototype.$execute = function(type, name, model, options, ca
19241931 return self ;
19251932 }
19261933
1934+ if ( model && ! controller && model . $$controller )
1935+ controller = model . $$controller ;
1936+
19271937 if ( skip === true ) {
19281938 var builder = new ErrorBuilder ( ) ;
19291939 self . resourceName && builder . setResource ( self . resourceName ) ;
@@ -2056,6 +2066,9 @@ SchemaBuilderEntity.prototype.operation = function(name, model, options, callbac
20562066 self . resourceName && builder . setResource ( self . resourceName ) ;
20572067 self . resourcePrefix && builder . setPrefix ( self . resourcePrefix ) ;
20582068
2069+ if ( model && ! controller && model . $$controller )
2070+ controller = model . $$controller ;
2071+
20592072 if ( ! isGenerator ( self , 'operation.' + name , operation ) ) {
20602073 if ( operation . $newversion ) {
20612074 operation . call ( self , new SchemaOptions ( builder , model , options , function ( res ) {
@@ -2265,7 +2278,7 @@ SchemaInstance.prototype.$push = function(type, name, helper, first) {
22652278 var self = this ;
22662279 var fn ;
22672280
2268- if ( type === 'save' || type === 'remove' ) {
2281+ if ( type === 'save' ) {
22692282
22702283 helper = name ;
22712284 name = undefined ;
@@ -2282,7 +2295,7 @@ SchemaInstance.prototype.$push = function(type, name, helper, first) {
22822295 } , self . $$controller ) ;
22832296 } ;
22842297
2285- } else if ( type === 'query' || type === 'get' || type === 'read' ) {
2298+ } else if ( type === 'query' || type === 'get' || type === 'read' || type === 'remove' ) {
22862299
22872300 helper = name ;
22882301 name = undefined ;
@@ -2300,7 +2313,6 @@ SchemaInstance.prototype.$push = function(type, name, helper, first) {
23002313 } ;
23012314
23022315 } else {
2303-
23042316 fn = function ( next ) {
23052317 self . $$schema [ type ] ( name , self , helper , function ( err , result ) {
23062318 self . $$result && self . $$result . push ( err ? null : copy ( result ) ) ;
@@ -2312,7 +2324,6 @@ SchemaInstance.prototype.$push = function(type, name, helper, first) {
23122324 self . $$callback = null ;
23132325 } , self . $$controller ) ;
23142326 } ;
2315-
23162327 }
23172328
23182329 if ( first )
@@ -2339,7 +2350,7 @@ SchemaInstance.prototype.$exec = function(name, helper, callback) {
23392350 var workflow = F . workflows [ key + '#' + name ] || F . workflows [ name ] ;
23402351
23412352 if ( workflow )
2342- workflow ( this , helper || EMPTYOBJECT , callback || NOOP ) ;
2353+ workflow ( this , helper , callback || NOOP ) ;
23432354 else
23442355 callback && callback ( new ErrorBuilder ( ) . push ( 'Workflow "' + name + '" not found in workflows.' ) ) ;
23452356
@@ -2360,12 +2371,10 @@ SchemaInstance.prototype.$save = function(helper, callback) {
23602371} ;
23612372
23622373SchemaInstance . prototype . $query = function ( helper , callback ) {
2363-
23642374 if ( this . $$can && this . $$async )
23652375 this . $push ( 'query' , helper ) ;
23662376 else
23672377 this . $$schema . query ( this , helper , callback , this . $$controller ) ;
2368-
23692378 return this ;
23702379} ;
23712380
@@ -2379,7 +2388,7 @@ SchemaInstance.prototype.$read = SchemaInstance.prototype.$get = function(helper
23792388 return this ;
23802389} ;
23812390
2382- SchemaInstance . prototype . $remove = function ( helper , callback ) {
2391+ SchemaInstance . prototype . $delete = SchemaInstance . prototype . $ remove = function ( helper , callback ) {
23832392
23842393 if ( this . $$can && this . $$async )
23852394 this . $push ( 'remove' , helper ) ;
@@ -2945,15 +2954,18 @@ ErrorBuilder.prototype._transform = function(name) {
29452954 return this . items ;
29462955} ;
29472956
2948- ErrorBuilder . prototype . output = function ( ) {
2957+ ErrorBuilder . prototype . output = function ( isResponse ) {
2958+
29492959 if ( ! this . transformName )
2950- return this . json ( ) ;
2960+ return isResponse ? this . json ( ) : this . items ;
2961+
29512962 var current = transforms [ 'error' ] [ this . transformName ] ;
29522963 if ( current ) {
29532964 this . prepare ( ) ;
2954- return current . call ( this ) ;
2965+ return current . call ( this , isResponse ) ;
29552966 }
2956- return this . json ( ) ;
2967+
2968+ return isResponse ? this . json ( ) : this . items ;
29572969} ;
29582970
29592971/**
@@ -3847,16 +3859,6 @@ RESTBuilder.prototype.stream = function(callback) {
38473859 return U . download ( self . $url , flags , self . $data , callback , self . $cookies , self . $headers , undefined , self . $timeout ) ;
38483860} ;
38493861
3850- RESTBuilder . prototype . file = function ( name , filename ) {
3851- var self = this ;
3852- var obj = { name : name , filename : filename } ;
3853- if ( self . $files )
3854- self . $files . push ( obj ) ;
3855- else
3856- self . $files = [ obj ] ;
3857- return self ;
3858- } ;
3859-
38603862RESTBuilder . prototype . exec = function ( callback ) {
38613863
38623864 if ( ! callback )
@@ -3908,7 +3910,20 @@ RESTBuilder.prototype.exec = function(callback) {
39083910 var type = err ? '' : headers [ 'content-type' ] || '' ;
39093911 var output = new RESTBuilderResponse ( ) ;
39103912
3911- output . value = type . indexOf ( '/xml' ) === - 1 ? response . isJSON ( ) ? JSON . parse ( response , jsonparser ) : F . onParseQuery ( response ) : response . parseXML ( ) ;
3913+ switch ( type . toLowerCase ( ) ) {
3914+ case 'text/xml' :
3915+ output . value = response . parseXML ( ) ;
3916+ break ;
3917+ case 'application/x-www-form-urlencoded' :
3918+ output . value = F . onParseQuery ( response ) ;
3919+ break ;
3920+ case 'application/json' :
3921+ output . value = response . parseJSON ( true ) ;
3922+ break ;
3923+ default :
3924+ output . value = response . isJSON ( ) ? response . parseJSON ( true ) : null ;
3925+ break ;
3926+ }
39123927
39133928 if ( output . value == null )
39143929 output . value = EMPTYOBJECT ;
@@ -4025,10 +4040,6 @@ global.OPERATION = function(name, value, callback, param) {
40254040 }
40264041} ;
40274042
4028- function jsonparser ( key , value ) {
4029- return typeof ( value ) === 'string' && value . isJSONDate ( ) ? new Date ( value ) : value ;
4030- }
4031-
40324043// ======================================================
40334044// EXPORTS
40344045// ======================================================
@@ -4041,6 +4052,7 @@ exports.Page = Page;
40414052exports . UrlBuilder = UrlBuilder ;
40424053exports . TransformBuilder = TransformBuilder ;
40434054exports . SchemaOptions = SchemaOptions ;
4055+ exports . RESTBuilderResponse = RESTBuilderResponse ;
40444056global . RESTBuilder = RESTBuilder ;
40454057global . RESTBuilderResponse = RESTBuilderResponse ;
40464058global . ErrorBuilder = ErrorBuilder ;
0 commit comments