Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jan 14, 2015 @ 08:59
    Dave Woestenborghs
    0

    Check if item is dirty in property editor

    Does anybody know how to check for unsaved changes in a content item from a property editor? I need to disable some functionality when there are unsaved changes.

    Dave

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 14, 2015 @ 10:16
    Sören Deger
    0

    Hi Dave,

    with this you can set the state manually in your controller to saved:

    $scope.formName.$dirty = false;

    It's not exact this what you need, but perhaps this can help you.

     

    Best 
    Sören 

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jan 14, 2015 @ 10:44
    Dave Woestenborghs
    0

    Hi Sören,

    I have a property editor with a button in it. Button should be disabled if there are unsaved changes in other properties in the content item.

    So i need to know if the item is dirty.

    Dave

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 14, 2015 @ 10:52
    Sören Deger
    0

    Hi Dave,

    I have not tested it yet, but it should work something like this:

    <button ng-show='$dirty==false' />

    or

    <button ng-show='formName.$dirty==false' />

     

    Sören

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jan 15, 2015 @ 08:30
    Dave Woestenborghs
    106

    I just found out how to do it. I added this code to my button click handler

    // get the content item form
    var contentForm = angular.element('form[name=contentForm]').scope().contentForm;
    
    // if we have a dirty property show a notification and cancel sending 
    if (contentForm.$dirty) {
    notificationsService.error('You have unsaved changes');
    return false;
    }   

    Dave

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 15, 2015 @ 19:09
    Sören Deger
    0

    Hi Dave,

    great to hear this! And thank you for sharing your code :-)


    Sören 

  • Bjarne Fyrstenborg 1286 posts 4060 karma points MVP 8x c-trib
    Mar 10, 2015 @ 21:59
    Bjarne Fyrstenborg
    3

    Just a comment in relation to this. If you need to set $dirty property to true, you can also use $setDirty()
    https://docs.angularjs.org/api/ng/type/form.FormController#$setDirty 

    So you can use:

    var contentForm = angular.element('form[name=contentForm]').scope().contentForm;
    contentForm.$dirty = true;

    or

    var contentForm = angular.element('form[name=contentForm]').scope().contentForm;
    contentForm.$setDirty();

    but this didn't change to dirty state for <ng-form name="propertyForm"> which still had class="ng-pristine ng-valid"

    so I had a look at how it is done in the core: https://github.com/umbraco/Umbraco-CMS/blob/2834ccdc2b057619178b3df49aa05ae938faca15/src/Umbraco.Web.UI.Client/src/views/propertyeditors/rte/rte.controller.js

    and came up with this:

    angular.module("umbraco").controller("Our.SwitcherController", function ($scope, $timeout, angularHelper) {
    
        var alreadyDirty = false;
        ...
    
        if (!alreadyDirty) {
            var currForm = angularHelper.getCurrentForm($scope);
            currForm.$setDirty();
            alreadyDirty = true;
        }
    
    });


    /Bjarne

  • Thomas 319 posts 606 karma points c-trib
    Oct 01, 2020 @ 08:32
    Thomas
    0

    Umbraco 8.7.0

    I have this where I reuse a Rich text editor..

    <ng-form id="commentform" name="commentForm">
                    <umb-property-editor model="rte"></umb-property-editor>
                </ng-form>
    

    This return null ...

    var currForm = angularHelper.getCurrentForm($scope);
    

    The same with this:

    var contentForm = angular.element('form[name=commentForm]').scope().contentForm;
    

    Can you see what i'm doing wrong..

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies