开发者

Best pratice to map a mixin in a Script# import library

What's the raccomended way to map a Javascript Mixin in a Script# import library?

For an example: this qooxdoo api

http://demo.qooxdoo.org/1.5.x/apiv开发者_开发知识库iewer/#qx.core.Object

Implements this mixin

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

How should I map it in C#? Extensions methods? Interfaces?


If you check out the script# sources on github, you'll see examples of how to do this in the jQuery world.

The answer depends if you're simply trying to import existing script or if you want to use script# to author the mixin itself.

If you want to simply import, I'll recommend you check out the jQuery and jQuery.History projects in the source code. jQuery.History represents a jQuery history mixin (but does so without any extension methods or interfaces).

If you want to author the mixin in script#, read on... the high-level approach is to define a static class and annotate it with the [Mixin] attribute. In the future, the approach will change to use c# extension methods, but this custom approach is what you need for now.

Back to sample - at https://github.com/nikhilk/scriptsharp/blob/master/samples/PhotoDemo/Gallery/GalleryPlugin.cs you'll see:

[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.
    }
}

This will get generated into script as:

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

For reference, the way jQuery.Current was defined in the out-of-box jQuery library (see https://github.com/nikhilk/scriptsharp/blob/master/src/Libraries/jQuery/jQuery.Core/jQuery.cs for full source):

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

    private jQuery() {
    }

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

A bit involved, but hopefully once you've tried it, it becomes straightforward. Out-of-box script# provides APIs and templates for jQuery scenarios which help simplify by removing the mechanics from the forefront, which you'll need to understand when working against a different script framework.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜