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

  • SEARCH
  • Home
  • 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 9115389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:25:13+00:00 2026-06-17T04:25:13+00:00

I am running into problem where i subscribe to propertyChanged event, the subscribed event

  • 0

I am running into problem where i subscribe to propertyChanged event, the subscribed event does fire for entity Modification, but never fires for when setting entity to Deleted.
what might i be doing wrong.

The objective of what i am doing is that, whenever user modifies the row, i want to provide
button at row level to cancel the changes. similarly when user deletes a row, i want to provide a button to unDelete a row. The modification part works as expected, but for Delete it is not working.

I was Expecting that item.entityAspect.setDeleted(), would fire the propertyChanged Event
so that i can update the vlaue of observable IsDeleted,which in turn would update the visibility of the button.

ViewModel

    /// <reference path="jquery-1.8.3.js" />
/// <reference path="../linq-vsdoc.js" />
/// <reference path="../linq.min.js" />
/// <reference path="../breeze.intellisense.js" />
/// <reference path="../breeze.debug.js" />

$(document).ready(function () {

    //extend country type
    var Country = function () {
        console.log("Country initialized");
        var self = this;
        self.Country_ID = ko.observable(0); // default FirstName
        self.Country_Code = ko.observable("");  // default LastName
        self.Country_Name = ko.observable("");
        self.entityState = ko.observable("");
        self.hasValidationErrors = ko.observable(false);
        self.IsDeleted = ko.observable(false);
        self.IsModified = ko.observable(false);
        self.templateName = ko.observable("AlwayEditable");

        var onChange = function () {
            var hasError = self.entityAspect.getValidationErrors().length > 0;
            if (hasError)
                self.hasValidationErrors(true);
            else
                self.hasValidationErrors(false);

        };

        //dummy property to wireup event
        //should not be used for any other purpose
        self.hasError = ko.computed(
                             {
                                 read: function () {
                                     self.entityAspect // ... and when errors collection changes
                                             .validationErrorsChanged.subscribe(onChange);
                                 },

                                 // required because entityAspect property will not be available till Query
                                 // return some data
                                 deferEvaluation: true

                             });

        //dummy property to wireupEvent and updated self.entityStateChanged property
        self.entityStateChanged = ko.computed({
            read: function () {
                self.entityAspect.propertyChanged.subscribe(function (changeArgs) {

                    if (changeArgs.entity.entityAspect.entityState.name == "Deleted") {
                        self.IsDeleted(false);
                    }
                    else if (changeArgs.entity.entityAspect.entityState.name == "Modified")
                        self.IsModified(true);

                }); //subscribe

            },
            deferEvaluation: true,
            // self.entityStateChanged(false)

        });



        self.fullName = ko.computed(
                         function () {
                             return self.Country_Code() + " --- " + self.Country_Name();
                         });
    };

    manager.metadataStore.registerEntityTypeCtor("Country", Country);


    var countryViewModel = function (manager) {
        var self = this;
        window.viewModel = self;

        self.list = ko.observableArray([]);
        self.pageSize = ko.observable(2);
        self.pageIndex = ko.observable(0);
        self.selectedItem = ko.observable();
        self.hasChanges = ko.observable(false);
        self.totalRows = ko.observable(0);
        self.totalServerRows = ko.observable(0);
        self.RowsModified = ko.observable(false);
        self.RowsAdded = ko.observable(false);
        self.RowsDeleted = ko.observable(false);


        self.templateToUse = function (dataItem, context) {
            var item = dataItem;
            if (!_itemTemplate) {
                _itemTemplate = ko.computed(function (item) {
                    //var x = this;
                    if (this.entityAspect == "undefined")
                        return this.templateName("AlwayEditable");
                    if (this.entityAspect.entityState.name == "Deleted") {
                        this.templateName("readOnlyTmpl");
                        return this.templateName();
                    }
                    else {
                        this.templateName("AlwayEditable");
                        return this.templateName();
                    }
                }, item);

            }
            if (item.entityAspect.entityState.name == "Deleted") {
                item.templateName("readOnlyTmpl");
                return item.templateName();
            }
            else {
                item.templateName("AlwayEditable");
                return item.templateName();
            }
            // return _itemTemplate();
        }

        var _itemTemplate;


        self.hasError = ko.computed(
                           {
                               read: function () {
                                   self.entityAspect // ... and when errors collection changes
                                           .validationErrorsChanged.subscribe(onChange);
                               },

                               // required because entityAspect property will not be available till Query
                               // return some data
                               deferEvaluation: true

                           });



        self.acceptChanges = function (item) {
            // self.selectedItem().entityAspect.acceptChanges();
            self.selectedItem(null);
        }


        manager.hasChanges.subscribe(function (newvalue) {
            self.hasChanges(newvalue.hasChanges);

        });

        self.edit = function (item, element) {
            highlightRow(element.currentTarget, item);
            self.selectedItem(item);
        };

        self.discardChanges = function () {
            manager.rejectChanges();
            manager.clear();
            self.pageIndex(0);
            self.loadData();
        };

        self.cancel = function (item, element) {
            item.entityAspect.rejectChanges();
            self.selectedItem(null);

        };

        self.add = function () {
            var countryType = manager.metadataStore.getEntityType("Country"); // [1]
            var newCountry = countryType.createEntity(); // [2]

            //if not using this line, the table is not updated to show this newly added item
            self.list.push(newCountry);

            manager.addEntity(newCountry); // [3]

            self.selectedItem(newCountry);

        };


        self.remove = function (item) {
            item.entityAspect.rejectChanges();
            item.entityAspect.setDeleted(); //was expecting that propertychaged subscribe event will fire, but it does not
            item.templateName("readOnlyTmpl"); //if i don't do this the template is not changed/updated
            item.IsDeleted(true); //have to use this

        };

        self.UndoDelete = function (item) {
            item.entityAspect.rejectChanges();
            item.templateName("AlwayEditable");
            item.IsDeleted(false);

        };

        self.save = function () {

            if (manager.hasChanges()) {
                alertTimerId = setTimeout(function () {
                    //this works as well
                    $.blockUI({ message: '<img src="Images/360.gif" /> </p><h1>Please Saving Changes</h1>', css: { width: '275px' } });
                }, 700);


                manager.saveChanges()
                    .then(saveSucceeded(alertTimerId))
                    .fail(saveFailed);
            } else {
                $.pnotify({
                    title: 'Save Changes',
                    text: "Nothing to save"
                });
                // alert("Nothing to save");
            };

        };


        manager.hasChanges.subscribe(function (newvalue) {
            self.hasChanges(newvalue.hasChanges);
        });

        manager.entityChanged.subscribe(function (changeArg) {

            self.RowsDeleted(manager.getEntities(null, [breeze.EntityState.Deleted]).length);
            self.RowsModified(manager.getEntities(null, [breeze.EntityState.Modified]).length);
            self.RowsAdded(manager.getEntities(null, [breeze.EntityState.Added]).length);


        });

        //we want maxPageIndex to be recalculated as soon as totalRows or pageSize changes
        self.maxPageIndex = ko.dependentObservable(function () {
            return Math.ceil(self.totalRows() / self.pageSize()) - 1;
            //return Math.ceil(self.list().length / self.pageSize()) - 1;
        });
        self.previousPage = function () {
            if (self.pageIndex() > 1) {
                self.pageIndex(self.pageIndex() - 1);
                //self.loadData();
                getData();
            }
        };
        self.nextPage = function () {
            if (self.pageIndex() < self.maxPageIndex()) {
                self.pageIndex(self.pageIndex() + 1);
                // self.loadData();
                getData();
            }

        };
        self.allPages = ko.dependentObservable(function () {
            var pages = [];
            for (i = 0; i <= self.maxPageIndex() ; i++) {
                pages.push({ pageNumber: (i + 1) });
            }
            return pages;
        });
        self.moveToPage = function (index) {
            self.pageIndex(index);
            //self.loadData();
            getData();
        };
    };


    // self.loadData
    var vm = new countryViewModel(manager);

    //ko.validation.group(vm);
    ko.applyBindings(vm);
    // ko.applyBindingsWithValidation(vm);
    vm.loadData();
    try {

    } catch (e) {

        displayModalMessage("Page Error :- Reload the Page", e.message);

    }
}); //end document.ready

View

<p><a class="btn btn-primary" data-bind="click: $root.add" href="#" title="Add New Country"><i class="icon-plus"></i> Add Country</a></p>

    <span> Search Country Code :</span><input id="txtSearch" type="text" /><input id="BtnSearch" type="button" value="Search" data-bind="click: $root.loadData" />
<!--<table class="table table-striped table-bordered " style="width: 700px">-->

<!--<table id="myTable" class="ui-widget" style="width: 800px">-->
    <table id="myTable" class="table table-striped table-bordered " style="width: 1200px">
    <caption> <div>
        <span class="label label-info">Number of Rows Added </span>   <span class="badge badge-info" data-bind="text: RowsAdded"></span> ,  
        <span class="label label-success">Number of Rows Modified</span>  <span  class="badge badge-success" data-bind="text: RowsModified"></span>  ,  
        <span class="label label-important">Number of Rows Deleted</span> <span class="badge badge-important" data-bind="text: RowsDeleted"></span> 
        <p/>
    </div></caption>
    <thead class="ui-widget-header">
        <tr>
            <th>Code</th>
            <th>Name</th>
            <th>Full Name</th>
            <th />
        </tr>
    </thead>
   <!--<tbody data-bind=" title:ko.computed(function() { debugger; }), template:{name:templateToUse, foreach: list, afterRender: HighlightRows }" class="ui-widget-content"></tbody>-->
       <tbody data-bind=" title:ko.computed(function() { debugger; }), template:{name:templateToUse, foreach: list}" ></tbody>
</table>

<div class="pagination">
    <ul><li data-bind="css: { disabled: pageIndex() === 0 }"><a href="#" data-bind="click: previousPage">Previous</a></li></ul>
    <ul data-bind="foreach: allPages">
        <li data-bind="css: { active: $data.pageNumber === ($root.pageIndex() + 1) }"><a href="#" data-bind="text: $data.pageNumber, click: function() { $root.moveToPage($data.pageNumber-1); }"></a></li>
    </ul>
    <ul><li data-bind="css: { disabled: pageIndex() === maxPageIndex() }"><a href="#" data-bind="click: nextPage">Next</a></li></ul>
</div>

    <!--<input id="Button1" type="button" value="Save"  data-bind="attr: { disabled: !hasChanges()}, click:saveChanges" />-->
    <a class="btn btn-success btn-primary" data-bind="click: $root.save, css: { disabled: !$root.hasChanges()}" href="#" title="Save Changes"> Save Changes</a>
   <!-- <input id="Button3" type="button" value="Create New"  data-bind="click:AddNewCountry" /> 
    <input id="Button4" type="button" value="Discard and reload data"  data-bind="click:discardreload, attr: { disabled: !hasChanges()}" /> -->
    <a class="btn btn-danger btn-primary" data-bind="click: $root.discardChanges, css: { disabled: !$root.hasChanges()}" href="#" title="Discard Changes"><i class="icon-refresh"></i> Discard & Reload</a>

    <script id="readOnlyTmpl" type="text/html">
   <tr >

       <td>
            <span class="label "  data-bind="text: Country_Code "></span>

                <div data-bind="if: hasValidationErrors">
                    <span class="label label-important" data-bind="text: $data.entityAspect.getValidationErrors('Country_Code')[0].errorMessage ">Important</span>
              </div>

       </td>
       <td>
            <span class="label "  data-bind="text: Country_Name "></span>

           <p data-bind="validationMessage: Country_Name"></p>
                <span data-bind='visible: ko.computed(function() { debugger; }), text: Country_Name.validationMessage'> </span>
       </td>
             <td> <span class="label "  data-bind="text: fullName "></span>

             </td>
        <td >



            <a class="btn btn-danger" data-bind="click: $root.cancel,  visible: $data.IsModified" href="#" title="cancel/undo changes">Undo Changes<i class="icon-trash"></i></a>

            <a class="btn btn-danger" data-bind="click: $root.remove, visible: !$data.IsDeleted() " href="#" title="Delete this Row">Delete<i class="icon-remove"></i></a>

             <a class="btn btn-danger" data-bind="click: $root.UndoDelete, visible: $data.IsDeleted() " href="#" title="Undo Delete">Un Delete<i class="icon-remove"></i></a>

     </td>
    </tr>
</script>


    <script id="AlwayEditable" type="text/html">
         <tr >
       <td><input type="text" placeholder="Country Code" data-bind="value: Country_Code , uniqueName: true, css: { error: hasValidationErrors }, valueUpdate: 'afterkeydown'"/>

          <!--  <div data-bind="if: $data.entityAspect.getValidationErrors().length>0">
             <pre data-bind="text:  $data.entityAspect.getValidationErrors('Country_Code')[0].errorMessage "></pre>

        </div>-->

                <div data-bind="if: hasValidationErrors">
                    <span class="label label-important" data-bind="text: $data.entityAspect.getValidationErrors('Country_Code')[0].errorMessage ">Important</span>

      </div>

       </td>
       <td><input type="text" placeholder="Country Name" data-bind="value: Country_Name, uniqueName: true, valueUpdate: 'afterkeydown'"/>
           <p data-bind="validationMessage: Country_Name"></p>
                <span data-bind='visible: ko.computed(function() { debugger; }), text: Country_Name.validationMessage'> </span>
       </td>
             <td>
                 <span data-bind=' text: fullName'> </span>
             </td>
        <td >



            <a class="btn btn-danger" data-bind="click: $root.cancel,  visible: $data.IsModified" href="#" title="cancel/undo changes">Undo Changes<i class="icon-trash"></i></a>

            <a class="btn btn-danger" data-bind="click: $root.remove, visible: !$data.IsDeleted() " href="#" title="Delete this Row">Delete<i class="icon-remove"></i></a>

             <a class="btn btn-danger" data-bind="click: $root.UndoDelete, visible: $data.IsDeleted() " href="#" title="Undo Delete">Un Delete<i class="icon-remove"></i></a>


        </td>
   </tr>
    </script>
  • 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-17T04:25:14+00:00Added an answer on June 17, 2026 at 4:25 am

    Analysis

    The propertyChanged event is raised when … a property changes. But that’s not what you want to watch. You want to monitor the entityAspect.entityState

    When you set a property to a new value (for example person.FirstName("Naunihal")), you get both a propertyChanged event and a change to the entity’s EntityState.

    When you delete the entity, the entity’s EntityState changes … to “Deleted”. But deleting doesn’t change a property of the entity. Breeze does not consider the EntityState itself to be a property of the entity. Therefore, there is no propertyChanged notification.

    Solution

    Update Jan 12, 2013
    I think more people will discover this solution if I rephrase the question that you asked so people understand that you want to listen for changes to EntityState.

    So I moved my answer to a new SO question: “How can I detect a change to an entity’s EntityState?“. Hope you don’t mind following that link.

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

Sidebar

Related Questions

I'm running into a problem when trying to create an ArrayList in Java, but
I've written a simple django app to test ImageField, but I'm running into problem
I'm running into a problem because my database has BIGINT data (64-bit integers) but
Running into a problem. I have a table defined to hold the values of
I'm running into a problem inserting rows from a file with 13,000 rows into
I'm running into a problem when trying to select records from my 2005 MS-SQL
I am running into a problem that just doesn't seem right. I've got a
I'm running into the problem of users being able to submit data with '
I'm running into this problem when trying to call a SOAP Web Service from
My company is running into a problem with a web service that is written

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.