eventsineach.html 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <script type="text/javascript" src="../build/jsMotif.js"></script>
  6. <title>jsMotif example</title>
  7. </head>
  8. <body>
  9. <h2>Cick on author</h2>
  10. <div id="template">
  11. <ul each="{books}">
  12. <li>
  13. <label>title: </label><strong fill="{title}"></strong><br>
  14. <label>author: </label><a href="#" rel="author-rel" fill="{author}"></a>
  15. </li>
  16. </ul>
  17. </div>
  18. <div id="message">
  19. </div>
  20. <script type="text/javascript">
  21. var books = {
  22. books: [
  23. {
  24. title: 'Earthquake',
  25. author: 'Ian Rohr'
  26. },
  27. {
  28. title: 'Woody Allen',
  29. author: 'Eric Lax'
  30. },
  31. {
  32. title: 'Rick Santorum',
  33. author: 'Rick Santorum'
  34. },
  35. {
  36. title: 'Memorial Day',
  37. author: 'Vince Flynn'
  38. },
  39. {
  40. title: 'Earthquake',
  41. author: 'Greg Roza'
  42. }
  43. ]
  44. };
  45. var tpl = new jsMotif.Template({
  46. template: 'template',
  47. targetNode: 'template',
  48. events:[{
  49. rel: 'author-rel',
  50. name: 'click',
  51. handler: function(book){
  52. /**
  53. Since rel is placed within 'each' instruction it has changed scope and instead of entire 'books' object,
  54. proper element from 'books' array is returned
  55. */
  56. var messageElem = document.getElementById('message');
  57. messageElem.innerHTML = 'You have selected <b>' + book.author + '</b>';
  58. }
  59. }]
  60. });
  61. tpl.render(books);
  62. </script>
  63. </body>
  64. </html>