Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between _isEnabled and isEnabled in Anguilla?

I've been following GUI extensions and notice examples use either _isEnabled or isEnabled, without the underscore. Both seem to work to extend or possibly replace existing functionality.

isEnabled

For example, the PowerTools base class (which doesn't seem to "extend" existing functionality) has:

PowerTools.BaseCommand.prototype.isEnabled = function(selection, pipeline)
{
    var p = this.properties;

    if (!p.initialized)
    {
        this.initialize();
    }

    if (!this.isToolConfigured())
    {
        return false;
    }

    if (this.isValidSelection)
    {
        return this.isValidSelection(selection, pipeline);
    }

    return true;
};

A tool can use this base class and declare .isValidSelection, for example:

PowerTools.Commands.CountItems.prototype.isValidSelection = 
                                       function (selection) { ... }

_isEnabled

I see Anguilla uses ._isEnabled for existing functionality (in Chrome's console in numerous places in the code). For example, WhereUsed has:

Tridion.Cme.Commands.WhereUsed.prototype._isAvailable =
                      function WhereUsed$_isAvailable(selection) ...

Private functions?

I'm familiar with a preceding underscore being a naming convention for private variables. Are the _isEnabled and other functions that start with an underscore "private?" If so, then

  • How should we extend (add additional functionality to existing code) these functions?
  • How should we replace (not have existing code run, but have ours run instead as in an "override") these?

I'm assuming the same approach applies to other functions that start with an underscore such as _isAvailable, and _invoke.

like image 366
Alvin Reyes Avatar asked Aug 13 '12 14:08

Alvin Reyes


1 Answers

The following methods are called for a command:

  1. isAvailable
  2. isEnabled
  3. invoke

The base class for all commands - Tridion.Core.Command - has a standard implementation of these methods. For the most part, this default implementation allows for extensions to Commands. They also call the underscore methods (_isAvailable, _isEnabled, and _execute).

I don't know why the CME commands only overwrite the underscore methods. Maybe someone thought it was just easier that way. They should be consider private (or the equivalent of "protected" in C#), so it actually seems like a bad practice to me.

It would be cleaner to implement the proper methods (isAvailable, isEnabled, and invoke) and then call the base implementation using this.callBase. However, you might need to stop the pipeline in this case, or also overwrite the underscore methods, in order to avoid your return value getting overwritten by the default underscore methods. It depends on the command you are implementing or extending.

In short: using the underscore methods is probably bad practice, but the Core implementation does seem to make it harder for you to do it "right". So I'd aim to avoid the underscore methods, but not sweat it if it turns out to be too hard to do so.

P.S. isValidSelection is a PowerTools-only method which separates the common logic that they all need from the logic specific to each command.

like image 53
Peter Kjaer Avatar answered Nov 06 '22 12:11

Peter Kjaer