What is the difference between _isEnabled and isEnabled in Anguilla?

I follow GUI extensions, and note examples use either _isEnabled or isEnabled , without underlining. Both seem to be working on extending or possibly replacing existing functionality.

Isenabled

For example, the PowerTools base class (which doesn't seem to extend existing functions) 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; }; 

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

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

_isEnabled

I see that Anguilla uses ._isEnabled for existing functions (in the Chrome console in many places in the code). For example, WhereUsed has:

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

Private functions?

I am familiar with the previous underscore, which is a naming convention for private variables. Are _isEnabled and other functions starting with the underscore character "private"? If so, then

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

I assume the same approach applies to other functions starting with underscores such as _isAvailable and _invoke .

+7
source share
1 answer

The following methods are called for the command:

  • isAvailable
  • Isenabled
  • 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 you to extend commands. They also call underscore methods (_isAvailable, _isEnabled, and _execute).

I don't know why CME commands only overwrite underscore methods. Maybe someone thought it was just easier. They should be considered private (or equivalent β€œprotected” in C #), so this actually seems like bad practice to me.

It would be easier to implement the correct methods (isAvailable, isEnabled and invoke) and then call the underlying implementation using this.callBase. However, in this case, you may need to stop the pipeline or also rewrite the underscore methods to prevent the return value from being replaced with standard underscore methods. It depends on the team you are implementing or expanding.

In short: using underscores is probably bad practice, but the Core implementation seems to make it difficult for you to do this β€œright”. Therefore, I would try to avoid underscore marks, but not sweat if it is too difficult to do.

PS isValidSelection is the only PowerTools method that separates the common logic that they need from the logic specific to each command.

+8
source

All Articles