UNPKG

boilerplate.js

Version:

Development Tools

86 lines (58 loc) 2 kB
components('laravel.view', function (view, selector = null, data = null, callback = null) { let args = [...arguments]; let obj = { 'view': null, 'selector': null, 'data': null, 'callback': null, }; if (args.length > 1) { let skip = { 'view': false, 'selector': false, 'data': false, 'callback': false, }; args.forEach(arg => { if (!skip.view && typeof arg === 'string' && args[0] === arg) { skip.view = true; return obj.view = arg; } if (!skip.selector && typeof arg === 'string') { skip.selector = true; return obj.selector = arg; } if (!skip.data && typeof arg === 'object') { skip.data = true; return obj.data = arg; } if (!skip.callback && typeof arg === 'function') { skip.callback = true; return obj.callback = arg; } }); } else { let data = view; obj.view = data.view; if (data.selector) obj.selector = data.target; if (data.callback) obj.callback = data.callback; if (data.data) obj.data = data.data; } log(obj); $.get('/view/' + obj.view, obj.data, data => { if (obj.selector) loadSelector(data); if (obj.callback) obj.callback(data); }); function loadSelector(data) { let elements = document.querySelectorAll(obj.selector); elements.forEach(element => { httpRequest.load(data, element, () => { try { element.onload.call(); } catch (error) { null; } }); }); } });