unable to get value from ko.observable()

96 views
Skip to first unread message

Lalit Singh

unread,
Jun 28, 2016, 11:47:33 AM6/28/16
to KnockoutJS
Hi guys,

i want to get the values from the ko.observable(). Please find below code which i am using

function sellingPriority(sellingPriority) {
            var self = this,
                _soldDate, _previousvalue;

            self.id = sellingPriority.id;
            self.isUnavailable = ko.observable(!sellingPriority.isAvailable);
            self.name = sellingPriority.name;
           
            self.soldByUser = ko.observable(sellingPriority.soldByUser);
            if (sellingPriority.actual) {
                self.actual = ko.observable(sellingPriority.actual).extend({ numeric: sellingPriority.actual });
            }
            else if (!sellingPriority.actual) {
                self.actual = ko.observable(0);
            }
 self.isDirty = ko.computed(function () {
                return self.actual();
            },this);
}

now i am getting udpated values into Dirty function as below

self.retrieveDirtySellingPriorityIds = function () {
               
                var sellingPriorityIds = [], sellingPriorityList = {},
                    sellingPriority,
                    i, len;

                for (i = 0, len = self.sellingPriorities().length; i < len; i++) {
                    sellingPriority = self.sellingPriorities()[i];

                    if (sellingPriority.isDirty()) {
                        sellingPriorityList.id = sellingPriority.id; // able to get the value of guid, where i am not using ko.observable()
                        sellingPriorityList.actual = sellingPriority.actual; // unable to get the value 
                        sellingPriorityIds.push(sellingPriorityList);
                    }
                }
return sellingPriorityIds
}

Please let me know if there is any solution.

Thanks
Regards,
Lalit Singh

Gunnar Liljas

unread,
Jun 28, 2016, 11:50:34 AM6/28/16
to knock...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lalit Singh

unread,
Jun 28, 2016, 12:06:10 PM6/28/16
to KnockoutJS
HI Gunnar,

I read the link ,there i am not able to see, the point is i am handling the newValue, but i want that value so that i can pass it to my plugin,

below section , where , how to get the value (i.e the red highlighted) , when i debug it shows me lot of properties, and objects associated.

self.retrieveDirtySellingPriorityIds = function () {
               
                var sellingPriorityIds = [], sellingPriorityList = {},
                    sellingPriority,
                    i, len;

                for (i = 0, len = self.sellingPriorities().length; i < len; i++) {
                    sellingPriority = self.sellingPriorities()[i];

                    if (sellingPriority.isDirty()) {
                        sellingPriorityList.id = sellingPriority.id; // able to get the value of guid, where i am not using ko.observable()
                        sellingPriorityList.actual = sellingPriority.actual; // unable to get the value 
                        sellingPriorityIds.push(sellingPriorityList);
                    }
                }
return sellingPriorityIds
}

Gunnar Liljas

unread,
Jun 28, 2016, 12:13:19 PM6/28/16
to knock...@googlegroups.com
"To read the observable’s current value, just call the observable with no parameters. In this example, myViewModel.personName() will return 'Bob', and myViewModel.personAge() will return 123."

To get the value you must call  sellingPriority.actual()

Lalit Singh

unread,
Jun 29, 2016, 1:38:04 AM6/29/16
to KnockoutJS
Thank you Gunnar, It is working now. :)

Lalit Singh

unread,
Jun 30, 2016, 10:33:08 AM6/30/16
to KnockoutJS
Hi Gunnar,

I have one more question, can we get the originalValue (i.e before modification) in Dirty function. The code is same as below. 

Now i am getting the modified value by using sellingPriority.actual(). I want to collect all original values and subtract it from modified. is there any feature. I am using below code

self.retrieveDirtySellingPriorityIds = function () {
                //self.showErrors(true);
                //if (self.isValid()) {
                var sellingPriorityIds = [], sellingPriorityList = {},
                    sellingPriority,
                    i, len;
                var tempSellingPriority;

                    for (i = 0, len = self.sellingPriorities().length; i < len; i++) {
                        sellingPriority = self.sellingPriorities()[i];
                        tempSellingPriority = _pspValues[i]; - this is the array which i populated during the initial call , but i see all latest value gets overridden from original value. i want to compare and subtract the modified value from original value.

                        if (sellingPriority.isDirty()) {
                            if (sellingPriority.id == tempSellingPriority.id) {
                                sellingPriorityList.id = sellingPriority.id;
                                sellingPriorityList.actual = sellingPriority.actual() - parseInt(tempSellingPriority.actual); - this returns me zero (15 minus 15) , it should be actually 15 (modified value)  minus 10 (original value)
                            }
                            sellingPriorityIds.push(sellingPriorityList);
                        }
                    }

Thank you in advance.
Regards,
Lalit Singh

Gunnar Liljas

unread,
Jun 30, 2016, 11:52:13 AM6/30/16
to knock...@googlegroups.com
That depends on how your isDirty extension works. It might be easier to keep a the original value in a separate property.

Lalit Singh

unread,
Jul 1, 2016, 6:13:28 AM7/1/16
to KnockoutJS
Please let me know how to keep original value in separate property

Lalit Singh

unread,
Jul 1, 2016, 11:27:58 AM7/1/16
to KnockoutJS
Gunnar,

would appreciate , if you could help me in how we can specify original value in a separate property.

Thanks
Lalit Singh

Gunnar Liljas

unread,
Jul 1, 2016, 11:51:57 AM7/1/16
to knock...@googlegroups.com
Just store it twice in your object.

obj.theValue(1);
obj.originalValue=1;


Lalit Singh

unread,
Jul 1, 2016, 11:56:34 AM7/1/16
to KnockoutJS
Hi Gunnar,

thank you for your response, it would be great if you could send me sample code or link, where i can get an idea. i am just nearer to my solution.

Gunnar Liljas

unread,
Jul 1, 2016, 6:02:06 PM7/1/16
to knock...@googlegroups.com
I'm not what more to say. Store the initial value in two different properties. Only edit one of them.

/G 
Reply all
Reply to author
Forward
0 new messages