Пример #1
0
        /// <summary>
        /// Renders the JBST control reference and any stored data to be used.
        /// </summary>
        /// <param name="writer">output</param>
        /// <param name="data">data to be bound as an object which will be serialized</param>
        /// <param name="index">the data index</param>
        /// <param name="count">the total data count</param>
        /// <param name="inner">a callback for writing inner placeholder content</param>
        internal void Write(TextWriter writer, object data, int index, int count, InnerCallback inner)
        {
            if (String.IsNullOrEmpty(this.JbstName))
            {
                throw new ArgumentNullException("JBST Name must be specified.");
            }

            // generate an ID for controls which do not have explicit
            if (String.IsNullOrEmpty(this.ID))
            {
                // happens with no parents
                this.ID = "_" + Guid.NewGuid().ToString("n");
            }

            bool   hasInner    = (inner != null);
            string placeholder = hasInner ? "div" : "noscript";

            // render the placeholder hook
            writer.Write('<');
            writer.Write(placeholder);
            writer.Write(" id=\"");
            writer.Write(this.ID);
            writer.Write("\">");

            if (hasInner)
            {
                // render inner as loading/error markup
                inner(writer);
            }

            string inlineData = null;

            if (data != null && !(data is EcmaScriptIdentifier) && this.AutoMarkup == AutoMarkupType.Data)
            {
                if (hasInner)
                {
                    writer.Write("<noscript>");
                }

                // serialize InlineData as a JavaScript literal
                StringBuilder builder = new StringBuilder();

                JsonMarkupWriter jsWriter = new JsonMarkupWriter(builder, writer);
                if (this.IsDebug)
                {
                    jsWriter.Settings.PrettyPrint = true;
                    jsWriter.Settings.NewLine     = Environment.NewLine;
                    jsWriter.Settings.Tab         = "\t";
                }
                jsWriter.Write(data);

                if (hasInner)
                {
                    writer.Write("</noscript>");
                }

                inlineData = builder.ToString();
            }

            writer.Write("</");
            writer.Write(placeholder);
            writer.Write('>');

            // render the binding script
            writer.Write("<script type=\"text/javascript\">");

            writer.Write(this.JbstName);
            writer.Write(".replace(\"");
            writer.Write(this.ID);
            writer.Write("\",");

            if (!String.IsNullOrEmpty(inlineData))
            {
                writer.Write(inlineData);
            }
            else if (data != null)
            {
                // serialize InlineData as a JavaScript literal
                EcmaScriptWriter jsWriter = new EcmaScriptWriter(writer);
                if (this.IsDebug)
                {
                    jsWriter.Settings.PrettyPrint = true;
                    jsWriter.Settings.NewLine     = Environment.NewLine;
                    jsWriter.Settings.Tab         = "\t";
                }
                jsWriter.Write(data);
            }
            else
            {
                // smallest most innocuous default data
                writer.Write("{}");
            }

            if (index >= 0)
            {
                writer.Write(",(");
                writer.Write(index);
                writer.Write(')');

                if (count >= 0)
                {
                    writer.Write(",(");
                    writer.Write(count);
                    writer.Write(')');
                }
            }
            writer.Write(");");

            writer.Write("</script>");
        }
Пример #2
0
        /// <summary>
        /// Renders the JBST control reference and any stored data to be used.
        /// </summary>
        /// <param name="writer">output</param>
        /// <param name="data">data to be bound as an object which will be serialized</param>
        /// <param name="index">the data index</param>
        /// <param name="count">the total data count</param>
        /// <param name="inner">a callback for writing inner placeholder content</param>
        internal void Write(TextWriter writer, object data, int index, int count, InnerCallback inner)
        {
            if (String.IsNullOrEmpty(this.JbstName))
            {
                throw new ArgumentNullException("JBST Name must be specified.");
            }

            // generate an ID for controls which do not have explicit
            if (String.IsNullOrEmpty(this.ID))
            {
                // happens with no parents
                this.ID = "_"+Guid.NewGuid().ToString("n");
            }

            bool hasInner = (inner != null);
            string placeholder = hasInner ? "div" : "noscript";

            // render the placeholder hook
            writer.Write('<');
            writer.Write(placeholder);
            writer.Write(" id=\"");
            writer.Write(this.ID);
            writer.Write("\">");

            if (hasInner)
            {
                // render inner as loading/error markup
                inner(writer);
            }

            string inlineData = null;
            if (data != null && !(data is EcmaScriptIdentifier) && this.AutoMarkup == AutoMarkupType.Data)
            {
                if (hasInner)
                {
                    writer.Write("<noscript>");
                }

                // serialize InlineData as a JavaScript literal
                StringBuilder builder = new StringBuilder();

                JsonMarkupWriter jsWriter = new JsonMarkupWriter(builder, writer);
                if (this.IsDebug)
                {
                    jsWriter.Settings.PrettyPrint = true;
                    jsWriter.Settings.NewLine = Environment.NewLine;
                    jsWriter.Settings.Tab = "\t";
                }
                jsWriter.Write(data);

                if (hasInner)
                {
                    writer.Write("</noscript>");
                }

                inlineData = builder.ToString();
            }

            writer.Write("</");
            writer.Write(placeholder);
            writer.Write('>');

            // render the binding script
            writer.Write("<script type=\"text/javascript\">");

            writer.Write(this.JbstName);
            writer.Write(".replace(\"");
            writer.Write(this.ID);
            writer.Write("\",");

            if (!String.IsNullOrEmpty(inlineData))
            {
                writer.Write(inlineData);
            }
            else if (data != null)
            {
                // serialize InlineData as a JavaScript literal
                EcmaScriptWriter jsWriter = new EcmaScriptWriter(writer);
                if (this.IsDebug)
                {
                    jsWriter.Settings.PrettyPrint = true;
                    jsWriter.Settings.NewLine = Environment.NewLine;
                    jsWriter.Settings.Tab = "\t";
                }
                jsWriter.Write(data);
            }
            else
            {
                // smallest most innocuous default data
                writer.Write("{}");
            }

            if (index >= 0)
            {
                writer.Write(",(");
                writer.Write(index);
                writer.Write(')');

                if (count >= 0)
                {
                    writer.Write(",(");
                    writer.Write(count);
                    writer.Write(')');
                }
            }
            writer.Write(");");

            writer.Write("</script>");
        }