简体   繁体   English

如何创建脏标志功能

[英]How to create dirty flag functionality

I want to create dirty flag functionality using knockout. 我想使用knockout创建脏标志功能。 I want to enable the save button only if something has changed. 我想只在事情发生变化时启用保存按钮。 My view and my view model is exactly same as example found on knockout js tutorial Loading and Saving data. 我的视图和我的视图模型与在knockout js教程加载和保存数据上找到的示例完全相同。 Link to tutorial 链接到教程

I am following fiddle example posted by Ryan here 我跟随Ryan 在这里发布的小提琴示例

I am not able to understand where to declare below code which he has declared in view model. 我无法理解在视图模型中声明的下面代码的位置。

 this.dirtyFlag = new ko.dirtyFlag(this);

If i take example from knockout tutorial the link which i posted above and i tried like below 如果我从淘汰教程中获取示例我上面发布的链接,我尝试如下

function Task(data) {
this.title = ko.observable(data.title);
this.isDone = ko.observable(data.isDone);
this.dirtyFlag = new ko.dirtyFlag(this);

} }

Binded my view like below 我的观点如下

<button data-bind="click: saveOperation , enable: isDirty" >Save</button>

It gives me error as unable to parse binding isDirty is not defined. 它给我错误,因为无法解析绑定isDirty没有定义。

I am not sure how to go on implementing this. 我不知道如何继续实施这个。

The dirty flag for knockout is already implement in the small library koLite - https://github.com/CodeSeven/kolite . 淘汰赛的脏标志已经在小型库koLite - https://github.com/CodeSeven/kolite

Or here is an example of creating it: http://www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html 或者以下是创建它的示例: http//www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html

Your code has several problems: 您的代码有几个问题:

  1. You are defining the dirtyFlag on your Task function. 您正在Task函数上定义dirtyFlag But you are checking it on the view bound to the viewModel instance. 但是您在绑定到viewModel实例的视图上检查它。

  2. You have to define the dirty flag after you loaded the data or you have to call dirtyFlag().reset() . 您必须加载数据定义脏标志或者必须调用dirtyFlag().reset()

  3. isDirty is a computed. isDirty是一个计算的。 You have to call it with parenthesis. 你必须用括号称它。

The view model looks like: 视图模型如下所示:

function TaskListViewModel() {
    // Data

    function Task(data) {
    this.title = ko.observable(data.title);
    this.isDone = ko.observable(data.isDone);

}

    var self = this;
    self.tasks = ko.observableArray([]);
    self.newTaskText = ko.observable();
    self.incompleteTasks = ko.computed(function() {
        return ko.utils.arrayFilter(self.tasks(), function(task) { return !task.isDone() && !task._destroy });
    });

    this.dirtyFlag = new ko.DirtyFlag(this.isDone);

    // Operations
    self.addTask = function() {
        self.tasks.push(new Task({ title: this.newTaskText() }));
        self.newTaskText("");
    };
    self.removeTask = function(task) { self.tasks.destroy(task) };
    self.save = function() {
        $.ajax("/echo/json/", {
            data: {
                json: ko.toJSON({
                    tasks: this.tasks
                })
            },
            type: "POST",
            dataType: 'json',
            success: function(result) {
                self.dirtyFlag().reset();
                alert(ko.toJSON(result))
            }
        });
    };

     //Load initial state from server, convert it to Task instances, then populate self.tasks
    $.ajax("/echo/json/", {
        data: {
            json: ko.toJSON(fakeData)
        },
        type: "POST",
        dataType: 'json',
        success: function(data) {
            var mappedTasks = $.map(data, function(item) {
                return new Task(item);
            });

            self.tasks(mappedTasks);

            self.dirtyFlag().reset();
        }
    });                               
}

The binding for the cancel button: 取消按钮的绑定:

<button data-bind="enable: dirtyFlag().isDirty()">Cancel</button>

And the working fiddle (a fork of yours) can be found at: http://jsfiddle.net/delixfe/ENZsG/6/ 工作小提琴(你的一个叉子)可以在以下网址找到: http//jsfiddle.net/delixfe/ENZsG/6/

There is also the ko.editables plugin: https://github.com/romanych/ko.editables 还有ko.editables插件: 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM