repeated-items.html 2.8 KB

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