repeated-items.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. ]
  30. var demo = new seed.ViewModel({
  31. el: '#app',
  32. data: {
  33. items: items,
  34. push: function () {
  35. this.items.push({ title: randomChar() })
  36. },
  37. pop: function () {
  38. this.items.pop()
  39. },
  40. shift: function () {
  41. this.items.shift()
  42. },
  43. unshift: function () {
  44. this.items.unshift({ title: randomChar() })
  45. },
  46. splice: function () {
  47. this.items.splice(0, 1, { title: randomChar() }, { title: randomChar() })
  48. },
  49. replace: function () {
  50. this.items.replace(randomPos(), { title: randomChar() })
  51. },
  52. remove: function () {
  53. this.items.remove(randomPos())
  54. },
  55. sort: function () {
  56. this.items.sort(function (a, b) {
  57. return a.title.charCodeAt(0) - b.title.charCodeAt(0)
  58. })
  59. },
  60. reverse: function () {
  61. this.items.reverse()
  62. }
  63. }
  64. })
  65. function randomChar () {
  66. return String.fromCharCode(Math.floor(Math.random() * 30 + 50))
  67. }
  68. function randomPos () {
  69. return Math.floor(Math.random() * items.length)
  70. }
  71. </script>
  72. </body>
  73. </html>