Skip to content

Instantly share code, notes, and snippets.

@kmorcinek
Last active December 14, 2015 19:39
Show Gist options
  • Save kmorcinek/5138476 to your computer and use it in GitHub Desktop.
Save kmorcinek/5138476 to your computer and use it in GitHub Desktop.
Usefull Knockoutjs stuff
ko.dataFor
ko.computed({ //}, viewModel); - second parameter as a context.
viewModel.whoAmI = function() { alert(this.name);}.bind(viewModel);
<script type="text/html" id="person-template">
<pre data-bind="text: ko.toJSON($root, null, 2)"></pre>
click: function() { viewModel.removeGift($data) }
valueHasMutated ()
this.count(this.count() + 1);
data-bind=”if: ...” rerender the content each time, observed value has changed.
Initialize() inside construktor.
do not operate on array element by element
//replace the contents of the observableArray
this.otherItems(newData);
ko.subscribable.fn.logIt
http://www.knockmeout.net/2013/06/knockout-debugging-strategies-plugin.html
ko.observable.fn.toString = function() {
return "observable: " + ko.toJSON(this(), null, 2);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment