I have following Knockout.js object:
var viewModel = {
description : ko.observable(""),
Name : ko.observable(""),
productid : ko.observable(""),
productmodel : ko.observable(""),
productnumber : ko.observable(""),
text_relevance : ko.observable(""),
mydunamicfield : ko.computed(function() {
return "bq=(and " +
((this.description == "") ? "" : ("description:" + this.description + " ")) +
")";
} , this)
};
But the mydunamicfield
property isn't producing the the correct concatenated result. If I try to reference this.description()
inside another function, I see the following error message when the page is loading:
Property 'description' of object [object Window] is not a function
What is the problem in this case?
Creating an Observable Variable - var myObservable = ko. observable(); myObservable('Hello'); alert(myObservable()); Assign ko. observable function to a variable. Knockout then converts the variable into a function and tracks when the value changes to notify the UI elements associated with the variable.
Computed Observable is a function which is dependent on one or more Observables and automatically updates whenever its underlying Observables (dependencies) change.
Knockout. js defines an important role when we want to detect and respond to changes on one object, we uses the observable. An observable is useful in various scenarios where we are displaying or editing multiple values and require repeated sections of the UI to appear and disappear as items are inserted and deleted.
applyBindings do, The first parameter says what view model object you want to use with the declarative bindings it activates. Optionally, you can pass a second parameter to define which part of the document you want to search for data-bind attributes. For example, ko.
Firstly, you must reference this.description
as this.description()
if you want to get its value.
Secondly, try putting your computed
field outside your viewModel
(as 'this'
which is the viewModel
itself isn't defined at the point you create the computed
observable.
See http://jsfiddle.net/rAEqK/2/ for a working example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With