public static string GenerateClientPostBackScript(string propertyName, ICommandBinding expression, RenderContext context, DotvvmControl control,
                                                          bool useWindowSetTimeout = false, bool?returnValue = false, bool isOnChange = false)
        {
            var uniqueControlId = "";

            if (expression is ControlCommandBindingExpression)
            {
                var target = (DotvvmControl)control.GetClosestControlBindingTarget();
                target.EnsureControlHasId();
                uniqueControlId = target.ID;
            }

            var arguments = new List <string>()
            {
                "'" + context.CurrentPageArea + "'",
                "this",
                "[" + String.Join(", ", GetContextPath(control).Reverse().Select(p => '"' + p + '"')) + "]",
                "'" + uniqueControlId + "'",
                useWindowSetTimeout ? "true" : "false",
                JsonConvert.SerializeObject(GetValidationTargetExpression(control)),
                "null",
                GetPostBackHandlersScript(control, propertyName)
            };

            // return the script
            var condition       = isOnChange ? "if (!dotvvm.isViewModelUpdating) " : null;
            var returnStatement = returnValue != null?string.Format(";return {0};", returnValue.ToString().ToLower()) : "";

            // call the function returned from binding js with runtime arguments
            var postBackCall = String.Format("{0}({1})", expression.GetCommandJavascript(), String.Join(", ", arguments));

            return(condition + postBackCall + returnStatement);
        }
Exemple #2
0
        public static string GenerateClientPostBackScript(string propertyName, ICommandBinding expression, DotvvmControl control, PostbackScriptOptions options)
        {
            object uniqueControlId = null;
            if (expression is ControlCommandBindingExpression)
            {
                var target = (DotvvmControl)control.GetClosestControlBindingTarget();
                uniqueControlId = target.GetDotvvmUniqueId() as string;
            }

            var arguments = new List<string>()
            {
                "'root'",
                options.ElementAccessor,
                "[" + String.Join(", ", GetContextPath(control).Reverse().Select(p => '"' + p + '"')) + "]",
                (uniqueControlId is IValueBinding ? ((IValueBinding)uniqueControlId).GetKnockoutBindingExpression() : "'" + (string) uniqueControlId + "'"),
                options.UseWindowSetTimeout ? "true" : "false",
                JsonConvert.SerializeObject(GetValidationTargetExpression(control)),
                "null",
                GetPostBackHandlersScript(control, propertyName)
            };

            // return the script
            var condition = options.IsOnChange ? "if (!dotvvm.isViewModelUpdating) " : null;
            var returnStatement = options.ReturnValue != null ? $";return {options.ReturnValue.ToString().ToLower()};" : "";

            // call the function returned from binding js with runtime arguments
            var postBackCall = $"{expression.GetCommandJavascript()}({String.Join(", ", arguments)})";
            return condition + postBackCall + returnStatement;
        }