Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. // model
  2. function Model () {
  3. this._state = {}
  4. return this
  5. }
  6.  
  7. Model.prototype.get = function (key) {
  8. return this._state[key]
  9. }
  10.  
  11. Model.prototype.set = function (key, value) {
  12. this._state[key] = value
  13. return this
  14. }
  15.  
  16. // view
  17. function View (container, html) {
  18. this.container = container
  19. this.html = html
  20. return this
  21. }
  22. View.prototype.template = function (model) {
  23.  
  24. var _html = this.html
  25.  
  26. Object
  27. .keys(model)
  28. .forEach(function (key) {
  29. _html = _html.replace(new Regexp('({{\s?' + key + '\s?}})', 'g'), model[key])
  30. })
  31.  
  32. return $(_html)
  33. }
  34. View.prototype.render = function (model) {
  35. this
  36. .template(model)
  37. .appendTo(this.container)
  38. }
  39.  
  40. // controller
  41. var model = new Model()
  42. .set('foo', 10)
  43. .set('bar', 20)
  44.  
  45. // demo
  46. var html = '<div class="{{ foo }}">{{ bar }}</div>'
  47. new View($('#container'), html)
  48. .render(model)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement