common.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict'
  2. const compiler = require('../../dist/compiler.js')
  3. const self = (global || root)
  4. self.performance = {
  5. now: function () {
  6. var hrtime = process.hrtime()
  7. return ((hrtime[0] * 1000000 + hrtime[1] / 1000) / 1000)
  8. }
  9. }
  10. function generateGrid (rowCount, columnCount) {
  11. var grid = []
  12. for (var r = 0; r < rowCount; r++) {
  13. var row = { id: r, items: [] }
  14. for (var c = 0; c < columnCount; c++) {
  15. row.items.push({ id: (r + '-' + c) })
  16. }
  17. grid.push(row)
  18. }
  19. return grid
  20. }
  21. const gridData = generateGrid(1000, 10)
  22. var gridComponent = {
  23. template: '<div><h1>{{ Math.random() }}</h1><my-table></my-table></div>',
  24. components: {
  25. myTable: {
  26. data: function () {
  27. return {
  28. grid: gridData
  29. }
  30. },
  31. // template: '<table><tr v-for="row in grid"><th>123</th><td v-for="item in row.items">{{ item.id }}</td></tr></table>',
  32. template: '<table width="100%" cellspacing="2"><row v-for="row in grid" :row="row"></row></table>',
  33. components: {
  34. row: {
  35. props: ['row'],
  36. template: '<tr><th>{{ Math.random() }}</th><column v-for="item in row.items"></column></tr>',
  37. components: {
  38. column: {
  39. template: '<td class="item">' +
  40. // 25 plain elements for each cell
  41. '<ul class="yoyo">' +
  42. '<li class="hihi" v-for="i in 5">' +
  43. '<span v-for="i in 5">fsefs</span>' +
  44. '</li>' +
  45. '</ul>' +
  46. '</td>'
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. function createCompiledOptions (options) {
  55. const res = compiler.compileToFunctions(options.template, {
  56. preserveWhitespace: false
  57. })
  58. Object.assign(options, res)
  59. delete options.template
  60. if (options.components) {
  61. const keys = Object.keys(options.components)
  62. let total = keys.length
  63. while (total) {
  64. const name = keys[total - 1]
  65. options.components[name] = createCompiledOptions(options.components[name])
  66. total--
  67. }
  68. }
  69. return options
  70. }
  71. module.exports = createCompiledOptions(gridComponent)