/www/backbone.js

https://github.com/3on/SynergyPad · JavaScript · 137 lines · 109 code · 25 blank · 3 comment · 3 complexity · 336837919d69cbcdf6dd079679e3b690 MD5 · raw file

  1. function init() {
  2. window.People = {}
  3. window.People.comment = Backbone.Model.extend({})
  4. window.People.commentList = Backbone.Collection.extend({
  5. model: window.People.comment
  6. })
  7. window.People.post = Backbone.Model.extend({
  8. default: function() {
  9. return {comments: new People.commentList()}
  10. }
  11. })
  12. window.People.postList = Backbone.Collection.extend({
  13. model: window.People.post,
  14. localStorage: new Store("PeopleFeed"),
  15. })
  16. window.People.feed = new window.People.postList()
  17. window.People.postView = Backbone.View.extend({
  18. tagName: 'li',
  19. template: _.template($('#tmpl-post').html()),
  20. events: {
  21. 'click li.feed div.post' : 'showComments',
  22. 'click img.comment' : 'showComments',
  23. 'click img.like' : 'likePost',
  24. 'keypress #peopleFeed li.feed li.add input' : 'addOnEnter',
  25. 'click #peopleFeed li.feed li.add button' : 'addOnClick',
  26. },
  27. initialize: function() {
  28. this.model.bind('change', this.render, this);
  29. this.model.bind('destroy', this.remove, this);
  30. this.input = $(this.el).select('li.add input').first()
  31. console.log(this)
  32. },
  33. showComments: function() {
  34. alert('Comments')
  35. return false
  36. },
  37. likePost: function() {
  38. alert('Like')
  39. return false
  40. },
  41. addOnEnter: function(e) {
  42. var text = this.input.val();
  43. this.comments.create({
  44. author: 'Someone',
  45. text: text
  46. });
  47. this.input.val('');
  48. },
  49. addOnClick: function(e) {
  50. var text = this.input.val();
  51. People.feed.create({
  52. author: 'Someone',
  53. text: text
  54. });
  55. this.input.val('')
  56. return false
  57. },
  58. render: function() {
  59. $(this.el).html(this.template(this.model.toJSON()));
  60. return this;
  61. },
  62. remove: function() {
  63. $(this.el).remove();
  64. }
  65. })
  66. window.People.feedView = Backbone.View.extend({
  67. el: $('#peopleFeed'),
  68. input: $('#peopleFeed .add input'),
  69. add: $('#peopleFeed .add'),
  70. events: {
  71. 'keypress #peopleFeed .add input': 'createOnEnter',
  72. 'click #peopleFeed .add button': 'createOnClick'
  73. },
  74. createOnEnter: function(e) {
  75. var text = this.input.val();
  76. if (!text || e.keyCode != 13) return;
  77. People.feed.create({
  78. text: text
  79. });
  80. this.input.val('');
  81. },
  82. createOnClick: function(e) {
  83. var text = this.input.val();
  84. People.feed.create({
  85. text: text
  86. });
  87. this.input.val('');
  88. },
  89. addOne: function(post) {
  90. var view = new People.postView({
  91. model: post
  92. });
  93. this.add.after(view.render().el);
  94. },
  95. render: function()
  96. {
  97. $("#peopleFeed li:not(li[class=add])").remove()
  98. //$.tmpl(this.template, this.model).appendTo(this.el)
  99. return this
  100. },
  101. initialize: function() {
  102. People.feed.bind('add', this.addOne, this);
  103. //PeopleView.bind('reset', this.addAll, this);
  104. //PeopleView.bind('all', this.render, this);
  105. this.render()
  106. }
  107. })
  108. }
  109. $(function() {
  110. init()
  111. var peopleFeed = new People.feedView()
  112. })