Exemplo n.º 1
0
        /// <summary>
        /// 呈现脚本。
        /// </summary>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var template = (await output.GetChildContentAsync()).GetContent();

            if (string.IsNullOrWhiteSpace(template) || (Models == null && RemoteUrl == null))
            {
                output.SuppressOutput();
                return;
            }

            var document = new TemplateDocument(template);

            if (Binder == "json")
            {
                var id = context.AllAttributes["id"]?.Value;
                if (id == null)
                {
                    id = "jmoz_" + context.UniqueId;
                    output.Attributes.SetAttribute("id", id);
                }

                output.Content.AppendHtml("<script>");
                output.Content.AppendHtml("$(function(){");
                output.Content.AppendHtml($"var $this = $('#{id}');");
                Render(output, document);
                output.Content.AppendHtml(document.Scripts);
                output.Content.AppendHtml("});");
                output.Content.AppendHtml("</script>");
            }
            else
            {
                if (Models is IEnumerable instances)
                {
                    foreach (var instance in instances)
                    {
                        output.Content.AppendHtml(document.ToHtmlString(_executor, instance, GetValue));
                    }
                }
                else
                {
                    output.Content.AppendHtml(document.ToHtmlString(_executor, Models, GetValue));
                }
                if (!string.IsNullOrWhiteSpace(document.Scripts))
                {
                    output.Content.AppendHtml("<script>").AppendHtml(document.Scripts).Append("</script>");
                }
            }
        }
Exemplo n.º 2
0
        private void Render(TagHelperOutput output, TemplateDocument document)
        {
            Func(output, "render", "data", c =>
            {
                c.AppendHtml("var html = '';");
                Func(output, "appender", "$model", ac => ac.AppendHtml(document.ToJsString(_executor)));
                c.AppendHtml("appender(data);");
                c.AppendHtml("$this.html(html);");
            });
            if (Models == null || Interval > 0)
            {
                Func(output, "ajax", "url", c =>
                {
                    c.AppendHtml("$.ajax({url:url+'&_'+(+new Date())")
                    .AppendHtml(", dataType:'JSON', type:'GET', success:function(data){")
                    .AppendHtml("render(data);");

                    if (Callback != null)
                    {
                        c.AppendHtml(Callback).AppendHtml("(data);");
                    }

                    if (Interval > 0)
                    {//定时器
                        c.AppendHtml("setTimeout(function(){ajax(url);}")
                        .AppendHtml(",")
                        .AppendHtml((Interval * 1000).ToString())
                        .AppendHtml(");");
                    }
                    c.AppendHtml("}, error:function(r){$this.html(r.responseText);");
                    if (Interval > 0)
                    {//定时器
                        c.AppendHtml("setTimeout(function(){ajax(url);}")
                        .AppendHtml(",")
                        .AppendHtml((Interval * 1000).ToString())
                        .AppendHtml(");");
                    }
                    c.AppendHtml("}});");
                });
            }

            switch (Models)
            {
            case string jscode when jscode.Trim().Length > 0:
                output.Content.AppendHtml("render(");

                output.Content.AppendHtml(jscode);
                output.Content.AppendHtml(");");
                break;

            default:
                output.Content.AppendHtml("render(");
                output.Content.AppendHtml(JsonConvert.SerializeObject(Models));
                output.Content.AppendHtml(");");
                break;

            case null:
                output.Content.AppendHtml("ajax(");
                var url = RemoteUrl;
                if (url.IndexOf('?') == -1)
                {
                    url += '?';
                }
                else
                {
                    url += '&';
                }
                url += string.Join("&", Data.Select(x => $"{x.Key}={x.Value}"));
                output.Content.AppendHtml("'").AppendHtml(url).AppendHtml("'");
                output.Content.AppendHtml(");");
                break;
            }
        }