framework7

Open full view…

Dom7.getJSON success or error function?

wetfeet
Wed, 11 Oct 2017 05:44:56 GMT

The parameters are `$$.getJSON(url, data, success, error)`. I saw this example in the [DOM7 Ajax method](http://framework7.io/docs/dom.html#shorthand-methods): ``` $$.getJSON('items.json', function (data) { console.log(data); }); ``` What I see are only the `url` and `success` parameter being filled. The `data` parameter is optional and is left out. How does Dom7 figure out if the callback function (second parameter passed in) is a function for `success` or `error`, since both `success` callback and `error` callback are optional and having 1 callback function is ambiguous?

NikolayKuznetsov
Thu, 12 Oct 2017 16:07:08 GMT

It does check type of parameters. If it is function (call back function) it will passed to ajax function. F See source --- (function () { var methods = ('get post getJSON').split(' '); function createMethod(method) { $[method] = function (url, data, success, error) { return $.ajax({ url: url, method: method === 'post' ? 'POST' : 'GET', data: typeof data === 'function' ? undefined : data, success: typeof data === 'function' ? data : success, error: typeof data === 'function' ? success : error, //niko complete: typeof data === 'function' ? error : complete, //niko dataType: method === 'getJSON' ? 'json' : undefined }); }; } for (var i = 0; i < methods.length; i++) { createMethod(methods[i]); } })(); ---