Knockout isDirty example, using dynamic viewmodule

2019-05-28 01:51发布

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条回答
欢心
2楼-- · 2019-05-28 02:42

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/

查看更多
登录 后发表回答