Last active
December 16, 2015 03:38
-
-
Save beyond-code-github/5371000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var koProvider = { | |
observable: function () { | |
return ko.observable(); | |
}, | |
computed: function (func) | |
{ | |
return ko.computed(func); | |
} | |
} | |
var variableProvider = { | |
observable: function () { | |
var variable; | |
return function (val) | |
{ | |
if (val) variable = val; | |
return variable; | |
} | |
}, | |
computed: function (func) | |
{ | |
return function() { | |
return func() | |
}; | |
} | |
} | |
window.ViewModel = {}; | |
window.ViewModel.New = function (provider) { | |
var price = provider.observable(); | |
var discountPercentage = provider.observable(); | |
var init = function(_price, _discountPercentage) | |
{ | |
price(_price); | |
discountPercentage(_discountPercentage); | |
} | |
var getRealPrice = provider.computed(function() { | |
return price() * discountPercentage(); | |
}); | |
// Public members | |
var model = {}; | |
// Properties | |
model.Price = price; | |
model.DiscountPercentage = discountPercentage; | |
// Methods | |
model.init = init; | |
model.getRealPrice = getRealPrice; | |
return model; | |
} | |
// This is now equivilent of C# constructor\initialiser | |
var viewModel = window.ViewModel.New(koProvider) | |
.init(120, 50); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment