0
self.work_days = ko.observableArray();

self.work_days().push(new WorkDayVM({}, new_date))//new_date is the date supplied from the form 

function WorkDayVM(data, day) {
   var self = this;
   self.in_time1 = ko.observable();
   self.out_time1 = ko.observable();
   self.in_time2 = ko.observable();
   self.out_time2 = ko.observable();
   self.work_time = ko.computed(function () {
     var in_time1_raw = self.in_time1();
     var out_time1_raw = self.out_time1();
     var in_time2_raw = self.in_time2();
     var out_time2_raw = self.out_time2();

     if(!in_time1_raw || !out_time1_raw || !in_time2_raw || !out_time2_raw)
                return;
     var t1 = get_minutes(in_time1_raw);
     var t2 = get_minutes(out_time1_raw);
     var t3 = get_minutes(in_time2_raw);
     var t4 = get_minutes(out_time2_raw);
     res = t2 - t1 + t4 - t3;
     return get_hr_m(res);//returns hr:min
  }, this);
}
console.log(self.work_days()[0].work_time); //prints dependentobservable()
console.log(self.work_days()[0].work_time());//prints undefined

I want to get work_time value. How to access that value ?

ebohlman
  • 14,795
  • 5
  • 33
  • 35
Bishnu Bhattarai
  • 2,800
  • 8
  • 36
  • 44
  • Is any data being pushed into `WorkDayVM`? In your example, pushing in an empty `data` object to your `WorkDayVM` would result in the `work_time` computed returning an undefined. – rwisch45 Feb 09 '14 at 05:39

1 Answers1

0

You are already accessing the work_time value correctly.

console.log(self.work_days()[0].work_time()); // prints undefined

The problem is in your WorkDayVM object. It's not storing any data. Your computed observable depends on a number of observables being filled with values, and if they aren't, it returns (undefined). There is nothing in your code that uses the incoming parameters data and new_date, so the observables your computed relies on are never filled.

If you actually use the parameters coming into the WorkDayVM constructor to fill the observables in_time1, in_time2, out_time1 and out_time2, you will see that your console logs something else than undefined, and is actually working.

In either case, I would change the return-statement in your computed observable to return something meaningful, if only null. Returning nothing from a computed observable is kind of bad practise, if you ask me.

Hans Roerdinkholder
  • 3,000
  • 1
  • 20
  • 30