forked from papertiger8848/modelproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelproxy-client.js
167 lines (147 loc) · 5.09 KB
/
modelproxy-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
KISSY.add( 'modelproxy', function ( S, IO ) {
function Proxy( options ) {
this._opt = options;
}
Proxy.prototype = {
request: function( params, callback, errCallback ) {
IO( {
url: Proxy.base + '/' + this._opt.id,
data: params,
type: this._opt.method,
dataType: this._opt.dataType,
success: callback,
error: errCallback
} );
},
getOptions: function() {
return this._opt;
}
};
Proxy.objects = {};
Proxy.create = function( id ) {
if ( this.objects[ id ] ) {
return this.objects[ id ];
}
var options = this._interfaces[ id ];
if ( !options ) {
throw new Error( 'No such interface id defined: '
+ id + ', please check your interface configuration file' );
}
return this.objects[ id ] = new this( options );
},
Proxy.configBase = function( base ) {
if ( this.base ) return;
this.base = ( base || '' ).replace( /\/$/, '' );
var self = this;
// load interfaces definition.
IO( {
url: this.base + '/$interfaces',
async: false,
type: 'get',
dataType: 'json',
success: function( interfaces ) {
self.config( interfaces );
},
error: function( err ) {
throw err;
}
} );
};
Proxy.config = function( interfaces ) {
this._interfaces = interfaces;
};
Proxy.getInterfaceIdsByPrefix = function( pattern ) {
if ( !pattern ) return [];
var ids = [], map = this._interfaces, len = pattern.length;
for ( var id in map ) {
if ( id.slice( 0, len ) == pattern ) {
ids.push( id );
}
}
return ids;
};
function ModelProxy( profile ) {
if ( !profile ) return;
if ( typeof profile === 'string' ) {
if ( /^(\w+\.)+\*$/.test( profile ) ) {
profile = Proxy
.getInterfaceIdsByPrefix( profile.replace( /\*$/, '' ) );
} else {
profile = [ profile ];
}
}
if ( profile instanceof Array ) {
var prof = {}, methodName;
for ( var i = profile.length - 1; i >= 0; i-- ) {
methodName = profile[ i ];
methodName = methodName
.substring( methodName.lastIndexOf( '.' ) + 1 );
if ( !prof[ methodName ] ) {
prof[ methodName ] = profile[ i ];
} else {
methodName = profile[ i ].replace( /\./g, '_' );
prof[ methodName ] = profile[ i ];
}
}
profile = prof;
}
for ( var method in profile ) {
this[ method ] = ( function( methodName, interfaceId ) {
var proxy = Proxy.create( interfaceId );
return function( params ) {
params = params || {};
if ( !this._queue ) {
this._queue = [];
}
this._queue.push( {
params: params,
proxy: proxy
} );
return this;
};
} )( method, profile[ method ] );
}
}
ModelProxy.prototype = {
done: function( f, ef ) {
if ( typeof f !== 'function' ) return;
if ( !this._queue ) {
f.apply( this );
return;
}
this._sendRequestsParallel( this._queue, f, ef );
this._queue = null;
return this;
},
_sendRequestsParallel: function( queue, callback, errCallback ) {
var args = [], self = this;
var cnt = queue.length;
for ( var i = 0; i < queue.length; i++ ) {
( function( reqObj, k ) {
reqObj.proxy.request( reqObj.params, function( data ) {
args[ k ] = data;
--cnt || callback.apply( self, args );
}, function( err ) {
errCallback = errCallback || self._errCallback;
if ( typeof errCallback === 'function' ) {
errCallback( err );
} else {
console.error( 'Error occured when sending request ='
, reqObj.proxy.getOptions(), '\nCaused by:\n', err );
}
} );
} )( queue[i], i );
}
},
error: function( f ) {
this._errCallback = f;
}
};
ModelProxy.create = function( profile ) {
return new this( profile );
};
ModelProxy.configBase = function( path ) {
Proxy.configBase( path );
};
return ModelProxy;
}, { requires: ['io'] } );