Represents a Javascript/Typescript file that needs to be called using Require.js In order to create a new JsFunction just call the JsModule indexer and invoke the returning delegate. JsModule MyModule = new JsModule("moduleName"); MyModule["functionName"](arguments...) Will translate to: require(["moduleName"], function(mod) { mod.functionName(arguments...); }
Exemplo n.º 1
0
        string AttachConstructor(JsModule module, string type)
        {
            var varModule = JsFunction.VarName(AttachFunction.Module);

            var varLines = JsFunction.VarName(module);

            var line = type.FirstLower();

            var args = AttachFunction.Arguments.ToString(a =>
                                                         a == JsFunction.This ? "that" :
                                                         a == this ? line :
                                                         JsonConvert.SerializeObject(a, AttachFunction.JsonSerializerSettings), ", ");

            var result = "require(['" + module + "', '" + AttachFunction.Module.Name + "'], function(" + varLines + ", " + varModule + ") {\r\n" +
                         "var " + line + " = " + NewLine(varLines, type) + ";\r\n" +
                         varModule + "." + AttachFunction.FunctionName + "(" + args + ");\r\n" +
                         line + ".ready();\r\n" +
                         "});";

            if (!AttachFunction.Arguments.Contains(JsFunction.This))
            {
                return(result);
            }

            return("(function(that) { " + result + "})(this)");
        }
Exemplo n.º 2
0
        string BasicConstructor(JsModule module, string type)
        {
            var varNameLines = JsFunction.VarName(module);

            var result = "require(['" + module + "'], function(" + varNameLines + ") { " + NewLine(varNameLines, type) + ".ready(); });";

            return(result);
        }
Exemplo n.º 3
0
        public MvcHtmlString ConstructorScript(JsModule module, string type)
        {
            var result = AttachFunction != null?
                         AttachConstructor(module, type) :
                             BasicConstructor(module, type);

            return(new MvcHtmlString("<script>" + result + "</script>"));
        }
Exemplo n.º 4
0
        internal static string VarName(JsModule module)
        {
            var result = module.Name.TryAfterLast(".") ?? module.Name;

            result = result.TryAfterLast("/") ?? result;

            result = Regex.Replace(result, "[^a-zA-Z0-9]", "");

            return(result);
        }
Exemplo n.º 5
0
        internal JsFunction(JsModule module, string functionName, params object[] arguments)
        {
            if (module == null)
                throw new ArgumentNullException("module");

            if (functionName == null)
                throw new ArgumentNullException("functionName");

            this.Module = module;
            this.FunctionName = functionName;
            this.Arguments = arguments ?? new object[0];
        }
Exemplo n.º 6
0
        internal JsFunction(JsModule module, string functionName, params object[] arguments)
        {
            if (module == null)
            {
                throw new ArgumentNullException("module");
            }

            if (functionName == null)
            {
                throw new ArgumentNullException("functionName");
            }

            this.Module       = module;
            this.FunctionName = functionName;
            this.Arguments    = arguments ?? new object[0];
        }
Exemplo n.º 7
0
        string BasicConstructor(JsModule module, string type)
        {
            var varNameLines = JsFunction.VarName(module);

            var result = "require(['" + module + "'], function(" + varNameLines + ") { " + NewLine(varNameLines, type) + ".ready(); });";

            return result;
        }
Exemplo n.º 8
0
        string AttachConstructor(JsModule module, string type)
        {
            var varModule = JsFunction.VarName(AttachFunction.Module);

            var varLines = JsFunction.VarName(module);

            var line = type.FirstLower();

            var args = AttachFunction.Arguments.ToString(a =>
                a == JsFunction.This ? "that" :
                a == this ? line :
                JsonConvert.SerializeObject(a, AttachFunction.JsonSerializerSettings), ", ");

            var result = "require(['" + module + "', '" + AttachFunction.Module.Name + "'], function(" + varLines + ", " + varModule + ") {\r\n" +
                "var " + line + " = " + NewLine(varLines, type) + ";\r\n" +
                varModule + "." + AttachFunction.FunctionName + "(" + args + ");\r\n" +
                line + ".ready();\r\n" +
                "});";

            if (!AttachFunction.Arguments.Contains(JsFunction.This))
                return result;

            return "(function(that) { " + result + "})(this)";
        }
Exemplo n.º 9
0
        public MvcHtmlString ConstructorScript(JsModule module, string type)
        {
            var result = AttachFunction != null ?
                AttachConstructor(module, type) :
                BasicConstructor(module, type);

            return new MvcHtmlString("<script>" + result + "</script>");
        }
Exemplo n.º 10
0
        internal static string VarName(JsModule module)
        {
            var result = module.Name.TryAfterLast(".") ?? module.Name;

            result = result.TryAfterLast("/") ?? result;

            result = Regex.Replace(result, "[^a-zA-Z0-9]", "");

            return result;
        }