/assets/js/animal.js
JavaScript | 138 lines | 127 code | 10 blank | 1 comment | 13 complexity | dfb2ea63e716a4f94e2543b71e2ff8ff MD5 | raw file
- // Generated by CoffeeScript 1.9.3
- (function() {
- window.Animal = (function() {
- function Animal() {}
- Animal.CATEGORIES = ["All", "Dog", "Cat", "Rabbit", "Horse"];
- Animal.prototype.behaviors = function() {
- switch (this.type) {
- case "cat":
- return ["meow", null];
- case "dog":
- return ["bark", "wag"];
- case "rabbit":
- return [null, "hop hop"];
- case "bird":
- return ["chirp", "flap"];
- case "horse":
- case "donkey":
- return ["neigh", null];
- default:
- return [null, null];
- }
- };
- Animal.prototype.matchesFilter = function(criteria) {
- if (criteria == null) {
- criteria = 'All';
- }
- return criteria === "All" || criteria.toLowerCase() === this.type;
- };
- Animal.prototype.fetchBreedInfo = function(callback) {
- return reqwest({
- url: "https://api.duckduckgo.com/",
- data: {
- q: this.breed,
- format: "json",
- t: "CoffeeScriptBook"
- },
- type: "jsonp",
- success: (function(_this) {
- return function(response) {
- var topic, topics;
- if (response.Abstract) {
- _this.breedInfo = {
- description: response.Abstract,
- source: response.AbstractSource,
- url: response.AbstractURL
- };
- }
- callback();
- topics = (function() {
- var i, len, ref, results;
- ref = response.RelatedTopics;
- results = [];
- for (i = 0, len = ref.length; i < len; i++) {
- topic = ref[i];
- if (topic.FirstURL != null) {
- results.push(topic.FirstURL);
- }
- }
- return results;
- })();
- return _this.fetchExtraLinks(topics, callback);
- };
- })(this)
- });
- };
- Animal.prototype.fetchExtraLinks = function(topics, callback) {
- var expected, i, len, results, topic;
- this.extraLinks = {};
- expected = topics.length;
- results = [];
- for (i = 0, len = topics.length; i < len; i++) {
- topic = topics[i];
- results.push((function(_this) {
- return function(topic) {
- return reqwest({
- url: topic,
- data: {
- format: "json"
- },
- type: "jsonp",
- success: function(response) {
- if (response.Heading) {
- _this.extraLinks[response.Heading] = topic;
- expected -= 1;
- if (expected === 0) {
- return callback();
- }
- }
- }
- });
- };
- })(this)(topic));
- }
- return results;
- };
- Animal.fromHash = function(data) {
- var animal, key, val;
- animal = new this;
- for (key in data) {
- val = data[key];
- animal[key] = val;
- }
- return animal;
- };
- Animal.loadSeedData = function(callback) {
- return reqwest({
- url: "/pets",
- type: "json",
- success: (function(_this) {
- return function(response) {
- var animal, animals;
- animals = (function() {
- var i, len, results;
- results = [];
- for (i = 0, len = response.length; i < len; i++) {
- animal = response[i];
- results.push(this.fromHash(animal));
- }
- return results;
- }).call(_this);
- return callback(animals);
- };
- })(this)
- });
- };
- return Animal;
- })();
- }).call(this);