fast-repeat.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var _ = require('./util')
  2. var FragmentFactory = require('./fragment/factory')
  3. module.exports = {
  4. bind: function () {
  5. var inMatch = this.expression.match(/(.*) in (.*)/)
  6. if (inMatch) {
  7. this.arg = inMatch[1]
  8. this._watcherExp = inMatch[2]
  9. }
  10. this.start = _.createAnchor('fast-repeat-start')
  11. this.end = _.createAnchor('fast-repeat-end')
  12. _.replace(this.el, this.end)
  13. _.before(this.start, this.end)
  14. // fragment factory
  15. this.factory = new FragmentFactory(this.vm, this.el)
  16. },
  17. create: function (data) {
  18. var host = this._host
  19. var parentScope = this._scope || this.vm
  20. var scope = Object.create(parentScope)
  21. var alias = this.arg
  22. Object.defineProperty(scope, alias, {
  23. enumberable: true,
  24. configurable: true,
  25. value: data
  26. })
  27. return this.factory.create(host, scope)
  28. },
  29. update: function (list) {
  30. var self = this
  31. var anchor = this.end
  32. if (this.frags) {
  33. this.frags.forEach(function (f) {
  34. f.destroy()
  35. })
  36. }
  37. this.frags = list.map(function (data) {
  38. return self.create(data)
  39. })
  40. this.frags.forEach(function (f) {
  41. f.before(anchor)
  42. })
  43. }
  44. }