Exemple #1
0
        /// <summary>
        /// function declaration
        /// ex: _alias0.LoginModel = _alias0.LoginModel || function () { var args = Array.prototype.slice.call(arguments); var obj = { "UserName": "", "Password": "", "RememberMe": false }; obj.constructor = _alias0.LoginModel; return obj; };
        /// </summary>
        /// <param name="withAlias"></param>
        /// <returns></returns>
        private List <string> CreateJsObjectDeclaration(bool withAlias)
        {
            List <string> funcDecl_Array = new List <string>();

            foreach (var dic in Classes)
            {
                foreach (var kv in dic)
                {
                    {
                        var    ts_out   = TypeSorter_Script.GetInstance(EnumScript.JS, kv.Value, _JSNamespace);
                        string funcDecl = ScriptHelper.GetInstance(EnumScript.JS).GetFactoryDeclaration(
                            kv.Key,
                            ts_out.ScriptValue(this.found_complex_types),
                            true,
                            _JSNamespace.GetObjectFullName(kv.Key, withAlias));
                        funcDecl_Array.Add(funcDecl);
                    }
                }
            }
            return(funcDecl_Array);
        }
Exemple #2
0
        /// <summary>
        /// Code TS of factories of c# oldClasses.
        /// There is not 'JSArrayFactory'
        /// </summary>
        /// <returns></returns>
        public string ToTSCore()
        {
            //-- sort types of oldClasses.
            this.SortTypesInClasses();

            Dictionary <string, List <TypeSorter_> > groups_by_ns = new Dictionary <string, List <TypeSorter_> >();

            foreach (var dic in this.Classes)
            {
                foreach (var kv in dic)
                {
                    var ns = kv.Value.GetScriptNamespace_Full();
                    if (groups_by_ns.ContainsKey(ns))
                    {
                        groups_by_ns[ns].Add(kv.Value);
                    }
                    else
                    {
                        var list = new List <TypeSorter_>();
                        list.Add(kv.Value);
                        groups_by_ns.Add(ns, list);
                    }
                }
            }

            StringBuilder scriptInstructions = new StringBuilder();

            scriptInstructions.AppendLine(
                @"declare namespace $dp.$shared {
    interface $Array<T> extends Array<T> {
        $dpItemFactory(): T;
    }
}");

            foreach (var kv in groups_by_ns)
            {
                //--ex: declare namespace $dp.$JsNet.ContosoUniversity.Models {
                scriptInstructions.AppendLine("declare namespace {name} {".Replace("{name}", kv.Key /*namespace*/));

                scriptInstructions.AppendLine(JSRaw.Region.Begin("interfaces"));
                foreach (var typeSorter in kv.Value)
                {
                    // interface Enrollment
                    // { EnrollmentID: number, CourseID: number, PersonID: number, Grade: number, Student: Student, Course: Course }
                    scriptInstructions.AppendLine("interface {name}".Replace("{name}", TypeHelper.GetName(typeSorter.TObj) /*model name*/));
                    var ts_out = TypeSorter_Script.GetInstance(EnumScript.TS, typeSorter, _JSNamespace);
                    scriptInstructions.AppendLine(ts_out.ScriptValue(this.found_complex_types));
                }
                scriptInstructions.AppendLine(JSRaw.Region.End());

                scriptInstructions.AppendLine(JSRaw.Region.Begin("functions"));
                foreach (var typeSorter in kv.Value)
                {
                    //-- var OfficeAssignment: () => $dp.$JsNet.ContosoUniversity.Models.OfficeAssignment;
                    scriptInstructions.AppendLine("var {model_name}: () => {model_fullname};"
                                                  .Replace("{model_name}", TypeHelper.GetName(typeSorter.TObj))
                                                  .Replace("{model_fullname}", kv.Key + "." + TypeHelper.GetName(typeSorter.TObj)));
                }
                scriptInstructions.AppendLine(JSRaw.Region.End());

                scriptInstructions.AppendLine("}");
            }

            return(scriptInstructions.ToString());
        }