Skip to content

Instantly share code, notes, and snippets.

@devdays
Created December 16, 2014 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devdays/b95bf39325e26cd20375 to your computer and use it in GitHub Desktop.
Save devdays/b95bf39325e26cd20375 to your computer and use it in GitHub Desktop.
Object JavaScript - Knockout attribute Bindings
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
.redItem {
color: red;
}
.greenItem {
color: green;
}
</style>
</head>
<body>
<div data-bind="html: someHtml"></div>
<div>And a link <a data-bind="attr: { href: myHref }">#</a></div>
<div data-bind="css: profitStatus, text: currentProfit"></div>
<button data-bind="click: reviseProfit">Revise Profit</button>
<script src="Scripts/knockout-2.2.1.debug.js"></script>
<script>
var viewModel = function () {
this.someHtml = "Here is a <strong>bold</strong> text.";
this.myHref = "#";
this.currentProfit = ko.observable(150000);
this.profitStatus = ko.computed(function () {
return this.currentProfit() < 0 ? "redItem" : "greenItem";
}, this);
this.reviseProfit = function () {
this.currentProfit(-50);
};
};
ko.applyBindings(new viewModel());
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment