Toggle navigation
Run LIVE DEMO
Show CODE
LIVE DEMO PAGE
Updated: 19.06.18 11:02
Basic example
<html> <head> <title>Login dialog smartapp2.js example</title> <!-- jQuery --> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <!-- Bootstrap --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <!-- Smartapp --> <script src="https://smartapp.ikx.no/downloads/jquery.smartapp2.js"></script> </head> <body> <div id="App"></div> <div id="Welcome"> See the code for code example of a login implementation </div> <div id="Login"> Login dialog </div> <script> // Setup your app: var $app = $("#App").smartapp2(); // Add views $app.createView("Welcome", $("#Welcome"), function() { }); // Add the login dialog $app.createDialog("Login", $("#Login"), function() { }); // Overwrite API functions // Keep old references to API get and post methods var apiGet = this.APIGet; var apiPost = this.APIPost; var apiDownload = this.APIDownload; // Overwrite them with your own functions: $app.AddFailedLoginCall = function (call) { if ($app.QueuedFailedLoginCalls == null || $app.QueuedFailedLoginCalls.length == 0) { $app.QueuedFailedLoginCalls = []; // Open the LOGIN dialog: $app.openDialog("Login", { show: true, keyboard: false, backdrop: "static" }).done(function ($v) { $app.ReloginView = $v; }); } $app.QueuedFailedLoginCalls.push(call); }; $app.ForwardDeferredResult = function (call, dfd, controller, method, data, opts) { if (opts == null) opts = {}; if (data == null) data = {}; controller = controller.replace(new RegExp("/", "g"), ""); var ajaxcall = null; function nestedCall() { data.auth = $app.token; document.cookie = "auth=" + $app.token; ajaxcall = call(controller, method, data, opts); ajaxcall.then(function (resultObject, textStatus, jqXhr) { ajaxcall = null; dfd.resolveWith(this, [resultObject, textStatus, jqXhr]); }, function (xhr, ajaxOptions, thrownError) { ajaxcall = null; if (xhr.status == 401 && !opts.nologin) // <--- Handle 401 login response $app.AddFailedLoginCall(nestedCall); // <--- Handle 401 login response else dfd.rejectWith(this, [xhr, ajaxOptions, thrownError]); }); }; nestedCall(); var abortFunc = function () { if (ajaxcall != null) ajaxcall.abort(); ajaxcall = null; }; return abortFunc; }; $app.APIGet = function (controller, method, data, opts) { var dfd = $.Deferred(); var abortFunc = $app.ForwardDeferredResult(apiGet, dfd, controller, method, data, opts); var promise = dfd.promise(); promise.abort = abortFunc; return promise; }; $app.APIPost = function (controller, method, data, opts) { var dfd = $.Deferred(); var abortFunc = $app.ForwardDeferredResult(apiPost, dfd, controller, method, data, opts); var promise = dfd.promise(); promise.abort = abortFunc; return promise; }; $app.APIDownload = function (controller, method, data, opts) { if (!data) data = {}; data.auth = ""; document.cookie = "auth=" + $app.token; return apiDownload(controller.replace(new RegExp("/", "g"), ""), method, data, opts); }; $app.run(); </script> </body> </html>