예제 #1
0
        private string GetInitializeData(TModel model, bool needBinding)
        {
            if (isInitialized)
            {
                return("");
            }
            isInitialized = true;
            KnockoutUtilities.ConvertData(model);
            this.model = model;

            var sb = new StringBuilder();

            var json = JsonConvert.SerializeObject(model);

            sb.AppendLine(@"<script type=""text/javascript""> ");
            sb.AppendLine(string.Format("var {0}Js = {1};", ViewModelName, json));
            var mappingData = KnockoutJsModelBuilder.CreateMappingData <TModel>();

            if (mappingData == "{}")
            {
                sb.AppendLine(string.Format("var {0} = ko.mapping.fromJS({0}Js); ", ViewModelName));
            }
            else
            {
                sb.AppendLine(string.Format("var {0}MappingData = {1};", ViewModelName, mappingData));
                sb.AppendLine(string.Format("var {0} = ko.mapping.fromJS({0}Js, {0}MappingData); ", ViewModelName));
            }
            sb.Append(KnockoutJsModelBuilder.AddComputedToModel(model, ViewModelName));
            if (needBinding)
            {
                sb.AppendLine(string.Format("ko.applyBindings({0});", ViewModelName));
            }
            sb.AppendLine(@"</script>");
            return(sb.ToString());
        }
예제 #2
0
        public HtmlString LazyApply(TModel model, string actionName, string controllerName)
        {
            var sb = new StringBuilder();

            sb.AppendLine(@"<script type=""text/javascript""> ");
            sb.Append("$(document).ready(function() {");

            sb.AppendLine(string.Format("$.ajax({{ url: '{0}', type: 'POST', success: function (data) {{", Url().Action(actionName, controllerName)));

            string mappingData = KnockoutJsModelBuilder.CreateMappingData <TModel>();

            if (mappingData == "{}")
            {
                sb.AppendLine(string.Format("var {0} = ko.mapping.fromJS(data); ", ViewModelName));
            }
            else
            {
                sb.AppendLine(string.Format("var {0}MappingData = {1};", ViewModelName, mappingData));
                sb.AppendLine(string.Format("var {0} = ko.mapping.fromJS(data, {0}MappingData); ", ViewModelName));
            }
            sb.Append(KnockoutJsModelBuilder.AddComputedToModel(model, ViewModelName));
            sb.AppendLine(string.Format("ko.applyBindings({0});", ViewModelName));

            sb.AppendLine("}, error: function (error) { alert('There was an error posting the data to the server: ' + error.responseText); } });");

            sb.Append("});");
            sb.AppendLine(@"</script>");

            return(new HtmlString(sb.ToString()));
        }
예제 #3
0
        private string GetInitializeData(TModel model, bool needBinding, string wrapperId, bool applyOnDocumentReady)
        {
            if (isInitialized)
            {
                return("");
            }

            isInitialized = true;

            KnockoutUtilities.ConvertData(model);
            this.model = model;

            var sb = new StringBuilder();

            var json = JsonConvert.SerializeObject(model);

            sb.AppendLine(@"<script type=""text/javascript""> ");
            if (applyOnDocumentReady)
            {
                sb.Append("$(document).ready(function() {");
            }
            sb.AppendLine(string.Format("var {0}Js = {1};", ViewModelName, json));
            var mappingData = KnockoutJsModelBuilder.CreateMappingData <TModel>();

            if (mappingData == "{}")
            {
                sb.AppendLine(string.Format("var {0} = ko.mapping.fromJS({0}Js); ", ViewModelName));
            }
            else
            {
                sb.AppendLine(string.Format("var {0}MappingData = {1};", ViewModelName, mappingData));
                sb.AppendLine(string.Format("var {0} = ko.mapping.fromJS({0}Js, {0}MappingData); ", ViewModelName));
            }
            sb.Append(KnockoutJsModelBuilder.AddComputedToModel(model, ViewModelName));
            if (needBinding)
            {
                if (!string.IsNullOrEmpty(wrapperId))
                {
                    sb.AppendLine(string.Format("ko.applyBindings({0}, document.getElementById('{1}'))", ViewModelName, wrapperId));
                }
                else
                {
                    sb.AppendLine(string.Format("ko.applyBindings({0});", ViewModelName));
                }
            }

            if (applyOnDocumentReady)
            {
                sb.Append("});");
            }
            sb.AppendLine(@"</script>");
            return(sb.ToString());
        }