6 _call: function(req, cb)
11 for (var i = 0; i < req.length; i++)
12 q += '%s%s.%s'.format(
18 q += '/%s.%s'.format(req.params[1], req.params[2]);
20 return $.ajax('/ubus' + q, {
22 contentType: 'application/json',
23 data: JSON.stringify(req),
26 timeout: L.globals.timeout,
31 _list_cb: function(msg)
33 var list = msg.result;
35 /* verify message frame */
36 if (typeof(msg) != 'object' || msg.jsonrpc != '2.0' || !msg.id || !$.isArray(list))
39 return $.Deferred().resolveWith(this, [ list ]);
42 _call_cb: function(msg)
45 var type = Object.prototype.toString;
46 var reqs = this._rpc_req;
54 for (var i = 0; i < msg.length; i++)
56 /* fetch related request info */
57 var req = L.rpc._requests[reqs[i].id];
58 if (typeof(req) != 'object')
59 throw 'No related request for JSON response';
61 /* fetch response attribute and verify returned type */
64 /* verify message frame */
65 if (typeof(msg[i]) == 'object' && msg[i].jsonrpc == '2.0')
66 if ($.isArray(msg[i].result) && msg[i].result[0] == 0)
67 ret = (msg[i].result.length > 1) ? msg[i].result[1] : msg[i].result[0];
71 for (var key in req.expect)
73 if (typeof(ret) != 'undefined' && key != '')
76 if (typeof(ret) == 'undefined' || type.call(ret) != type.call(req.expect[key]))
77 ret = req.expect[key];
84 if (typeof(req.filter) == 'function')
87 req.priv[1] = req.params;
88 ret = req.filter.apply(L.rpc, req.priv);
91 /* store response data */
92 if (typeof(req.index) == 'number')
93 data[req.index] = ret;
97 /* delete request object */
98 delete L.rpc._requests[reqs[i].id];
101 return $.Deferred().resolveWith(this, [ data ]);
107 for (var i = 0; i < arguments.length; i++)
108 params[i] = arguments[i];
114 params: (params.length > 0) ? params : undefined
117 return this._call(msg, this._list_cb);
122 if (!$.isArray(this._batch))
128 if (!$.isArray(this._batch))
129 return L.deferrable([ ]);
131 var req = this._batch;
135 return this._call(req, this._call_cb);
138 declare: function(options)
143 /* build parameter object */
146 if ($.isArray(options.params))
147 for (p_off = 0; p_off < options.params.length; p_off++)
148 params[options.params[p_off]] = arguments[p_off];
150 /* all remaining arguments are private args */
151 var priv = [ undefined, undefined ];
152 for (; p_off < arguments.length; p_off++)
153 priv.push(arguments[p_off]);
155 /* store request info */
156 var req = _rpc._requests[_rpc._id] = {
157 expect: options.expect,
158 filter: options.filter,
163 /* build message object */
176 /* when a batch is in progress then store index in request data
177 * and push message object onto the stack */
178 if ($.isArray(_rpc._batch))
180 req.index = _rpc._batch.push(msg) - 1;
181 return L.deferrable(msg);
185 return _rpc._call(msg, _rpc._call_cb);