【问题标题】:Best pratice to map a mixin in a Script# import library在 Script# 导入库中映射 mixin 的最佳实践
【发布时间】:2011-09-27 16:58:12
【问题描述】:

在 Script# 导入库中映射 Javascript Mixin 的最佳方式是什么?

例如:这个 qooxdoo api

http://demo.qooxdoo.org/1.5.x/apiviewer/#qx.core.Object

实现这个混入

http://demo.qooxdoo.org/1.5.x/apiviewer/#qx.data.MBinding

我应该如何在 C# 中映射它?扩展方法?接口?

【问题讨论】:

  • 我认为 Script# 中没有可用的扩展方法

标签: interop mixins script#


【解决方案1】:

如果您查看 github 上的 script# 源代码,您将看到如何在 jQuery 世界中执行此操作的示例。

答案取决于您是只是尝试导入现有脚本,还是想使用 script# 来编写 mixin 本身。

如果您想简单地导入,我建议您查看源代码中的jQueryjQuery.History 项目。 jQuery.History 表示一个 jQuery 历史 mixin(但没有任何扩展方法或接口)。

如果您想在 script# 中编写 mixin,请继续阅读... 高级方法是定义一个静态类并使用 [Mixin] 属性对其进行注释。将来,该方法将更改为使用 c# 扩展方法,但这种自定义方法是您现在需要的。

回到示例 - 在https://github.com/nikhilk/scriptsharp/blob/master/samples/PhotoDemo/Gallery/GalleryPlugin.cs 你会看到:

[Mixin("$.fn")]
public static class GalleryPlugin {

    public static jQueryObject Gallery(GalleryPluginOptions customOptions) {
        ...
        // Use jQuery.Current to access the "this" object at runtime pointing
        // to the object instance whose prototype now contains the mixin methods.
    }
}

这将生成到脚本中:

$.fn.gallery = function(customOptions) {
}

作为参考,jQuery.Current 在开箱即用的 jQuery 库中定义的方式(完整源代码请参见https://github.com/nikhilk/scriptsharp/blob/master/src/Libraries/jQuery/jQuery.Core/jQuery.cs):

[IgnoreNamespace]
[Imported]
[ScriptName("$")]
public sealed class jQuery {

    private jQuery() {
    }

    [ScriptAlias("this")]
    [IntrinsicProperty]
    public static jQueryObject Current {
        get {
            return null;
        }
    }
}

有点涉及,但希望一旦你尝试过,它就会变得简单。开箱即用的脚本# 为 jQuery 场景提供 API 和模板,通过从最前沿移除机制来帮助简化,在使用不同的脚本框架时需要了解这些机制。

希望这会有所帮助!

【讨论】:

  • 感谢您的回复,但我认为我误解了一些东西。从您在此处描述的方法看来,Mixin 似乎只能应用于一个类/命名空间,这对我来说没有意义。例如,我应该如何实现这个 Mixin demo.qooxdoo.org/current/apiviewer/#qx.ui.core.MExecutable(注意这个 mixin 包含在一个以上的类中)?我将在需要时使用的静态 MExecutable 类?在按钮的情况下,这会给我一个像 MExecutable.Execute 而不是 button.Execute () 的语法?
猜你喜欢
  • 2021-07-24
  • 2023-03-16
  • 1970-01-01
  • 2020-04-19
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多