예제 #1
0
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var result = base.BindModel(controllerContext, bindingContext);

            KnockoutUtilities.ConvertData(result);
            return(result);
        }
예제 #2
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());
        }
예제 #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());
        }
 protected override JsonResult Json(object data, string contentType, Encoding contentEncoding)
 {
     KnockoutUtilities.ConvertData(data);
     return(base.Json(data, contentType, contentEncoding));
 }