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

/assets/js/animal.js

https://gitlab.com/HonestidaMordaz/pet-store
JavaScript | 138 lines | 127 code | 10 blank | 1 comment | 13 complexity | dfb2ea63e716a4f94e2543b71e2ff8ff MD5 | raw file
  1. // Generated by CoffeeScript 1.9.3
  2. (function() {
  3. window.Animal = (function() {
  4. function Animal() {}
  5. Animal.CATEGORIES = ["All", "Dog", "Cat", "Rabbit", "Horse"];
  6. Animal.prototype.behaviors = function() {
  7. switch (this.type) {
  8. case "cat":
  9. return ["meow", null];
  10. case "dog":
  11. return ["bark", "wag"];
  12. case "rabbit":
  13. return [null, "hop hop"];
  14. case "bird":
  15. return ["chirp", "flap"];
  16. case "horse":
  17. case "donkey":
  18. return ["neigh", null];
  19. default:
  20. return [null, null];
  21. }
  22. };
  23. Animal.prototype.matchesFilter = function(criteria) {
  24. if (criteria == null) {
  25. criteria = 'All';
  26. }
  27. return criteria === "All" || criteria.toLowerCase() === this.type;
  28. };
  29. Animal.prototype.fetchBreedInfo = function(callback) {
  30. return reqwest({
  31. url: "https://api.duckduckgo.com/",
  32. data: {
  33. q: this.breed,
  34. format: "json",
  35. t: "CoffeeScriptBook"
  36. },
  37. type: "jsonp",
  38. success: (function(_this) {
  39. return function(response) {
  40. var topic, topics;
  41. if (response.Abstract) {
  42. _this.breedInfo = {
  43. description: response.Abstract,
  44. source: response.AbstractSource,
  45. url: response.AbstractURL
  46. };
  47. }
  48. callback();
  49. topics = (function() {
  50. var i, len, ref, results;
  51. ref = response.RelatedTopics;
  52. results = [];
  53. for (i = 0, len = ref.length; i < len; i++) {
  54. topic = ref[i];
  55. if (topic.FirstURL != null) {
  56. results.push(topic.FirstURL);
  57. }
  58. }
  59. return results;
  60. })();
  61. return _this.fetchExtraLinks(topics, callback);
  62. };
  63. })(this)
  64. });
  65. };
  66. Animal.prototype.fetchExtraLinks = function(topics, callback) {
  67. var expected, i, len, results, topic;
  68. this.extraLinks = {};
  69. expected = topics.length;
  70. results = [];
  71. for (i = 0, len = topics.length; i < len; i++) {
  72. topic = topics[i];
  73. results.push((function(_this) {
  74. return function(topic) {
  75. return reqwest({
  76. url: topic,
  77. data: {
  78. format: "json"
  79. },
  80. type: "jsonp",
  81. success: function(response) {
  82. if (response.Heading) {
  83. _this.extraLinks[response.Heading] = topic;
  84. expected -= 1;
  85. if (expected === 0) {
  86. return callback();
  87. }
  88. }
  89. }
  90. });
  91. };
  92. })(this)(topic));
  93. }
  94. return results;
  95. };
  96. Animal.fromHash = function(data) {
  97. var animal, key, val;
  98. animal = new this;
  99. for (key in data) {
  100. val = data[key];
  101. animal[key] = val;
  102. }
  103. return animal;
  104. };
  105. Animal.loadSeedData = function(callback) {
  106. return reqwest({
  107. url: "/pets",
  108. type: "json",
  109. success: (function(_this) {
  110. return function(response) {
  111. var animal, animals;
  112. animals = (function() {
  113. var i, len, results;
  114. results = [];
  115. for (i = 0, len = response.length; i < len; i++) {
  116. animal = response[i];
  117. results.push(this.fromHash(animal));
  118. }
  119. return results;
  120. }).call(_this);
  121. return callback(animals);
  122. };
  123. })(this)
  124. });
  125. };
  126. return Animal;
  127. })();
  128. }).call(this);