/app/views/catalog.js

https://github.com/corsairdnb/vuaro · JavaScript · 58 lines · 47 code · 11 blank · 0 comment · 0 complexity · 3a6acd76509788d52e8598f1b6d266f8 MD5 · raw file

  1. define([
  2. 'jquery',
  3. 'backbone',
  4. 'handlebars',
  5. 'underscore',
  6. 'models/catalog',
  7. 'text!templates/catalog.html',
  8. 'collections/cart'
  9. ], function($, Backbone, Handlebars, _, model, template, collection) {
  10. var View = Backbone.View.extend({
  11. initialize: function() {
  12. this.model = model;
  13. this.model.on("change", this.render, this);
  14. this.listenTo(collection, "add remove", this.render);
  15. },
  16. el: $(".catalog"),
  17. template: Handlebars.compile(template),
  18. events: {
  19. "click button": "add"
  20. },
  21. getItemAttributes: function(btn){
  22. return {
  23. id: btn.attr("data-id"),
  24. name: btn.attr("data-name")
  25. };
  26. },
  27. add: function(e) {
  28. var btn = $(e.target);
  29. btn.attr("disabled","disabled");
  30. collection.create(this.getItemAttributes(btn));
  31. },
  32. isDisabled: function(id) {
  33. return typeof collection.get(id) !== "undefined";
  34. },
  35. render: function() {
  36. var $this = this;
  37. $this.$el.html("");
  38. _.each(this.model.attributes, function(item){
  39. item["disabled"] = $this.isDisabled(item.id);
  40. $this.$el.append($this.template(item))
  41. });
  42. }
  43. });
  44. return function(container){
  45. return new View();
  46. };
  47. });