Created
October 9, 2011 21:34
-
-
Save edorcutt/1274226 to your computer and use it in GitHub Desktop.
KRL WebApp Template Cheatsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruleset a169x425 { | |
meta { | |
name "KRL WebApp Template Cheatsheet" | |
description << | |
KRL Demo Template | |
>> | |
author "Ed Orcutt, LOBOSLLC" | |
logging on | |
// -------------------------------------------- | |
// Test development version of Ruleset | |
// http://ktest.heroku.com/a169x425 | |
// -------------------------------------------- | |
// ErrorStack reporting of errors from your ruleset | |
// http://docs.kynetx.com/docs/ErrorStack | |
key errorstack "REDACTED" | |
// -------------------------------------------- | |
// Error Handling in KRL | |
// http://www.windley.com/archives/2011/05/error_handling_in_krl.shtml | |
use module a16x104 alias es | |
with es_key = keys:errorstack() | |
// -------------------------------------------- | |
// jQuery UI library | |
// http://docs.kynetx.com/docs/JQuery_UI | |
use css resource jquery_ui_lightness_css | |
use javascript resource jquery_ui_js | |
// -------------------------------------------- | |
// Use external resource files, either javascript or CSS | |
// http://docs.kynetx.com/docs/Use_resource | |
// use javascript resource "" | |
// use css resource "" | |
} | |
dispatch { | |
// domain "exampley.com" | |
} | |
global { | |
thisRID = meta:rid(); | |
demoArray = ["Bob", "Ted", "Alice"]; | |
} | |
// ------------------------------------------------------------------------ | |
rule hello_world { | |
select when pageview ".*" setting () | |
pre { | |
msg = "hello neo ..."; | |
num = math:random(99999); | |
} | |
{ | |
notify("KRL Demo Template", msg) with sticky = true; | |
// -------------------------------------------- | |
// Raise event action | |
// http://docs.kynetx.com/docs/Raise_Event_Action_and_Runtime_API | |
raise_event("randomHello") with app_id = thisRID | |
and parameters = { | |
"num": num | |
}; | |
// -------------------------------------------- | |
// Insert your own Javascript | |
// http://docs.kynetx.com/docs/Emit | |
emit << | |
// -------------------------------------------- | |
// Raise event to plant StatCounter | |
// with statURL as an event parameter | |
// http://docs.kynetx.com/docs/Raise_Event_Action_and_Runtime_API | |
var thisApp = KOBJ.get_application(thisRID); | |
thisApp.raise_event("statcounter", { | |
"statURL" : "http://c.statcounter.com/7288818/0/0ab97fe3/1/" | |
}, thisRID); | |
>>; | |
} | |
// -------------------------------------------- | |
// Actions to be taken based on whether or not the rule fired | |
// http://docs.kynetx.com/docs/Postlude | |
fired { | |
raise explicit event loopit for thisRID | |
with title = "Explicit Event Demo"; | |
} | |
} | |
// ------------------------------------------------------------------------ | |
rule hello_jquery { | |
select when pageview ".*" setting () | |
pre { | |
demo_div = << | |
<div class="demo-dialog" title="Dialog Demo" style="display: none; "> | |
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p> | |
</div> | |
>>; | |
} | |
{ | |
append("body", demo_div); | |
jquery_ui:dialog(".demo-dialog") with closeText = "Close Me"; | |
} | |
} | |
// ------------------------------------------------------------------------ | |
rule randomHello { | |
select when web randomHello | |
pre { | |
num = event:param("num"); | |
msg = "random number: #{num}"; | |
} | |
{ | |
notify("Raise Event Demo", msg) with sticky = true; | |
} | |
} | |
// ------------------------------------------------------------------------ | |
rule loopit_first { | |
select when explicit loopit | |
pre { | |
title = event:param("title"); | |
} | |
{ | |
notify(title, "I am run first!") with sticky = true; | |
} | |
} | |
// ------------------------------------------------------------------------ | |
rule loopit_second { | |
select when explicit loopit | |
foreach demoArray setting (myName) | |
pre { | |
msg = "Hello " + myName; | |
} | |
{ | |
notify("Foreach Demo", msg) with sticky = true; | |
} | |
} | |
// ------------------------------------------------------------------------ | |
// StatCounter provides Analytics for your Web App | |
// http://statcounter.com/ | |
rule StatCounter { | |
select when web statcounter | |
pre { | |
statURL = event:param("statURL"); | |
// StatCounter tracking code | |
statCode = << <img src="#{statURL}" border="0"> >>; | |
} | |
append("body", statCode); | |
} | |
// ------------------------------------------------------------------------ | |
// Error Handling in KRL | |
// http://www.windley.com/archives/2011/05/error_handling_in_krl.shtml | |
rule Catch_Errors_Throw_At_ErrorStack { | |
select when system error or user error | |
pre { | |
genus = event:param("genus"); | |
species = event:param("species"); | |
} | |
es:send_error("(#{genus}:#{species}) " + event:param("msg")) | |
with rule_name = event:param("rule_name") | |
and rid = event:param("rid"); | |
} | |
// ------------------------------------------------------------------------ | |
// Beyond here there be dragons :) | |
// ------------------------------------------------------------------------ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment