Skip to content

Instantly share code, notes, and snippets.

@gergob
Created November 24, 2012 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gergob/4141058 to your computer and use it in GitHub Desktop.
Save gergob/4141058 to your computer and use it in GitHub Desktop.
Animals
/// <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
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment