public
Last active

Animals

  • Download Gist
Animals.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/// <reference path="//Microsoft.WinJS.1.0/js/base.js" />
/// <reference path="//Microsoft.WinJS.1.0/js/ui.js" />
 
 
(function () {
"use strict";
 
WinJS.Namespace.define("Zoo", {
Animal: WinJS.Class.define(
 
//constructor
function () {
this._name = "";
this._age = "";
this._isHungry = false;
this._hoursSinceLastFeed = 0;
},
 
//methods
{
getName: function () { return this._name; },
setName: function (newValue) { this._name = newValue; },
 
getAge: function () { return this._age; },
setAge: function (newValue) { this._age = newValue; },
 
isHungry: function () { return this._isHungry; },
 
getHoursSinceLastFeed: function () { return this._hoursSinceLastFeed; },
setHoursSinceLastFeed: function (newValue) {
this._hoursSinceLastFeed = newValue;
//if it has been 4 hours since last feed
//the animal is hungry for sure :)
if (newValue > 4) {
this._isHungry = true;
}
else {
this._isHungry = false;
}
},
},
 
//static methods
{
buildAnimal: function (model) {
 
var newAnimal = new Zoo.Animal();
 
if (model.hasOwnProperty("name")) {
newAnimal.setName(model.name);
}
 
if (model.hasOwnProperty("age")) {
newAnimal.setAge(model.age);
}
 
if (model.hasOwnProperty("hoursSinceLastFeed")) {
newAnimal.setHoursSinceLastFeed(model.hoursSinceLastFeed);
}
 
//return a Bindable object
return new WinJS.Binding.as(newAnimal);
},
 
loadZoo: function (uri) {
//IMPORTANT TO RETURN THE PROMISE
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri)
.then(function (file) {
return Windows.Storage.FileIO.readTextAsync(file)
.then(function (textFromFile) {
var myParsedJsonData = JSON.parse(textFromFile);
 
//this will store all the new animals transferred to zoo
var zoo = new Array();
 
if (myParsedJsonData) {
myParsedJsonData.forEach(function (newObject) {
var newAnimal = Zoo.Animal.buildAnimal(newObject);
zoo.push(newAnimal);
});
}
 
return zoo;
});
});
}
})//end WinJS.Class.define
});
})();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.