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

有些话、适合烂在心里 提交于 2019-12-01 16:56:21

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/11936363/whats-the-difference-between-isenabled-and-isenabled-in-anguilla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!