Update Episerver UI from Dojo Command?



I have added a new option in the epi.cms.publishmenu and that works just fine. When the user clicks the menu option I show a confirm dialog and if the user clicks the Publish button in the dialog I publish the page and try to update the UI.

I found this earlier poat from which I tried to reuse code:

The thing is that it works the second time I click the link. The first time the "Changes to be publish" is still there and if I press my option one more time it disappears and the page looks like it should with nothing to publish.
If I reresh the page after the first time it also looks like it should, no changes to publish.

this is my code:


function (declare, topic, Deferred, dependency, _ChangeContentStatus, dialogService, XhrWrapper, ContentActionSupport, ContentViewModel, resources, LanguageSelector) {

    return declare([_ChangeContentStatus], {

        name: "PublishAndMark",
        label: resources.dojo.publishandmark,
        iconClass: "epi-MyIcon",
        canExecute: true,
        action: ContentActionSupport.saveAction.Publish,
        forceReload: true,

        postscript: function () {

            this._dialogService = this._dialogService || dialogService;
            this._pageDataStore = dependency.resolve("epi.storeregistry").get("epi.cms.contentdata");
            this.projectService = dependency.resolve("epi.cms.ProjectService");

        _execute: function () {

            var self = this;
            //var args = arguments;
            var languageDialog = new LanguageSelector(self.model.contentData.contentLink, self.model.languageContext.language);

            var confirmation = this._dialogService.confirmation({
                description: resources.dojo.confirmation,
                dialogClass: "epi-dialog-smartPublish",
                content: languageDialog,
                title: resources.dojo.chooselanguage,
                cancelActionText: epi.resources.action.cancel,
                confirmActionText: epi.resources.action.publish,
                setFocusOnConfirmButton: true,

            var deferred = new Deferred();
            confirmation.then(function () {

                var contentData = self.model.contentData;

                var viewModel = new ContentViewModel({
                    contentLink: contentData.contentLink,
                    contextTypeName: self.model.contextTypeName

                viewModel.set("contentData", contentData);
                viewModel.set("languageContext", self.model.languageContext);

                if (languageDialog.selectedLanguages.length > 0) {
                    var xhr = new XhrWrapper();
                        url: "/MyUrl",
                        handleAs: "json",
                        content: {
                            contentId: self.model.contentData.contentLink,
                            sourceLanguage: self.model.languageContext.language,
                            targetLanguages: languageDialog.selectedLanguages,
                            includeBlock: languageDialog.includeBlockInContentAreas
                        failOk: true,
                        load: function (response) {
                        error: function (response) {

                topic.publish("/epi/shell/context/request", {
                    uri: "epi.cms.contentdata:///" + contentData.contentLink
                }, {
                    sender: self,
                    viewName: self.view,
                    forceContextChange: true,
                    forceReload: true

        _onModelChange: function () {


            var isPublishCommandAvailable = this.model.contentData.transitions.filter(function (transition) {
                return transition.name === "publish";
            }).length !== 0;

            this.projectService.getCurrentProjectId().then(function (isProjectActive) {
                if (isProjectActive || !this.model) {
                    this.set("isAvailable", false);
                } else {
                    var contentData = this.model.contentData || this.model;
                    var hasPublishRights = ContentActionSupport.hasAccess(contentData.accessMask, ContentActionSupport.accessLevel.Publish);
                    this.set("isAvailable", hasPublishRights && isPublishCommandAvailable);

What am I missing to get this to work the first time?



Edited, Jan 14, 2021 8:09
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.