/// <summary> /// Writes text iff the property contains hard-coded value OR /// writes knockout text binding iff the property contains binding /// </summary> /// <param name="writer">HTML output writer</param> /// <param name="obj">Dotvvm control which contains the <see cref="DotvvmProperty"/> with value to be written</param> /// <param name="property">Value of this property will be written</param> /// <param name="wrapperTag">Name of wrapper tag, null => knockout binding comment</param> public static void WriteTextOrBinding(this IHtmlWriter writer, DotvvmBindableObject obj, DotvvmProperty property, string wrapperTag = null) { var valueBinding = obj.GetValueBinding(property); if (valueBinding != null) { if (wrapperTag == null) { writer.WriteKnockoutDataBindComment("text", valueBinding.GetKnockoutBindingExpression(obj)); writer.WriteKnockoutDataBindEndComment(); } else { writer.AddKnockoutDataBind("text", valueBinding.GetKnockoutBindingExpression(obj)); writer.RenderBeginTag(wrapperTag); writer.RenderEndTag(); } } else { if (wrapperTag != null) { writer.RenderBeginTag(wrapperTag); } writer.WriteText(obj.GetValue(property).ToString()); if (wrapperTag != null) { writer.RenderEndTag(); } } }
public static void WriteKnockoutDataBindComment(this IHtmlWriter writer, string name, DotvvmBindableObject control, DotvvmProperty property) { var binding = control.GetValueBinding(property); if (binding is null) { throw new DotvvmControlException(control, $"A value binding expression was expected in property {property}."); } writer.WriteKnockoutDataBindComment(name, binding.GetKnockoutBindingExpression(control)); }
/// <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())); }
/// <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()); } return(JsonConvert.SerializeObject(obj.GetValue(property))); }
/// <summary> /// Gets the validation target expression. /// </summary> public static string GetValidationTargetExpression(DotvvmBindableObject control) { if (!(bool)control.GetValue(Validation.EnabledProperty)) { return(null); } return(control.GetValueBinding(Validation.TargetProperty)?.GetKnockoutBindingExpression(control) ?? RootValidationTargetExpression); }
internal IEnumerable <IValueBinding> GetDataContextHierarchy() { var bindings = new List <IValueBinding>(); DotvvmBindableObject current = this; do { var binding = current.GetValueBinding(DataContextProperty, false); if (binding != null) { bindings.Add(binding); } current = current.Parent; }while (current != null); bindings.Reverse(); return(bindings); }
public static void AddKnockoutDataBind(this IHtmlWriter writer, string name, DotvvmBindableObject control, DotvvmProperty property, Action?nullBindingAction = null, string?valueUpdate = null, bool renderEvenInServerRenderingMode = false, bool setValueBack = false) { var expression = control.GetValueBinding(property); if (expression != null && (!control.RenderOnServer || renderEvenInServerRenderingMode)) { writer.AddKnockoutDataBind(name, expression.GetKnockoutBindingExpression(control)); if (valueUpdate != null) { writer.AddKnockoutDataBind("valueUpdate", $"'{valueUpdate}'"); } } else { nullBindingAction?.Invoke(); if (setValueBack && expression != null) { control.SetValue(property, expression.Evaluate(control)); } } }
public static void WriteKnockoutDataBindComment(this IHtmlWriter writer, string name, DotvvmBindableObject control, DotvvmProperty property) { writer.WriteUnencodedText($"<!-- ko { name }: { control.GetValueBinding(property).GetKnockoutBindingExpression(control) } -->"); }
public static void WriteKnockoutDataBindComment(this IHtmlWriter writer, string name, DotvvmBindableObject control, DotvvmProperty property) { writer.WriteKnockoutDataBindComment(name, control.GetValueBinding(property).GetKnockoutBindingExpression(control)); }