Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

  • create your sample application: create a folder "helloJquery" inside "apps"(e.g. with webDAV)
  • download jQuery and copy it in your "helloJquery" folder (e.g. with curl)
  • create a file helloJquery.html with the content below and copy it in your "helloJquery"
  • point your browser to http://localhost:8888/apps/helloJquery/helloJquery.htmlImage Removed. the next steps are there...
Code Block
html
html
titlehelloJquery.html
<html>
  <head>
    <title>Driving Sling with jQuery</title>
    <script type="text/javascript" src="jquery-1.2.6.js"></script>
  </head>
<body>

<h3>Driving Sling with jQuery</h3>
<p>Start firebug, click on Console, then go multiline (click the little red arrow on the bottom bottom right)</p>
<p>enter the code below, click on 'run'</p>

<pre>
$.post(
  "/content/*",
  { ':nameHint': 'myNamemyNode', oneProperty: 'oneValue' }, 
  function(data){ alert(data); }
);
</pre>

<p>Then <a href="http://localhost:7402/content/mynamemyNode.html">view your new node</a></p>

</body>
<html>

...

We used jQuery to create a node named "mynodemyNode" below "content", with a property "oneProperty" and value "oneValue".

Code Block
JavaScript
JavaScript
$.post(
  "/content/*",
  {
    ':nameHint': 'myNamemyNode',
    oneProperty: 'oneValue'
  },
  function(data){
    alert(data);
  }
);

If you are not familiar with jQuery, the code might look cryptic, but in 2 words: $.post creates the ajax post request, taking 3 parameters:

  • "/content/*" as url, using SlingPostServlet to create a new jcr node under the /content node
  • ':nameHint': "mynodemyNode", oneProperty: 'oneValue' is a key:pair list of request parameters.
    • ':nameHint' is a command (starts with ':') that tells Sling to use "mynodemyNode" for the node name.
    • oneProperty: sets one property under mynode myNode
  • the third (optional) parameter is a callback

It's the ajax equivalent of using this html form:

Code Block
html
html
<form action="/content/*" method="POST">
  <input type="hidden" name=":nameHint"   value=""myNamemyNode">
  <input type="text"   name="oneProperty" value="oneValue">
</form>

...