_isEnabled 和 isEnabled 在安圭拉有什么区别? [英] What's the difference between _isEnabled and isEnabled in Anguilla?

查看:25
本文介绍了_isEnabled 和 isEnabled 在安圭拉有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注 GUI 扩展,并注意到示例使用 _isEnabledisEnabled,没有下划线.两者似乎都可以扩展或可能替换现有功能.

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.

例如,PowerTools 基类(似乎没有扩展"现有功能)具有:

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;
};

一个工具可以使用这个基类并声明.isValidSelection,例如:

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

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

_isEnabled

我看到 Anguilla 将 ._isEnabled 用于现有功能(在 Chrome 控制台的代码中的许多地方).例如,WhereUsed 有:

_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) ...

私有函数?

我熟悉前面的下划线是私有变量的命名约定._isEnabled 和其他以下划线开头的函数是私有的"吗?如果是这样,那么

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?

我假设相同的方法适用于以下划线开头的其他函数,例如 _isAvailable_invoke.

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

推荐答案

为命令调用以下方法:

  1. 可用
  2. 已启用
  3. 调用

所有命令的基类 - Tridion.Core.Command - 具有这些方法的标准实现.在大多数情况下,此默认实现允许对命令进行扩展.它们还调用下划线方法(_isAvailable、_isEnabled 和 _execute).

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).

我不知道为什么 CME 命令只覆盖下划线方法.也许有人认为那样更容易.它们应该被视为私有的(或相当于 C# 中的受保护的"),所以这对我来说实际上似乎是一个不好的做法.

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.

实现正确的方法(isAvailable、isEnabled 和 invoke)然后使用 this.callBase 调用基本实现会更清晰.但是,在这种情况下,您可能需要停止管道,或者还需要覆盖下划线方法,以避免您的返回值被默认下划线方法覆盖.这取决于您正在实施或扩展的命令.

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.

附言isValidSelection 是一种仅限 PowerTools 的方法,它将它们都需要的通用逻辑与特定于每个命令的逻辑分开.

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

这篇关于_isEnabled 和 isEnabled 在安圭拉有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆