2
votes

I'm using Ryan Niemeyer's Dirty Flag. An example of his method can be seen in this jsFiddle.

He has a dirtyItems method on the ViewModel

this.dirtyItems = ko.computed(function() {
  return ko.utils.arrayFilter(this.items(), function(item) {
    return item.dirtyFlag.isDirty();
  });
}, this);

However, I can't seem to get his dirtyItems method working with my data.The biggest difference is that I am using the mapping plugin. Everything I have tried comes back empty.

Here is a jsFiddle of my implementation.

1
What level do you want your flag to be at? Do you want it to be per Category or per Profile Property? - RP Niemeyer
Maybe I wrongly assumed it was set on any observable (like ProfilePropertyValue), and any change would propagate it up so that every parent level would also show as dirty. If it has to be one or the other, then i want it to be set at the ProfilePropertyValue level. - Brad Bamford

1 Answers

3
votes

You could add the dirtyFlag to the ProfilePropertyValue like:

var mappingOptions = {
    ProfilePropertyValue: {
        create: function (mappingoptions) {
            var data = mappingoptions.data;
            data.ProfilePropertyValue = data.ProfilePropertyValue || {
                "ID": null,
                    "Checkbox": ko.observable(false)
            };

            var result = ko.mapping.fromJS(data);
            result.dirtyFlag = ko.dirtyFlag(result);

            return result;
        }
    }
};

Then, you would need to check it when building your list of dirty items, if that is something that you need.

Sample: http://jsfiddle.net/rniemeyer/7DGfs/