Skip to content

Instantly share code, notes, and snippets.

@thehig
Created March 4, 2015 22:36
Show Gist options
  • Save thehig/d0a46192fb0f27496b3b to your computer and use it in GitHub Desktop.
Save thehig/d0a46192fb0f27496b3b to your computer and use it in GitHub Desktop.
javascript: Promise Chaining WIP
// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
(function () {
"use strict";
WinJS.UI.Pages.define("/pages/browse/byHub/byHub.html", {
ready: function (element, options) {
// TODO: Initialize the page here.
var that = this;
this._layoutRoot = element;
this._options = options;
this.enterPage = this.enterPage.bind(this);
this.exitPage = this.exitPage.bind(this);
//Set to true when hunting
var bugHunt = function(message) {
if (!bugHunt.entries) bugHunt.entries = [];
var t = new Date();
bugHunt.entries.push(t.getHours() + ":" + t.getMinutes() + "." + t.getSeconds() + " == " + message);
if (bugHunt.enabled)
console.log(message);
};
//this._layoutRoot.querySelector('#header-title').innerText = "Browse Set";
this._debug = bugHunt;
this._pageData = [];
//No parameter
if (!options || !options.parent) {
//TODO: Remove this redundant data call once the 'real' user collections exist
this._featuredProducts= [];
this._pagePromises.push(Rainfall.Services.Products.getProducts(undefined, true).then(function(results) {
this._featuredProducts = results;
}.bind(this)));
//Default value for header
WinJS.Binding.processAll(this._layoutRoot, {title: "Retailers"});
// No parameter -> Get all retailers default categories
this._pagePromises.push(Rainfall.Services.Retailers.getRetailer().then(function (retailers) {
bugHunt("Retailers " + retailers.length);
retailers.forEach(function(retailer) {
//Get the default categories
that._pagePromises.push(Rainfall.Services.Categories.getCategory(retailer.defaultCategories.join())
.then(function(defaultCategories) {
bugHunt("\t" + retailer.title + " tiles " + defaultCategories.length);
retailer.tiles = defaultCategories;
that._pageData.push(retailer);
}));
});
}));
} else {
var parameter = options.parent;
WinJS.Binding.processAll(this._layoutRoot, options.parent);
if (parameter instanceof Rainfall.Models.RetailerDataModel) {
//Load the retailers default categories
this._pagePromises.push(Rainfall.Services.Categories.getCategory(parameter.defaultCategories.join())
.then(function (categories) {
categories.forEach(function(category) {
that._pagePromises.push(Rainfall.Services.Categories.getCategory(category.children.join())
.then(function(childCategories) {
category.tiles = childCategories;
that._pageData.push(category);
})//then
); //push
});//foreach
})//then
);//push
} else if (parameter instanceof Rainfall.Models.CategoryDataModel) {
this._pagePromises.push(Rainfall.Services.Categories.getCategory(parameter.children.join()).then(function(categories) {
categories.forEach(function(category) {
if (category.children && category.children.length > 0) {
//Get the child categories
that._pagePromises.push(Rainfall.Services.Categories.getCategory(category.children.join()).then(function(childCategories) {
category.tiles = childCategories;
that._pageData.push(category);
}));
} else if (category.products && category.products.length > 0) {
//Get the child products
that._pagePromises.push(Rainfall.Services.Products.getProducts(category.products.join()).then(function(childproducts) {
category.tiles = childproducts;
that._pageData.push(category);
}));
}
});
}));
}
}
},
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment