Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting value of Observable not updating in Knockout

(There are a ton of questions every day that link back to why can't I set the value of my observable, instead of having so many different answers saying the same thing I wanted to create a question to refer back to for everyone)

Setting value of Knockout Observable / Observable Array doesn't update

Setting the value of my observable observableArray isn't updating!

Adding an item to an Observable Array

Why can't I add an item into my Knockout observable array?

like image 258
PW Kad Avatar asked Oct 15 '13 21:10

PW Kad


People also ask

How do I assign a value to knockout observable?

To create an observable, assign the ko. observable function to the variable. A default value can be specified in the constructor of the call. Knockout then converts your variable into a function and tracks when the value changes, in order to notify the UI elements associated with the variable.

How do you reset an observable array?

To clear an observableArray you can set the value equal to an empty array.

What is observable array in knockout JS?

An observableArray just tracks which objects it holds, and notifies listeners when objects are added or removed.

What is observable array?

ObservableArray is an array that allows listeners to track changes when they occur. In order to track changes, the internal array is encapsulated and there is no direct access available from the outside. Bulk operations are supported but they always do a copy of the data range.


1 Answers

Setting value of Knockout Observable / Observable Array doesn't update

You need to use the setter function to update the value of your observable / observableArray -

Ex. 1 (observable) -

var name = 'John';
var myValue = ko.observable();
myValue(name); // Set myValue equal to John, update any subscribers

var newObservable = ko.observable('Bill');
myValue(newObservable()); // Set myValue equal to the value of newObservable, which is Bill

Ex. 2 (observableArray) -

var names = ['John', 'William', 'Dave'];
var myArray = ko.observableArray();
myArray(names); // Set myArray equal to the array of names John, update any subscribers

var newArray = ko.observableArray(['Sanford']);
myArray(newArray()); // Makes a clone of the array

Note See this Question to understand why this probably not what you are trying to do - What is the best way of cloning/copying an observablearray in knockoutJS?

Adding an item to an Observable Array

You need to push the item into the observableArray, not the underlying value of the observableArray -

var name = 'John';
var myValue = ko.observable(name);
var myArray = ko.observableArray();
myValue.push(myValue()); // Add myValue to my observableArray**

Creating a model to use/share in your view model

You can create a re-usable model to use in your view models. This is similar to using a class in C# to create objects that have common properties.

function objectModel(item) {
    var self = this;
    self.Name = ko.observable(item.name);
    self.Description = ko.observable(item.description);
}

Which can be created like -

var object = {
    name: 'John',
    description: 'a person'
}

var john = new objectModel(object);

Which could also be done by parameters instead of just objects -

function objectModel(name, description) {
    var self = this;
    self.Name = ko.observable(name);
    self.Description = ko.observable(description);
}

var john = new objectModel('John', 'a person');
like image 174
PW Kad Avatar answered Sep 24 '22 00:09

PW Kad