How can I use knockout’s $parent/$root pseudovariables from inside a .computed() observable?

The pseudovariables are only available in the context of data binding. The view model itself ideally should not know about or have any dependencies on the view that is displaying it. So, when adding computed observables in the view model, you have no knowledge of how it will be bound (like what is going to … Read more

Knockout firing click binding on applyBindings

The reason is, that you’re indeed invoking the preview function (because writing functionName means referring to the function, writing functionName() means calling it). So data-bind=”click: Report.preview” would be working as expected, but without handing over the parameter. As the manual states (on a different topic, but this still applies): If you need to pass more … Read more

Call a function on enter key press

No need for a custom binding, just use knockout’s keypress event(Knockout docs): <input type=”text” data-bind=”textInput : keyword, event: {keypress: onEnter}” > </input> And your function: that.onEnter = function(d,e){ e.keyCode === 13 && that.search(); return true; }; JSFiddle example EDIT: New binding from knockout(3.2.0) : textInput – obviates the need to have a valueUpdate binding.

Conditionally add an element attribute in knockout.js

I needed this when selecting an <option> (that I was generating manually instead of built-in knockout). <option data-bind=”text: text, attr:{ value:value, ‘selected’: typeof(selected) !== ‘undefined’ ? selected : null }”> Choice X </option> This says to always update the ‘text’ attribute and to add the ‘value’ attribute, but only add ‘selected’ if the the data … Read more

Detecting change to Knockout view model

Use extenders: ko.extenders.trackChange = function (target, track) { if (track) { target.isDirty = ko.observable(false); target.originalValue = target(); target.setOriginalValue = function(startingValue) { target.originalValue = startingValue; }; target.subscribe(function (newValue) { // use != not !== so numbers will equate naturally target.isDirty(newValue != target.originalValue); }); } return target; }; Then: self.MyProperty= ko.observable(“Property Value”).extend({ trackChange: true }); Now you … Read more

Example of knockoutjs pattern for multi-view applications [closed]

There are a few directions that you could go with this one. One option is to call ko.applyBindings with distinct view models against separate DOM elements like: var viewModelA = { name: “Bob” }; var viewModelB = { price: 50 }; ko.applyBindings(viewModelA, document.getElementById(“aContainer”)); ko.applyBindings(viewModelB, document.getElementById(“bContainer”)); http://jsfiddle.net/9abgxn8k/ In this case, you would want to make sure … Read more

Why does knockout.js have a reputation for being better for small projects, backbone.js for big?

From my (short) comparison of Knockout and Backbone: Knockout aims to provide slick, easy to use model bindings between the HTML and Model. It’s very XAML/Silverlight/WPF like in it’s implementation and usage patterns (this makes sense considering where it came from). Knockout does not provide guidance or constructs beyond the model, though. It’s up to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)