Permalink
Browse files

Initial code for the optional todo #8 of part 3

  • Loading branch information...
1 parent d457c80 commit ffb47557925de82d4061e229afd68ed6eaf983fe @techarch committed Oct 29, 2011
@@ -48,6 +48,10 @@ sgs.mediator.consumptionscenarios.createViewMediator = function (pageSettings) {
// Save the view model
sgs.mediator.consumptionscenarios.setViewModel(viewModel);
+ // Set the pricing based on the Coffee Pricing view model
+ var priceList = sgs.mediator.coffeepricing.getViewModel();
+ viewModel.pricing(priceList);
+
if (typeof(console) != 'undefined' && console) console.info("sgs.mediator.coffeeconsumption ready!");
}
@@ -11,6 +11,7 @@ sgs.model.coffeeconsumption.initializeViewModel = function (pageSettings, scenar
if (typeof(pageSettings) == 'undefined') var pageSettings = { }
var viewModel = {
+ pricing: ko.observable(null),
scenarioName: ko.observable(scenarioName),
drinkType: ko.observable("Regular"),
drinkSize: ko.observable("Tall"),
@@ -45,7 +46,16 @@ sgs.model.coffeeconsumption.initializeViewModel = function (pageSettings, scenar
// Stub implementation to be fully implemented later on
viewModel.costPerWeek = ko.dependentObservable(function() {
- var result = 0;
+ // Get the base price
+ if (this.pricing() == null) {
+ return 0;
+ }
+
+ var basePrice = this.pricing().getCoffeeBeveragePrice(this.drinkType(), this.drinkSize());
+ var dailyCost = basePrice * this.drinksPerDay();
+ var weeklyCost = dailyCost * this.drinkDaysPerWeek();
+ var result = Math.round(weeklyCost * 100) / 100;
+
return result;
}, viewModel);
@@ -14,13 +14,15 @@ sgs.model.consumptionscenarios.initializeViewModel = function (pageSettings) {
var proposed = sgs.model.coffeeconsumption.initializeViewModel (pageSettings, "Proposed");
var viewModel = {
+ pricing: ko.observable(null),
currentConsumption: ko.observable(current),
proposedConsumption: ko.observable(proposed)
};
// Stub implementation to be fully implemented later on
viewModel.savingsPerWeek = ko.dependentObservable(function() {
- var result = 0;
+ var costDifference = this.currentConsumption().costPerWeek() - this.proposedConsumption().costPerWeek();
+ var result = Math.round(costDifference * 100) / 100;
return result;
}, viewModel);
@@ -30,5 +32,10 @@ sgs.model.consumptionscenarios.initializeViewModel = function (pageSettings) {
return result;
}, viewModel);
+ viewModel.pricing.subscribe(function(newPricing) {
+ viewModel.currentConsumption().pricing(newPricing);
+ viewModel.proposedConsumption().pricing(newPricing);
+ });
+
return viewModel;
}

0 comments on commit ffb4755

Please sign in to comment.