Beispiel #1
0
        /// <summary>
        /// Returns the rendered JavaScript for the generated object and name.
        /// Note this method returns only the generated object, not the
        /// related code to save updates.
        ///
        /// You can use this method with MVC Views to embedd generated JavaScript
        /// into the the View page.
        /// <param name="addScriptTags">If provided wraps the script text with script tags</param>
        /// </summary>
        public string ToString(bool addScriptTags)
        {
            if (!AutoRenderClientScript || ScriptVars.Count == 0)
            {
                return(string.Empty);
            }

            StringBuilder sb = new StringBuilder();

            if (addScriptTags)
            {
                sb.AppendLine("<script>");
            }

            // Check for any prefix code and inject it
            if (sbPrefixScriptCode.Length > 0)
            {
                sb.Append(sbPrefixScriptCode.ToString());
            }

            // If the name includes a . assignment is made to an existing
            // object or namespaced reference - don't create var instance.
            if (!ClientObjectName.Contains("."))
            {
                sb.Append("var ");
            }

            sb.AppendLine(ClientObjectName + " = {");


            foreach (KeyValuePair <string, object> entry in ScriptVars)
            {
                if (entry.Key.StartsWith("."))
                {
                    // It's a dynamic key
                    string[] tokens = entry.Key.Split(new char[1] {
                        '.'
                    }, StringSplitOptions.RemoveEmptyEntries);
                    string varName  = tokens[0];
                    string property = tokens[1];


                    object propertyValue = null;
                    if (entry.Value != null)
                    {
                        propertyValue = ReflectionUtils.GetPropertyEx(entry.Value, property);
                    }

                    sb.AppendLine("\t" + varName + ": " + JsonSerializer.Serialize(propertyValue) + ",");
                }
                else
                {
                    sb.AppendLine("\t" + entry.Key + ": " + JsonSerializer.Serialize(entry.Value) + ",");
                }
            }

            if (UpdateMode != AllowUpdateTypes.None)
            {
                sb.AppendLine("\t_Items:{},");
                sb.AppendLine("\tadd: function(key,value) { _Items[key] = value; },");
            }

            // Strip off last comma plus CRLF
            if (sb.Length > 0)
            {
                sb.Length -= 3;
            }

            sb.AppendLine("\r\n};");

            if (sbPostFixScriptCode.Length > 0)
            {
                sb.AppendLine(sbPostFixScriptCode.ToString());
            }

            if (addScriptTags)
            {
                sb.AppendLine("</script>\r\n");
            }



            if (UpdateMode != AllowUpdateTypes.None)
            {
                string clientID = "__" + ClientObjectName;

                string script = string.Format(@"$(document.forms[0]).submit(function() {{ __submitServerVars({0},'__{0}'); }});",
                                              ClientObjectName);

                sb.AppendLine("<script>\r\n" + STR_SUBMITSCRIPT + "\r\n" + script + "\r\n</script>");
                sb.AppendFormat(@"<input type=""hidden"" id=""{0}"" name=""{0}""  value="""" />" + "\r\n", clientID);
            }


            return(sb.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// Returns the rendered JavaScript for the generated object and name.
        /// Note this method returns only the generated object, not the
        /// related code to save updates.
        ///
        /// You can use this method with MVC Views to embedd generated JavaScript
        /// into the the View page.
        /// <param name="addScriptTags">If provided wraps the script text with script tags</param>
        /// </summary>
        public string ToString(bool addScriptTags)
        {
            if (ScriptVars.Count == 0)
            {
                return(string.Empty);
            }

            StringBuilder sb = new StringBuilder();

            if (addScriptTags)
            {
                sb.AppendLine("<script>");
            }

            // Check for any prefix code and inject it
            if (sbPrefixScriptCode.Length > 0)
            {
                sb.Append(sbPrefixScriptCode);
            }

            // If the name includes a . assignment is made to an existing
            // object or namespaced reference - don't create var instance.
            if (!ClientObjectName.Contains("."))
            {
                sb.Append("var ");
            }

            sb.AppendLine(ClientObjectName + " = {");


            foreach (KeyValuePair <string, object> entry in ScriptVars)
            {
                if (entry.Key.StartsWith("."))
                {
                    // It's a dynamic key
                    string[] tokens = entry.Key.Split(new char[1] {
                        '.'
                    }, StringSplitOptions.RemoveEmptyEntries);
                    string varName  = tokens[0];
                    string property = tokens[1];


                    object propertyValue = null;
                    if (entry.Value != null)
                    {
                        propertyValue = ReflectionUtils.GetPropertyEx(entry.Value, property);
                    }

                    sb.AppendLine("\t" + varName + ": " + Serialize(propertyValue) + ",");
                }
                else
                {
                    sb.AppendLine("\t" + entry.Key + ": " + Serialize(entry.Value) + ",");
                }
            }

            // Strip off last comma plus CRLF
            if (sb.Length > 0)
            {
                sb.Length -= 3;
            }

            sb.AppendLine("\r\n};");

            if (sbPostFixScriptCode.Length > 0)
            {
                sb.AppendLine(sbPostFixScriptCode.ToString());
            }

            if (addScriptTags)
            {
                sb.AppendLine("</script>\r\n");
            }

            return(sb.ToString());
        }