repeated-items.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>SEED repeated items</title>
  5. <meta charset="utf-8">
  6. <script src="bind.js"></script>
  7. <script src="../../../dist/seed.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <p>
  12. <button class="push" sd-on="click:push">push</button>
  13. <button class="pop" sd-on="click:pop">pop</button>
  14. <button class="shift" sd-on="click:shift">shift</button>
  15. <button class="unshift" sd-on="click:unshift">unshift</button>
  16. <button class="splice" sd-on="click:splice">splice</button>
  17. <button class="remove" sd-on="click:remove">remove</button>
  18. <button class="replace" sd-on="click:replace">replace</button>
  19. <button class="sort" sd-on="click:sort">sort</button>
  20. <button class="reverse" sd-on="click:reverse">reverse</button>
  21. </p>
  22. <p>Total items: <span class="count" sd-text="items.length"></span></p>
  23. <ul>
  24. <li class="item" sd-repeat="item:items">
  25. {{$index}} {{item.title}}
  26. </li>
  27. </ul>
  28. </div>
  29. <script>
  30. Seed.config({debug: true})
  31. var items = [
  32. { title: 'A'},
  33. { title: 'B'},
  34. { title: 'C'}
  35. ]
  36. var demo = new Seed({
  37. el: '#app',
  38. scope: {
  39. items: items,
  40. push: function () {
  41. this.items.push({ title: getChar() })
  42. },
  43. pop: function () {
  44. this.items.pop()
  45. },
  46. shift: function () {
  47. this.items.shift()
  48. },
  49. unshift: function () {
  50. this.items.unshift({ title: getChar() })
  51. },
  52. splice: function () {
  53. this.items.splice(1, 1, { title: getChar() }, { title: getChar() })
  54. },
  55. replace: function () {
  56. this.items.replace(getPos(), { title: getChar() })
  57. },
  58. remove: function () {
  59. this.items.remove(getPos())
  60. },
  61. sort: function () {
  62. this.items.sort(function (a, b) {
  63. return a.title.charCodeAt(0) - b.title.charCodeAt(0)
  64. })
  65. },
  66. reverse: function () {
  67. this.items.reverse()
  68. }
  69. }
  70. })
  71. var getChar = (function () {
  72. var count = 0
  73. return function () {
  74. return (count++).toString()
  75. }
  76. })()
  77. function getPos () {
  78. return items.length - 1
  79. }
  80. </script>
  81. </body>
  82. </html>