|
Syntax BasicsSalsaScript is written in files with the extension ".sjs", for Serverside JavaScript. SJS files are HTML files with embedded tags for SalsaScript code, similar to JSP, PHP, or ASP pages. If you are using the standard Salsa Controllers, SJS pages can also be thought of as the 'View' component in a Model-View-Controller architecture.
SalsaScript code is distinguished from standard HTML in two ways:
<? var x = 3 + 3; ?> <?= x ?>will return to the browser nothing but the value: 6 You can also use the 'print' method to print a value directly to the page
<?
var x = 3 + 3;
print(x);
?>
results in
6 All Javascript standard methods and code blocks are supported, using standard brackets.
<? for (var i = 0; i < 10; i++) { ?>
<?= i ?>
<? } ?>
results in
0
1
2
3
4
5
6
7
8
9
Just as in normal Javascript, you can also create functions dynamically, and call them inline
<?
// This function has embedded HTML and Javascript
function displayList(number) { ?>
<li><?= number ?>.
<? }
//The page loops through 10 numbers, and calls the displayList function for each
for (var i = 0; i < 10; i++) {
displayList(i);
}
?>
results in
Common FunctionsSalsaScript comes with a variety of common functions to grant access to different parts of an application.URL ParametersYou can retrieve parameters from the URL using a salsa.getParameter method call.
<?
var x=salsa.getParameter("name");
print(x);
?>
getParameter will always return an object with a length, even if it does not exist, so to test if a value is passed, use the ".length" attribute.
<?
var x=salsa.getParameter("name");
if (x.length>0){
?>Your parameter was <?=x?><?
}else{
?>A name was not specified.<?
}
?>
Sometimes the existence of a parameter is important, regardless if it has a value or not. To test for the presence of a parameter, use the 'hasParameter' method, which will return back true or false if a parameter is present or not.
<?
//will return true for url 'mypage.sjs?name'
if (salsa.hasParameter("name")){
?>There is a 'name' parameter<?
}else{
//will return false for url 'mypage.sjs'
?>There is not a 'name' parameter<?
}
?>
salsa.forward and salsa.sendRedirect SyntaxsalsaScript allows you to use the jsp .forward and .sendRedirect actions.
Forward
salsa.forward("/api/action/processAction.jsp");
will return to the browser nothing but the value:
Redirect
salsa.sendRedirect("/api/action/processAction.jsp");
When using a redirect, the server sends back an HTTP respons status of 302. HTTP/1.1 302 Object moved Location: domain/newlocation.jsp Baked in XMLThe Javascript standard incorporates XML as a first class citizen, meaning that XML can be included inline with Javascript/SalsaScript code.
<?
var myxml=<data organization_KEY="1">
<event>
<item>
<event_KEY>23</event_KEY>
<Event_Name>Team Meeting</Event_Name>
</item>
<item>
<event_KEY>24</event_KEY>
<Event_Name>House Party</Event_Name>
</item>
<count>2</count>
</event>
</data>
for each(event in myxml.event.item){
?><li><?=event.Event_Name?></li>
<?
}
?>
results in:
XML data can be used inline, or created from a String using the XML constructor <? var xmlString="<item><Event_Name>Team Meeting</Event_Name></item><item><event_KEY>24</event_KEY><Event_Name>House Party</Event_Name></item>"; var xmlObject=new XML(xmlString); ?> Note: The SalsaScript interpreter requires that new XML not contain an xml tag (), and start with a '<';
The Crawler ObjectIn the new server to server world, it's often useful to pull content dynamically from a webpage. SalsaScript provides the 'crawler' object to simplify retrieving data. Getting Web pagesAn HTTP GET request is useful for retrieving data from RESTful API services, or other web pages you want to parse the content of, such as an RSS feed. Parameters for a GET request are appended to the end of
<?
var content = crawler.get("http://salsa.democracyinaction.org/dia/info.jsp?param=myparam");
?>
Getting XML feedsA common use of the GET command is to retrieve a RSS feed, or other XML entities. These pages are often prefixed with a <?xml?> tag, that is not handled by Javascript. The crawler.getXML(url) method retrieves the XML, and removes errant characters, to return back a valid Javascript XML object.
<?
var content = crawler.getXML("http://rss.cnn.com/rss/cnn_topstories.rss");
?>
Posting to a URLAn HTTP POST request is useful if you're posting account information, or information you don't want to appear in logs. Post parameters are contained in a Javascript object as the second parameter.
<?
var content = crawler.post("http://rss.cnn.com/rss/cnn_topstories.rss",{myparam:'myvalue',myparam2:'myvalue2'});
?>
crawler.get + XML = SnappyCombining the crawler object and standard XML is a fast and easy way to pull and render XML content to the screen. For example, the following few lines of code pulls down a current RSS feed from CNN, parses it as XML, then loops through all the top stories, and prints them to the screen.
<h3>Top Stories</h3>
<?
var xmlContent = crawler.getXML("http://rss.cnn.com/rss/cnn_topstories.rss");
for each(story in xmlContent.channel.item){
?><li><a href='<?=story.link?>'><?=story.title?></a></li>
<?
}
?>
|