computed-observable

Wrapping localStorage in a writeable computed observable fails to bust the cache

孤人 提交于 2019-12-10 15:27:24
问题 I am new to KnockoutJS and am curious to see if this is possible. I am trying to wrap a local storage object in a writeable computed so that I can take advantage of the auto-binding goodness of KnockoutJS. However, the "read" operation doesn't reference any observables - consequently the initial value never gets updated: <select data-bind="foreach: logLevelsArray, value: currentLogLevel"> <option data-bind="attr: { value: $index() }, text: $data"></option> </select> _.extend

Computed stops triggering forever if dependency is inside of false branch statement

心已入冬 提交于 2019-12-04 14:58:24
I'm faced a problem that my computed observable stops triggering after some sequence of dependency changes. Finally I found out the point: if dependency was inside of false branch statement during latest evaluation, computed will not be triggered next time even if condition became true before evaluation finished . Here is a sample: https://jsfiddle.net/sgs218w0/1/ var viewModel = new function(){ var self = this; self.trigger = ko.observable(true); self.fire = function(){ self.trigger(! self.trigger()); }; self.content = function(){ var test = 3; return ko.computed(function(){ alert("triggered!

Create a computed observable for formatted values for a bunch of variables

∥☆過路亽.° 提交于 2019-12-03 17:39:47
问题 I have 3 observable variables in view-model, and want to output to formatted value. However, I don't want to write computed method for each of them since they are identical. What is the best way to reuse the code? Thanks. The code I am going to achieve is: this.formattedPrice = ko.computed({ read: function () { return '$' + this.price().toFixed(2); }, write: function (value) { // Strip out unwanted characters, parse as float, then write the raw data back to the underlying "price" observable

Force a computed property function to run

不问归期 提交于 2019-12-02 17:13:44
Given a computed property vm.checkedValueCount = ko.computed(function(){ var observables = getCurrentValues(); //an array of ko.observable[] return _.filter(observables, function(v) { return v() }).length; }); suppose getCurrentValues() can return different sets of observables which are modified elsewhere in the code (and comes from a more complex structure than an observableArray). I need checkedValueCount to update whenever one of its dependencies change getCurrentValues() returns a different set of observables. The problem is that ko.computed seems to memoize the last returned value and

Kendo DataSource: How to define “Computed” Properties for data read from remote odata source

二次信任 提交于 2019-12-01 11:13:08
Situation: kendo DataSource var ordersDataSource = new kendo.data.DataSource({ type: "odata", transport: { read: { url: "http://localhost/odata.svc/Orders?$expand=OrderDetails" } }, schema: { type: "json", data: function(response){ return response.value; } total: function(response){ return response['odata.count']; } }, serverPaging: true, serverFiltering: true, serverSorting: true }) the json data read from the odata source is like: { odata.metadata: "xxxx", odata.count: "5", value: [ { OrderId: 1, OrderedDate: "2013-02-20", OrderInfoA: "Info A", OrderInfoB: "Info B" OrderDetails: [ {

How to create a computed observable array in Knockout

 ̄綄美尐妖づ 提交于 2019-12-01 02:03:34
I would like to know how to create a computed observable array. In my view model, I have 2 observable arrays, and I would like to have a computed observable array that is simply both arrays combined. function ViewModel() { var self = this; self.listA= ko.observableArray([]); self.listB = ko.observableArray([]); self.masterList= //combine both list A and B This will combine the two arrays and return the combined list. However, it is not a computed observable array (don't know if that is even possible), but a regular computed observable. self.masterList = ko.computed(function() { return this

How to create a computed observable array in Knockout

我们两清 提交于 2019-11-30 21:22:51
问题 I would like to know how to create a computed observable array. In my view model, I have 2 observable arrays, and I would like to have a computed observable array that is simply both arrays combined. function ViewModel() { var self = this; self.listA= ko.observableArray([]); self.listB = ko.observableArray([]); self.masterList= //combine both list A and B 回答1: This will combine the two arrays and return the combined list. However, it is not a computed observable array (don't know if that is

Knockout: computed observable vs function

两盒软妹~` 提交于 2019-11-27 17:57:26
When using knockout, what is the advantage of using read-only computed observables rather than simple functions? Take the following viewmodel constructor and html snippet, for example: ​ var ViewModel = function(){ var self = this; self.someProperty = ko.observable("abc"); self.anotherProperty = ko.observable("xyz"); self.someComputedProperty = function(){ return self.someProperty() + self.anotherProperty(); }; }; <input data-bind="value: someProperty"/> <input data-bind="value: anotherProperty"/> <p data-bind="text: someComputedProperty()"></p> Everything here seems to work as you'd expect,

Knockout: computed observable vs function

不羁的心 提交于 2019-11-26 19:14:00
问题 When using knockout, what is the advantage of using read-only computed observables rather than simple functions? Take the following viewmodel constructor and html snippet, for example: ​ var ViewModel = function(){ var self = this; self.someProperty = ko.observable("abc"); self.anotherProperty = ko.observable("xyz"); self.someComputedProperty = function(){ return self.someProperty() + self.anotherProperty(); }; }; <input data-bind="value: someProperty"/> <input data-bind="value: