PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/public/my.js

https://bitbucket.org/koalaKaravan/gigabrausr
JavaScript | 54 lines | 38 code | 10 blank | 6 comment | 0 complexity | cf31b5f7627c5ef4e916eb9139913d48 MD5 | raw file
  1. // (c) 2012 Cole Krumbholz, SendSpree Inc.
  2. //
  3. // This document may be used and distributed in accordance with
  4. // the MIT license. You may obtain a copy of the license at
  5. // http://www.opensource.org/licenses/mit-license.php
  6. var MyThings = Backbone.Collection.extend({
  7. url: '/backliftapp/mythings',
  8. });
  9. var MyView = Backbone.View.extend({
  10. el: '#mainview',
  11. initialize: function() {
  12. this.collection.bind('add', this.render, this);
  13. this.collection.bind('remove', this.render, this);
  14. this.collection.bind('reset', this.render, this);
  15. },
  16. render: function() {
  17. var t = _.template($('#mainview-template').html(), {
  18. thinglist: this.collection
  19. });
  20. this.$('ul').html(t);
  21. },
  22. events: {
  23. "click #add-btn": "add_thing",
  24. "click .thing-del-btn": "del_thing",
  25. },
  26. add_thing: function (ev) {
  27. var thing_name = $('#add-input').val();
  28. newthing = new Backbone.Model({name: thing_name});
  29. this.collection.add(newthing);
  30. newthing.save();
  31. },
  32. del_thing: function (ev) {
  33. // format of button id is 'del-' + cid
  34. var cid = ev.target.id.split('-')[1];
  35. var oldthing = this.collection.getByCid(cid);
  36. oldthing.destroy();
  37. this.collection.remove(oldthing);
  38. },
  39. });
  40. $(function() {
  41. var myThings = new MyThings();
  42. var myView = new MyView({collection: myThings});
  43. myThings.fetch();
  44. });