For example, when two Klips require the user to login in order to access data, one can be designated the 'master' Klip responsible for the login and logout procedures, with the other Klip designated as the 'slave'. When the user logs in through the master Klip, the slave Klip is notified of the login status and reacts accordingly. On the other hand, when the user logs in through the slave Klip, it lets the master Klip know this attempt is being made, so that the master Klip can go ahead with the login process. The master Klip will then notify the slave Klip of the login status, and the slave Klip reacts accordingly.
The three functions available in the Engines.DataPool object can be used to execute the above process:
- setPool() can be used to store the user's username, password, and login status in the data pool
- getPool() can be used to retrieve the stored username, password, and login status from the data pool
- notifyPool() can be used by the master Klip to monitor for login requests from the slave Klip, or by the slave Klip to monitor for login commands from the master Klip
Example:
Here is what the entire process might look like:
- User presses the "Log In" button in a slave Klip.
- Slave Klip sends a login request by setting "LOGIN" to the data pool "Login_Example_loginReq"
that the master Klip is monitoring:
Engines.DataPool.setPool("Login_Example_loginReq", "LOGIN"); - Master Klip detects the change in the "Login_Example_loginReq" pool:
Engines.DataPool.notifyPool("Login_Example_loginReq", logInOrOut);
and executes the login process by interacting with the server. - If login is successful, the master Klip puts the user's information in the data pool:
Engines.DataPool.setPool("Login_Example_username", c_username.value);
Engines.DataPool.setPool("Login_Example_password", c_password.value);
and sends a notification to the slave Klip by setting "LOGIN" to the data pool "Login_Example_loginNotify" that the slave Klip is listening for:
Engines.DataPool.setPool("Login_Example_loginNotify", "LOGIN"); - Slave Klip gets the notification through:
Engines.DataPool.notifyPool("Login_Example_loginNotify", logInOrOut);
and executes data displaying tasks.
<klip>
<identity>
<title>
API - Engines.DataPool (Master)
</title>
</identity>
<locations>
<icon>
http://www.klipfolio.com/static/klips/klipfolio/sample_icon.png
</icon>
<banner>
http://www.klipfolio.com/static/klips/klipfolio/sample_banner.png
</banner>
</locations>
<style>
item
{
type: item;
}
message
{
itemcol: 1;
noterow: 1;
emphasis: strong;
notelabel: false;
label: "Message";
}
</style>
<klipscript>
<![CDATA[
// User interface components
var c_server;
var c_username;
var c_password;
// Global to keep track of whether the Action Window is open.
// If the Action Window is open, this Klip will ignore further
// login requests from the slave Klips.
var gWindowOpen = false;
function onLoad ()
{
// Reset data pool values and Items
Engines.DataPool.setPool("Login_Example_username", "");
Engines.DataPool.setPool("Login_Example_password", "");
Engines.DataPool.setPool("Login_Example_loginReq", "");
Engines.DataPool.setPool("Login_Example_loginNotify", "");
Items.clear(true);
// Create the Login Action Window
var stack = Actions.Window.addStack("Login");
stack.title = "Login Example: Sign In";
page = stack.addPage("sign_in");
page.addText("Sign in with your username and password.");
page.addText("Username:");
c_username = page.addTextField("");
page.addText("Password:");
c_password = page.addTextField("", true);
page.next = "Sign In";
page.onNext = fn_login_next;
page.back = ""; // Hidden
Actions.Window.onDone = actionClose;
// Notifications
// The Master Klip listens to the "Login_Example_loginReq" data pool
// for login or logout requests from the Slave Klips.
Engines.DataPool.notifyPool("Login_Example_loginReq", logInOrOut);
// Action Button
Actions.Button.show("Login");
Actions.Button.onClick = buttonClick;
}
function buttonClick()
{
// Launch the login wizard
launchLoginWizard();
}
function actionClose(page)
{
// Clear the action window of error messages
page.setError("");
gWindowOpen = false;
}
function fn_login_next(page)
{
if((c_username.value.length > 0) && (c_password.value.length > 0))
{
// Check the username and password the user entered
var success = verifyLogin(c_username.value, c_password.value);
if(success == 1)
{
// If the username and password are valid, put them in the shared data
// pool, then tell the slave Klips to login via the "Login_Example_loginNotify"
// data pool.
Engines.DataPool.setPool("Login_Example_username", c_username.value);
Engines.DataPool.setPool("Login_Example_password", c_password.value);
Engines.DataPool.setPool("Login_Example_loginNotify", "LOGIN");
Actions.Window.hide();
page.setError("");
gWindowOpen = false;
// Create a global item action to allow the user to logout
Items.globalactions = ["Logout", logout];
Engines.Data.process("<item><message>Login Successful!</message></item>");
return true;
}
else
{
page.setError("There was an error during login. Please try again.");
Prefs.setPref("username", "");
Prefs.setPref("password", "");
return false;
}
}
else
{
page.setError("Please enter your username and password");
Prefs.setPref("username", "");
Prefs.setPref("password", "");
return false;
}
}
function verifyLogin(username, password)
{
// Verify the user's login and password
// For the purposes of this example, we will assume that all non-empty
// usernames and passwords are valid.
return true;
}
function logout()
{
// This function can be called either via the Master Klip's global item action,
// or in response to a logout request from a Slave Klip (via the logInOrOut() function).
Items.clear(true);
Items.globalactions = [];
Actions.Button.show ("Login");
// Tell the slave Klips to logout via the "Login_Example_loginNotify"
// data pool, and clear the user's information from the data pool.
Engines.DataPool.setPool("Login_Example_loginNotify", "LOGOUT");
Engines.DataPool.setPool("Login_Example_username", "");
Engines.DataPool.setPool("Login_Example_password", "");
}
function logInOrOut(pool)
{
if(pool == "Login_Example_loginReq")
{
// We have received a notification from one of the slave Klips.
var check = Engines.DataPool.getPool("Login_Example_loginReq");
if(check == "LOGIN")
{
// The user has requested to login, so launch the login wizard.
launchLoginWizard();
}
else if(check == "LOGOUT")
{
// The user has requested to logout.
logout();
Actions.Window.hide();
gWindowOpen = false;
}
}
}
function launchLoginWizard()
{
if(!gWindowOpen)
{
// Only launch the login action window if it isn't already displayed.
gWindowOpen = true;
// Clear the username and password fields and display the action window.
c_username.value = "";
c_password.value = "";
Actions.Window.show("Login", "sign_in");
}
}
function onRefresh ()
{
return true;
}
]]>
</klipscript>
</klip>
<klip>
<identity>
<title>
API - Engines.DataPool (Slave)
</title>
</identity>
<locations>
<icon>
http://www.klipfolio.com/static/klips/klipfolio/sample_icon.png
</icon>
<banner>
http://www.klipfolio.com/static/klips/klipfolio/sample_banner.png
</banner>
</locations>
<style>
item
{
type: item;
}
message
{
itemcol: 1;
noterow: 1;
emphasis: strong;
notelabel: false;
label: "Message";
}
</style>
<klipscript>
<![CDATA[
function onLoad ()
{
// Reset the items at startup
Items.clear(true);
// Notifications
// The Slave Klip listens to the "Login_Example_loginNotify" data pool
// for login or logout commands from the Master Klip.
Engines.DataPool.notifyPool("Login_Example_loginNotify", logInOrOut);
// Action Button
Actions.Button.show("Login");
Actions.Button.onClick = buttonClick;
}
function buttonClick()
{
// The user has requested to login, so notify the Master Klip.
Engines.DataPool.setPool("Login_Example_loginReq", "LOGIN");
}
function logoutAction()
{
// The user has requested to logout, so notify the Master Klip.
Engines.DataPool.setPool("Login_Example_loginReq", "LOGOUT");
}
function logout()
{
Items.clear(true);
Items.globalactions = [];
Actions.Button.show ("Login");
}
function logInOrOut(pool)
{
if(pool == "Login_Example_loginNotify")
{
// This is a notification from the Master Klip.
var check = Engines.DataPool.getPool("Login_Example_loginNotify");
if(check == "LOGIN")
{
// The user has successfully logged in, so refresh the Klip
// to get the user's login information.
Klip.requestRefresh();
}
else if(check == "LOGOUT")
{
// The user has logged out, so clear the Klip.
logout();
}
}
}
function onRefresh ()
{
var username = Engines.DataPool.getPool("Login_Example_username");
var password = Engines.DataPool.getPool("Login_Example_password");
var success = true;
if(username.length && password.length)
{
// If the username and password are available, the Master Klip has successfully
// logged in.
success = Engines.Data.process("<item><message>Login Successful!</message></item>");
Items.globalactions = ["Logout", logoutAction];
}
return success;
}
]]>
</klipscript>
</klip>
Since:
5.0.1
See also:
The
How-to article on sharing data among multiple Klips.

