Questions tagged [computed-observable]

A computed observable is a synchronously updated value based on other observables; if they change so does the computer observable, immediately. A term frequently used with Knockout.js.

A familiar computed observable would be a cell in a spreadsheet that contains the sum of a column. If any of the cells in the column change, the sum is immediately updated.

Reference: http://knockoutjs.com/documentation/computedObservables.html

129 questions
81
votes
5 answers

Force a computed property function to run

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…
George Mauer
  • 103,465
  • 117
  • 349
  • 581
55
votes
1 answer

Knockout: computed observable vs function

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; …
Duncan
  • 1,133
  • 2
  • 11
  • 20
42
votes
5 answers

How to create a computed observable array in Knockout

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; …
Matt Mangold
  • 690
  • 1
  • 6
  • 10
17
votes
2 answers

Why does my ko computed observable not update bound UI elements when its value changes?

I'm trying to wrap a cookie in a computed observable (which I'll later turn into a protectedObservable) and I'm having some problems with the computed observable. I was under the opinion that changes to the computed observable would be broadcast to…
Allen Rice
  • 18,062
  • 13
  • 77
  • 111
9
votes
1 answer

Cannot write a value to a ko.computed unless you specify a 'write' option

I am trying to use computed properties in another computed properties and when i run code i am getting following error in console. Cannot write a value to a ko.computed unless you specify a 'write' option function AppViewModel() { var self =…
rahularyansharma
  • 10,835
  • 17
  • 74
  • 127
9
votes
1 answer

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

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…
Gary Zi
  • 95
  • 1
  • 6
7
votes
2 answers

Knockout Observable Array with Sorting and Foreach Data-Binding

I have an array of objects that are hooked up into a knockout observable array. I needed to apply sorting to these arrays and I ran into some behavior that is a bit confusing. My first attempt involved applying the sort in the foreach…
ecozoic
  • 113
  • 1
  • 1
  • 6
6
votes
2 answers

What is difference on ko.computed between deferEvaluation and extend({deferred: true})

I am little bit confused by behavior explanation of deferred calculation defined on ko.computed variable. Such computed variable might be defined with property deferEvaluation: true which should postpone evaluation to the moment when any other…
Jan Stanicek
  • 899
  • 1
  • 9
  • 27
6
votes
2 answers

Why does a function behave like a computed?

Given the following HTML/JS (http://jsfiddle.net/mgs_jsfiddle/gUhm2/)

mgs
  • 3,676
  • 2
  • 15
  • 16
5
votes
5 answers

Prevent computed from collecting dependencies

Is there a good way to roughly achieve the following concept: var computed = ko.computed(function() { readSomeObservables(); //<-- if these change, notify computed ko.stopCollectingDependencies(); readSomeMoreObservables(); //<-- if…
Rex M
  • 135,205
  • 29
  • 270
  • 310
4
votes
2 answers

knockout.js how to set selected option after confirm() cancelled within a ko.computed write

i have a selector element with options and default text: self._selected = ko.observable(); self.option = ko.computed({ read:function(){ return self._selected; }, write: function(data){ if(data){ …
GZH
  • 168
  • 1
  • 5
4
votes
2 answers

How to update an observable in a computed without triggering the computed?

I have a particular knockout observable that's used in many computed functions and it works exactly as it's supposed to. However on one of these computed functions I do NOT want it to trigger when the observable is updated. Code wise it would look…
Nathan
  • 295
  • 2
  • 13
4
votes
2 answers

Angular2 version of knockout's computed observable

I am currently working on porting some code that I have written from Knockout to Angular2. I really like the construct of computed observables in knockout which means the results of that function are only computed when the obeservables it depends…
Gabe O'Leary
  • 1,779
  • 4
  • 21
  • 38
4
votes
1 answer

How to bind property on parent object with knockoutjs?

I'm developing a application with knockout.js framework. I have one viewmodel like that: var MyViewModel= { Id: ko.observable(), CountryCode: ko.observable(), NormalizedAddress: { COUNTRY_CODE: ko.computed(function ()…
ilMattion
  • 1,491
  • 1
  • 15
  • 41
4
votes
2 answers

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

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…
Ryan Wheale
  • 18,754
  • 5
  • 58
  • 83
1
2 3
8 9