9

I'm trying to use client settings in Alchemy to enable/disable some command (RibbonToolbarExtension), but I don't know how can I use the "getSettings" service, because is an asynchronous service, that provides the value with a promise, but the isEnabled function gets executed before the promise is returned.

The code we are using is something like:

    isEnabled: function (selection) {
    // see configuration to decide
    var p = selection.properties;

    Alchemy.Plugins["${PluginName}"].Api.getSettings()
        .success(function (settings) {
            //alert("settings are: " + JSON.stringify(settings));
            console.log(settings);
            for (var i = 0; i < settings.AllowedPublications.length; i++) {
                var idPub = settings.AllowedPublications[i].IdPub;
                if (p.parentItemUri == "tcm:0-" + idPub + "-1") {
                    // true when the publication is configured
                    return true;
                }
            }
        }).error(function () {
            console.log("error reading settings");
        });

    return false;
},

I am not able to use the returned value "true" of the settings logic, before the isenabled "return false" is executed.

Is there any example of using client settings to enable/disable commands in Alchemy?

2
  • It should work. I guess the problem could be with the code/logic to return TRUE. What do you get from console.log(settings);, Is it null ? Also Could you put the alert(settings.AllowedPublications.length); just before the for loop, Just to make sure it returns the allowed publications. Jan 13, 2016 at 18:54
  • i haven't got my hands dirty on Alchemy yet, but usual JS trick like recursive timeout till you get the results the allow usual flow.
    – Raj Kumar
    Jan 13, 2016 at 19:07

2 Answers 2

6

The way that I've done this in the past is to create a function separate from the isEnabled function which I use to place the settings in a global variable. I then reference this from inside isEnabled. Due to the issues you outlined I was unable to have the getSettings inside my isEnabled function, though it may be possible with more JS expertise than I have.

I've also written a blog post here outlining exactly what I did to set up one of my plugins to use settings similarly to what you're attempting here. It may be helpful to you on this and any other issues you run into.

1
  • I had tried something similar to the way you comment, but the problem still arise from the fact that the execution of the "isEnabled" function doesn't wait to the execution of the "getSettings" function. Jan 14, 2016 at 1:15
5

Finally I get my code working, based partially in the ideas outlined by Tanner Brine. I use a global setting as commented, and a function to read and assign the settings:

takeSettings: function () {
    Alchemy.Plugins["${PluginName}"].Api.getSettings()
    .success(function (settings) {
        // assign setting to global variable
        globalSettings = settings;

    }).error(function () {
        console.log("error reading settings: " + error.message);
    });

}

but if I use this function inside the isEnabled function, the problem still comes from the asynchronous response.

Then I finally used the "init" function, and everythings works, ok.

init: function () {
    this.takeSettings();
},

Probably it all works ok, due to the time passing between the first init call and the isEnabled execution, but the workaround works for me if there is no better solution.


I found out that calling the "takeSettings()" function inside the "isEnabled()" function also works ok, due to the fact that the "isEnabled" function gets called twice (don't know why), and in the second one, already has the globalSettings set.

isEnabled: function (selection) {
    // call function inside isEnabled
    this.takeSettings();
    console.log("set: "+globalSettings)

    var p = selection.properties;
    var result = false;
    // check if the settings are set
    if (globalSettings.AllowedPublications) {
        for (var i = 0; i < globalSettings.AllowedPublications.length; i++) {
            var idPub = globalSettings.AllowedPublications[i].IdPub;
            if (p.parentItemUri == "tcm:0-" + idPub + "-1") {
                result = true;
            }
        }
    }

    return result;
},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.