Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9271939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:45:09+00:00 2026-06-18T15:45:09+00:00

I want to create dirty flag functionality using knockout. I want to enable the

  • 0

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 example found on knockout js tutorial Loading and Saving data. Link to tutorial

I am following fiddle example posted by Ryan here

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.

I am not sure how to go on implementing this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:45:10+00:00Added an answer on June 18, 2026 at 3:45 pm

    Your code has several problems:

    1. You are defining the dirtyFlag on your Task function. But you are checking it on the view bound to the viewModel instance.

    2. You have to define the dirty flag after you loaded the data or you have to call dirtyFlag().reset().

    3. isDirty is a computed. 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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want create an application with animate button? how can i do? after click
I want create module which update list of usb devices automatically (not only mass
i want create multiple search where statement $where_search is a multiple condition from post
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want create a excel with Apache POI in java and I must insert
i want create image animation , i have 50 images with png format now
I want to create a new field (or two) in my table that is
I want to create a Silverlight app to extract and manipulate data from an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.