nikhilk / scriptsharp

Script# Project - a C# to JavaScript compiler, to power your HTML5 and Node.js web development.
http://scriptsharp.com
Other
659 stars 182 forks source link

Passing function via a Dictionary #249

Closed dealproc closed 12 years ago

dealproc commented 12 years ago

Considering this:

public class ViewModel
{
    public Observable<string> FirstName;
    public Observable<string> LastName;
    public Observable<string> EmailAddress;
    public Observable<int> Age;
    public Observable<string> Location;
    public Observable<string> Subscription;
    public Observable<string> Password;
    public Observable<string> ConfirmPassword;
    public Observable<string> Captcha;
    public KnockoutValidationGroup Errors;

    public string[] SubscriptionOptions;

    public void Submit()
    {
        if (this.Errors.GetValue().Length == 0)
        {
            Window.Alert("Thank You!");
        }
        else
        {
            Window.Alert("Please check your submission");

        }
    }

    public void RequireLocation()
    {
        this.Location.Extend(new Dictionary("required", true));
    }

    public ViewModel()
        : base()
    {
        FirstName = Knockout.Observable<string>().Extend(new Dictionary("minLength", 2, "maxLength", 10));
        LastName = Knockout.Observable<string>().Extend(new Dictionary("required", true));
        EmailAddress = Knockout.Observable<string>().Extend(new Dictionary("required", new Dictionary("message", "Please supply your email address")));
        Age = Knockout.Observable<int>().Extend(new Dictionary("min", 1, "max", 100));
        Location = Knockout.Observable<string>();
        Subscription = Knockout.Observable<string>().Extend(new Dictionary("required", true));
        Password = Knockout.Observable<string>();
        ConfirmPassword = Knockout.Observable<string>().Extend(new Dictionary("validation", new Dictionary("validator", MustEqual, "message", "Passwords do not match.", "params", this.Password)));
        Captcha = Knockout.Observable<string>().Extend(new Dictionary("validation", new Dictionary("validator", captcha, "message", "Please check.")));

        Errors = KnockoutValidation.Group(this);
    }

    private bool captcha(string val)
    {
        return val == "11";
    }
    private bool MustEqual(string val, Observable<string> other)
    {
        return val == other.GetValue();
    }
}

I'm trying to pass MustEqual, Captcha, and Password into dictionaries without success. What is the preferred method do make this happen... a cast to object didn't seem to work either.

dealproc commented 12 years ago

Nevermind... didn't realize the overload existed to do this:

new Dictionary<string, Func<string, Observable<string>, bool>>("validator", (Func<string, Observable<string>, bool>)MustEqual) got it there... hoping the ss.createDelegate that's in the rendered Javascript will work.