How to create dirty flag functionality

前端 未结 3 1471
生来不讨喜
生来不讨喜 2021-01-20 15:23

I want to create dirty flag functionality using knockout. I want to enable the save button only if something has changed. My view and my view model is exactly same as exampl

3条回答
  •  无人及你
    2021-01-20 15:49

    There is also the ko.editables plugin: https://github.com/romanych/ko.editables

    var user = {
        FirstName: ko.observable('Some'),
        LastName: ko.observable('Person'),
        Address: {
            Country: ko.observable('USA'),
            City: ko.observable('Washington')
        }
    };
    ko.editable(user);
    
    user.beginEdit();
    user.FirstName('MyName');
    user.hasChanges();          // returns `true`
    user.commit();
    user.hasChanges();          // returns `false`
    user.Address.Country('Ukraine');
    user.hasChanges();          // returns `true`
    user.rollback();
    user.Address.Country();     // returns 'USA'
    

提交回复
热议问题