ScriptSharp(Script#)和Knockout

时间:2012-02-25 19:56:37

标签: knockout.js script#

我的脚本锐利动作正在重命名,因此它们以下划线为前缀。虽然代码在浏览器中运行得非常好,但是必须记住添加下划线以使用它在客户端方面很烦人。这是设计,是否有办法改变它?

以下是一个示例: 我再现了 - Knockout JS的教程示例“点击计数器”。

脚本Sharp View模型(C#代码):

public sealed class ClickCounterViewModel
{
    public Observable<int> numberOfClicks;

    //Dependent Observable is now called computed but is backward compat.
    public DependentObservable<bool> hasClickedTooManyTimes;

    //WARNING - this get converted to _registerClick Client Side - not sure why.
    Action registerClick;
    Action resetClicks;

    public ClickCounterViewModel()
    {
        numberOfClicks = Knockout.Observable<int>(0);
        registerClick = delegate() {
             this.numberOfClicks.SetValue(this.numberOfClicks.GetValue() + 1); 
        };

        resetClicks = delegate() { this.numberOfClicks.SetValue(0); };

        DependentObservableOptions<bool> options = new DependentObservableOptions<bool>();
        options.Model = this;
        options.GetValueFunction = new Func<bool>(delegate { 
             return this.numberOfClicks.GetValue() >= 3; 
        });

        hasClickedTooManyTimes = Knockout.DependentObservable<bool>(options);
    }
}

当此代码转换为javascript时,操作会以下划线为前缀。这是预期的行为吗?

生成代码(javascript) - 只显示生成的注释以说明问题:

Knockout2Example2.ClickCounterViewModel = function Knockout2Example2_ClickCounterViewModel() {
/// <field name="numberOfClicks" type="Observable`1">
/// </field>
/// <field name="hasClickedTooManyTimes" type="DependentObservable`1">
/// </field>
/// <field name="_registerClick" type="Function">
/// </field>
/// <field name="_resetClicks" type="Function">
/// </field>
/// This script was generated using Script# v0.7.4.0

2 个答案:

答案 0 :(得分:1)

哎呀!

我没有让我的两个动作registerClick和resetClicks public。 将它们公开处理问题并将它们呈现给没有下划线的js。

答案 1 :(得分:1)

Underscore是一种常见的JavaScript约定,意思是&#34;这是私有的#34;。 Scriptsharp给出以下划线开头的非公共成员,属性和方法名称。将您的操作公开,以便在外部JavaScript中方便地引用它们。