Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github
Last active December 16, 2015 03:38
Show Gist options
  • Save beyond-code-github/5371000 to your computer and use it in GitHub Desktop.
Save beyond-code-github/5371000 to your computer and use it in GitHub Desktop.
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