Globals Block
From KynetxDocs
The global block allows the programmer to create definitions that will be used throughout the ruleset. Global definitions can emit JavaScript and desclare datasets.
Contents |
CSS
The global declaration can contain one or more css statements that will add a <style/> element at the end of the header. The style element will contain anything inside the css statement. The css statement is followed by a string or extended quote that gives the new CSS.
The following example would change the global value for anchor links on the page:
global {
css <<
a:link {color:#FF9}
>>;
}
Note that CSS is a global and will be injected on the page whether or not a rule is selected in the ruleset. This is subject to change as we get more experience with CSS and KRL.
Global Emits
A global JavaScript emitt can be used to declare JavaScript variables and functions for later user by all rules in the ruleset as shown here:
global {
emit <<
var pagename = 'foobar';
>>;
emit <<
var pagename2 = 'fizzle';
>>
}
This is equivalent to
global {
emit <<
var pagename = 'foobar';
var pagename2 = 'fizzle';
>>
}
Datasets
Global declarations can declare a dataset for use in the rules of the program.
Dataset declarations are preceded by the keywork dataset and contain a name and URL separated by the < symbol.
An optional caching specification can added after the URL. If a caching specification is not given the data is assumed to not be cachable. The caching specification consists of the keyword cachable followed by an optional time period specified as
for <num> <period>
where <period> can be seconds, minutes, hours, days, or weeks. If the time period is not specified, the caching period defaults to 24 hours.
The following shows examples of dataset declarations.
global {
dataset public_timeline <- "http://twitter.com/statuses/public_timeline.json";
dataset cached_timeline <- "http://twitter.com/statuses/public_timeline.json" cachable;
dataset other_data <- "http://example.com/foo.json" cachable for 3 hours;
}
Notes on Usage
- A dataset declaration defines variables with the data in both the KRL ruleset and in the Javascript that will be run on the browser.
- The data is assumed to be JSON and is parsed on the server. If that fails for any reason, the data will be returned as a single string.
- The data is retrieved and cached on the server, not directly from the data source in the browser.
- To avoid server overload, a minimum caching period (currently 20 minutes) is imposed on all remote data sources. Specifying caching periods shorter than the minimum will have no effect.
- Large datasets with short caching periods can cause significant load on the user's browser as these must be downloaded frequently and may cause performance problems for your ruleset.
- Multiple dataset specification can cause performance problems.
Datasources
Datasource are like datasets in that they are a way of accessing remote data, but they are queried when needed rather than loaded all at once as datasets are. Like data sets, data sources are declared in the globla declaration block like so:
global {
datasource library_search <- "http://test.azigo.com:9083/solr/select/?wt=json";
}
Later, a data source can be queried as a user defined datasource in the pre block of any rule in the ruleset.
Data sources use the same syntax for specifying cachability as data sets.
global {
datasource library_search <- "http://test.azigo.com:9083/solr/select/?wt=json" cachable for 3 hours;
}
