0

I've been writing an app with the kogrid, recently I changed my datasource from an array of objects to an array of knockout objects. However, to my surprise when I update the observable properties within my objects the grid is not updated.

Here is my data array:

self.gridData = ko.observableArray([
    { name: ko.observable("joe"), age: ko.observable(5) }
]);

when I update the age property nothing happens on the grid:

self.gridData()[0].age(6);

does anyone have a good answer for why this is?

Update

I've answered the question below, but does anyone know why the kogrid would be caching the unwrapped values?

Community
  • 1
  • 1
cwohlman
  • 763
  • 6
  • 21
  • Isn't gridData a field? I've never actually used knockout, but you seem to be calling it like a function. – mcwayliffe Feb 18 '14 at 15:59
  • 2
    @rogue_js that bit of syntax is actually fine, it's Knockout's way of retrieving an observable's value. – Jeroen Feb 18 '14 at 16:20
  • Can you include some more info on how you wire everything together, possibly create a repro? The code you posted looks just fine. – Jeroen Feb 18 '14 at 16:22
  • @Jeroen Does my answer below help the question make more sense? Do you know why the values should be cached? – cwohlman Feb 18 '14 at 17:00
  • Well, caching is often for performance, but a proper feature in any case. If the cache isn't being invalidated correctly that may be a bug in kogrid, but the bug can also be in your code. Impossible to tell without a repro :-) – Jeroen Feb 18 '14 at 17:54

1 Answers1

0

I looked into the kogrid source and found this line in src/classes/row.js

self.getProperty = function (path) {
    return self.propertyCache[path] || (self.propertyCache[path] = window.kg.utils.evalProperty(self.entity, path));
};

it looks like the property cache is caching the unwrapped value of the property we're accessing in the default cell template:

<div data-bind="attr: { 'class': 'kgCellText colt' + $index()}, html: $data.getProperty($parent)"></div>

(Note: $data in the template is the column, which has a getProperty wrapper for row.getProperty)

I simply removed the line to cache property values like this:

self.getProperty = function (path) {
    return window.kg.utils.evalProperty(self.entity, path);
};
cwohlman
  • 763
  • 6
  • 21