logo
down
shadow

Computed variable with properties in KnockoutJS?


Computed variable with properties in KnockoutJS?

By : Reydw
Date : November 22 2020, 10:38 AM
I wish this help you Can a computed variable have properties? , Create a separate model for ScaledRoom:
code :
var ScaledRoom  = function () {
    var self = this;

    self.Width = ko.observable(0);
    self.Height = ko.observable(0);
    self.Depth = ko.observable(0);
}
var ViewModel = function () {
    var self = this;

    self.RoomWidth = ko.observable(0);
    self.RoomHeight = ko.observable(0);
    self.RoomDepth = ko.observable(0);

    self.ScaledRoom = ko.computed(function () {
        var scaledRoom;
        if (!self.ScaledRoom) {
            scaledRoom = new ScaledRoom();
        }
        else {
            scaledRoom = self.ScaledRoom();
        }

        scaledRoom.Width(self.RoomWidth() * 10);
        scaledRoom.Height(self.RoomHeight() * 15);
        scaledRoom.Depth(self.RoomDepth() * 11);

        return scaledRoom;
    });
};
<div>
    Room Width: <input type="text" data-bind="value: RoomWidth"/>
</div>
<div>
    Room Height: <input type="text" data-bind="value: RoomHeight"/>
</div>
<div>
    Room Depth: <input type="text" data-bind="value: RoomDepth"/>
</div>
<div data-bind="with: ScaledRoom">
    <div data-bind="text: Width"></div>
    <div data-bind="text: Height"></div>
    <div data-bind="text: Depth"></div>
</div>


Share : facebook icon twitter icon
Knockoutjs + ko.mapping.fromJS + computed field not calculate computed

Knockoutjs + ko.mapping.fromJS + computed field not calculate computed


By : Austin Reilley
Date : March 29 2020, 07:55 AM
To fix this issue I try to do this: , Try ko.mapping.fromJSON() instead of ko.mapping.fromJS()
Using prototype for KnockoutJS computed properties

Using prototype for KnockoutJS computed properties


By : Shalini
Date : March 29 2020, 07:55 AM
may help you . this is not the Actual viewmodel since the instance is not created yet. You can do
code :
ViewModel = function() {
   this.fullName = ko.computed(this.getFullName, this);
};

ViewModel.prototype = {
   getFullName: function() {
      return this.firstName() + " " + this.lastName();
   }
};
requirejs & knockoutjs ko.computed Pass a function that returns the value of the ko.computed

requirejs & knockoutjs ko.computed Pass a function that returns the value of the ko.computed


By : Joe Roulette
Date : March 29 2020, 07:55 AM
I wish this helpful for you Since Knockout 2.0, dependent observables are called computed observables. So:
code :
self.typeList = ko.dependentObservable();
self.typeList = ko.computed(function(){
   // Compute value here...
   return value;
});
KnockoutJs computed not working with last computed field

KnockoutJs computed not working with last computed field


By : kwlong
Date : March 29 2020, 07:55 AM
it fixes the issue here it is the jsfidle, note the subscribe function that detects any changes in the observable and make the operation, newval is the new value that you input, as the other pal said you cannot do both observable and computed. when changes in a1, a2 happens A is updated, when changes in A and B happens C is updated, also i recommend that you use self for namespacing all the variables. Vote me as acepted answer :)
code :
var ViewModel = function () {
     self = this;
     self.A1 = ko.observable(0);
     self.B1 = ko.observable(0);
     self.A2 = ko.observable(0);
     self.B2 = ko.observable(0);
     self.A = ko.observable(0);
     self.B = ko.observable(0);
     self.C = ko.observable(0);
     self.A1.subscribe(function(newval){
           self.A(parseFloat(newval) + parseFloat(self.A2()))
     });
     self.A2.subscribe(function(newval){
           self.A(parseFloat(self.A1()) + parseFloat(newval));
     });
     self.B1.subscribe(function(newval){
           self.B(parseFloat(newval) + parseFloat(self.B2()))
     });
     self.B2.subscribe(function(newval){
           self.B(parseFloat(self.B1()) + parseFloat(newval));
     });
     self.A.subscribe(function(newval){
           self.C(parseFloat(newval) + parseFloat(self.B()))
     });
     self.B.subscribe(function(newval){
           self.C(parseFloat(self.A()) + parseFloat(newval));
     });

 };
            ko.applyBindings(new ViewModel());
Combining KnockoutJS special properties with computed functions

Combining KnockoutJS special properties with computed functions


By : cb24
Date : March 29 2020, 07:55 AM
it fixes the issue I'm binding a template using a foreach data-binding. Within the template $index returns a value. Also, a ko.computed function I've written returns a number (the length of the array - 1). Both numbers appear to be valid, but when I check to see if they are equal, it always returns false. , You need to call the functions:
code :
<div>
   <pre data-bind="text: ko.toJSON($index(), null, 2)"></pre>
   <pre data-bind="text: ko.toJSON(lastIndex(), null, 2)"></pre>
   <pre data-bind="text: ko.toJSON($index() != lastIndex(), null, 2)"></pre>
</div>
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org