private PropertySerializeInfo GetPropertySerializationInfo(DotvvmProperty property) { var binding = GetBinding(property); if (binding == null && !typeof(ITemplate).IsAssignableFrom(property.PropertyType)) { JsonSerializerSettings settings = DefaultViewModelSerializer.CreateDefaultSettings(); settings.StringEscapeHandling = StringEscapeHandling.EscapeHtml; return(new PropertySerializeInfo { Property = property, Js = JsonConvert.SerializeObject(GetValue(property), Formatting.None, settings), IsSerializable = true }); } else if (binding is IValueBinding) { return(new PropertySerializeInfo { Property = property, Js = (binding as IValueBinding).GetKnockoutBindingExpression(this), IsSerializable = true }); } else { return(new PropertySerializeInfo { Property = property }); } }
private PropertySerializeInfo GetPropertySerializationInfo(DotvvmProperty property) { if (ContainsPropertyStaticValue(property)) { JsonSerializerSettings settings = DefaultViewModelSerializer.CreateDefaultSettings(); settings.StringEscapeHandling = StringEscapeHandling.EscapeHtml; return(new PropertySerializeInfo { Property = property, Js = JsonConvert.SerializeObject(GetValue(property), Formatting.None, settings), IsSerializable = true }); } else if (GetBinding(property) is IValueBinding valueBinding) { return(new PropertySerializeInfo { Property = property, Js = valueBinding.GetKnockoutBindingExpression(this), IsSerializable = true }); } else { return(new PropertySerializeInfo { Property = property }); } }
static T Populate <T>(string json, JObject encryptedValues = null) { var serializer = JsonSerializer.Create(DefaultViewModelSerializer.CreateDefaultSettings()); serializer.Converters.Add(CreateConverter(true, encryptedValues)); //serializer.Populate(new StringReader(json), viewModel); return(serializer.Deserialize <T>(new JsonTextReader(new StringReader(json)))); }
/// <summary> /// Returns Javascript expression that represents the property value (even if the property contains hardcoded value) /// </summary> public static string GetKnockoutBindingExpression(this DotvvmBindableObject obj, DotvvmProperty property) { var binding = obj.GetValueBinding(property); if (binding != null) { return(binding.GetKnockoutBindingExpression(obj)); } return(JsonConvert.SerializeObject(obj.GetValue(property), DefaultViewModelSerializer.CreateDefaultSettings())); }
static string Serialize <T>(T viewModel, out JObject encryptedValues, bool isPostback = false) { encryptedValues = new JObject(); var serializer = JsonSerializer.Create(DefaultViewModelSerializer.CreateDefaultSettings()); serializer.Converters.Add(CreateConverter(isPostback, encryptedValues)); var output = new StringWriter(); serializer.Serialize(output, viewModel); return(output.ToString()); }
protected internal string TranslateValueOrBinding(DotvvmProperty property) { var binding = GetValueBinding(property); if (binding == null) { return(JsonConvert.SerializeObject(GetValue(property), DefaultViewModelSerializer.CreateDefaultSettings())); } else { return("ko.unwrap(" + binding.GetKnockoutBindingExpression(this) + ")"); } }
private static string TranslateRouteParameter(DotvvmBindableObject control, KeyValuePair <string, object> param, bool caseSensitive = false) { string expression = ""; if (param.Value is IBinding) { EnsureValidBindingType(param.Value as IBinding); expression = (param.Value as IValueBinding)?.GetKnockoutBindingExpression(control) ?? JsonConvert.SerializeObject((param.Value as IStaticValueBinding)?.Evaluate(control), DefaultViewModelSerializer.CreateDefaultSettings()); } else { expression = JsonConvert.SerializeObject(param.Value, DefaultViewModelSerializer.CreateDefaultSettings()); } return(JsonConvert.SerializeObject(caseSensitive ? param.Key : param.Key.ToLower()) + ": " + expression); }
private PropertySerializeInfo GetPropertySerializationInfo(DotvvmProperty property) { var binding = GetBinding(property); if (binding == null) { return(new PropertySerializeInfo { Property = property, Js = JsonConvert.SerializeObject(GetValue(property), Formatting.None, DefaultViewModelSerializer.CreateDefaultSettings()), IsSerializable = true }); } else if (binding is IValueBinding) { return(new PropertySerializeInfo { Property = property, Js = (binding as IValueBinding).GetKnockoutBindingExpression(this), IsSerializable = true }); } else { return(new PropertySerializeInfo { Property = property }); } }
private static void AddValidatedValue(IHtmlWriter writer, IDotvvmRequestContext context, DotvvmProperty prop, DotvvmControl control) { writer.AddKnockoutDataBind("dotvvmValidation", control, ValueProperty, renderEvenInServerRenderingMode: true); // render options var bindingGroup = new KnockoutBindingGroup(); foreach (var property in ValidationOptionProperties) { var javascriptName = KnockoutHelper.ConvertToCamelCase(property.Name); var optionValue = control.GetValue(property); if (!object.Equals(optionValue, property.DefaultValue)) { bindingGroup.Add(javascriptName, JsonConvert.SerializeObject(optionValue, DefaultViewModelSerializer.CreateDefaultSettings())); } } writer.AddKnockoutDataBind("dotvvmValidationOptions", bindingGroup); }
public virtual void Add(string name, DotvvmControl control, DotvvmProperty property, Action nullBindingAction = null) { var binding = control.GetValueBinding(property); if (binding == null) { if (nullBindingAction != null) { nullBindingAction(); } else { Add(name, JsonConvert.SerializeObject(control.GetValue(property), DefaultViewModelSerializer.CreateDefaultSettings())); } } else { entries.Add(new KnockoutBindingInfo() { Name = name, Expression = GetKnockoutBindingExpression(control, control.GetValueBinding(property)) }); } }