Friday, April 3, 2015

Knockout


Knockout Click Binding:

<td>
<a data-bind="text:title,click:$parent.openRecord"/>
</td>

if you use href in "a" tag, the function wont be called.

Inside your view model,

var self = this;
self.openRecord = function (record) { // Write your code here for click operation };


Knockout Dirty flag:


Create new ko object :
ko.dirtyFlag = function (root, isInitiallyDirty) { var result = function () { }, _initialState = ko.observable(ko.toJSON(root)), _isInitiallyDirty = ko.observable(isInitiallyDirty); result.isDirty = ko.computed(function () { return _isInitiallyDirty() || _initialState() !== ko.toJSON(root); }); result.reset = function () { _initialState(ko.toJSON(root)); _isInitiallyDirty(false); }; return result; };


In your view model, you can use the flag like this:
var isDirty = false; //set to true to load your item as Dirty.
  self.dirtyFlag = new ko.dirtyFlag(this, isDirty);

To check for a specific item in your Model as dirty or not,
        self.quantitydirtyFlag = new ko.dirtyFlag(self.quantity, isDirty);

        self.dirtyItems = ko.computed(function () {
        self.isDirty = ko.computed(function () {

            return ko.utils.arrayFilter(self.items(), function (item) {
                return item.dirtyFlag.isDirty();
            });
        }, self);

            return self.dirtyItems().length > 0;

        }, self);


you can check for whether the item is dirty or not by,

record.dirtyFlag.isDirty()
for the specific item,
record.quantitydirtyFlag.isDirty()

Dirty flag original reference here.