Skip to content

Commit

Permalink
Initial code for the optional todo #8 of part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
techarch committed Oct 29, 2011
1 parent d457c80 commit ffb4755
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions scripts/viewmediator/sgs.mediator.consumption-scenarios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}

Expand Down
12 changes: 11 additions & 1 deletion scripts/viewmodel/sgs.model.coffee-consumption.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 8 additions & 1 deletion scripts/viewmodel/sgs.model.consumption-scenarios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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.