What is the best way of cloning/copying an observablearray in knockoutJS?

To clone an observableArray you would want to do: var viewModel = { array1: ko.observableArray([“one”, “two”]), array2: ko.observableArray() }; viewModel.clone = function() { viewModel.array1(viewModel.array2.slice(0)); }; If you want to just do a copy, then you would do: viewModel.array1(viewModel.array2()); The problem with the second example is that the underlying array is the same, so pushing to … 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.

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

Sorting an Observable Array in Knockout

KnockoutJS observable arrays offer a sort function, this makes it relatively easy to sort a databound array in your UI (regardless of the natural order of the data.) data-bind=”foreach: items.sort(function (l, r) { return l.lastName() > r.lastName() ? 1 : -1 })” You should not have to pre-sort/re-sort your source data, if you’re sorting before … Read more

knockoutjs – bindings with parameters triggered on load

I think “Note 2” on this knockout page explains it all: http://knockoutjs.com/documentation/click-binding.html You can avoid the problem by either using: Anonymous functions: <button data-bind=”click: function(data, event) { myFunction(data, event, ‘param1’, ‘param2’) }”>Click me</button> … or … The bind method: <button data-bind=”click: myFunction.bind($data, ‘param1’, ‘param2’)”>Click me</button>

How to prevent Html showing before knockout binding is executed

Here’s a simple trick. Just make your root element initially hidden and set the visible binding to true. <div style=”display: none;” data-bind=”visible: true”> <!– the rest of your stuff –> </div> As it’s rendered, before knockout does its thing, it will be initially hidden. When the bindings are applied, knockout will override the style and … Read more

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