repeated-items.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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-each="item:items" sd-text="item.title"></li>
  23. </ul>
  24. </div>
  25. <script src="../dist/seed.js"></script>
  26. <script>
  27. seed.config({debug: true})
  28. var items = [
  29. { title: 'A'},
  30. { title: 'B'},
  31. { title: 'C'}
  32. ]
  33. var demo = new seed.ViewModel({
  34. el: '#app',
  35. data: {
  36. items: items,
  37. push: function () {
  38. this.items.push({ title: randomChar() })
  39. },
  40. pop: function () {
  41. this.items.pop()
  42. },
  43. shift: function () {
  44. this.items.shift()
  45. },
  46. unshift: function () {
  47. this.items.unshift({ title: randomChar() })
  48. },
  49. splice: function () {
  50. this.items.splice(0, 1, { title: randomChar() }, { title: randomChar() })
  51. },
  52. replace: function () {
  53. this.items.replace(randomPos(), { title: randomChar() })
  54. },
  55. remove: function () {
  56. this.items.remove(randomPos())
  57. },
  58. sort: function () {
  59. this.items.sort(function (a, b) {
  60. return a.title.charCodeAt(0) - b.title.charCodeAt(0)
  61. })
  62. },
  63. reverse: function () {
  64. this.items.reverse()
  65. }
  66. }
  67. })
  68. function randomChar () {
  69. return String.fromCharCode(Math.floor(Math.random() * 30 + 50))
  70. }
  71. function randomPos () {
  72. return Math.floor(Math.random() * items.length)
  73. }
  74. </script>
  75. </body>
  76. </html>