PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/views/cart.js

https://github.com/corsairdnb/vuaro
JavaScript | 49 lines | 39 code | 10 blank | 0 comment | 0 complexity | a2dbfd7f7c187bdc0480756e55b00378 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. define([
  2. 'jquery',
  3. 'backbone',
  4. 'handlebars',
  5. 'underscore',
  6. 'collections/cart',
  7. 'text!templates/cart.html'
  8. ], function($, Backbone, Handlebars, _, collection, template) {
  9. var View = Backbone.View.extend({
  10. initialize: function() {
  11. this.listenTo(collection, 'add', this.render);
  12. this.$list = this.$(".list");
  13. collection.fetch({reset: true});
  14. },
  15. el: ".cart",
  16. template: Handlebars.compile(template),
  17. events: {
  18. "click .clear": "clear",
  19. "click button": "remove"
  20. },
  21. clear: function(){
  22. _.invoke(collection.slice(0), "destroy");
  23. this.$list.html("");
  24. },
  25. remove: function(e){
  26. var id = $(e.target).attr("data-id");
  27. collection.get(id).destroy();
  28. this.render();
  29. },
  30. render: function() {
  31. var $this = this;
  32. $this.$list.html("");
  33. _.each(collection.models, function(item){
  34. $this.$list.append($this.template(item.attributes));
  35. });
  36. }
  37. });
  38. return View;
  39. });