예제 #1
0
        public BindingExpressionCompilationInfo PrecompileBinding(ResolvedBinding binding, string id, Type expectedType)
        {
            var compilerAttribute = GetCompilationAttribute(binding.BindingType);
            var requirements = compilerAttribute.GetRequirements(binding.BindingType);

            var result = new BindingExpressionCompilationInfo();
            result.MethodName = TryExecute(binding.BindingNode, "Error while compiling binding to delegate.", requirements.Delegate, () => CompileMethod(compilerAttribute.CompileToDelegate(binding.GetExpression(), binding.DataContextTypeStack, expectedType)));
            result.UpdateMethodName = TryExecute(binding.BindingNode, "Error while compiling update delegate.", requirements.UpdateDelegate, () => CompileMethod(compilerAttribute.CompileToUpdateDelegate(binding.GetExpression(), binding.DataContextTypeStack)));
            result.OriginalString = TryExecute(binding.BindingNode, "hey, no, that should not happen. Really.", requirements.OriginalString, () => binding.Value);
            result.Expression = TryExecute(binding.BindingNode, "Could not get binding expression.", requirements.Expression, () => binding.GetExpression());
            result.ActionFilters = TryExecute(binding.BindingNode, "", requirements.ActionFilters, () => GetActionAttributeData(binding.GetExpression()));
            result.Javascript = TryExecute(binding.BindingNode, "Could not compile binding to Javascript.", requirements.Javascript, () => compilerAttribute.CompileToJavascript(binding, new CompiledBindingExpression()
            {
                Expression = result.Expression,
                Id = id,
                OriginalString = result.OriginalString
            }));
            return result;
        }
		public override string CompileToJavascript(ResolvedBinding binding, CompiledBindingExpression compiledExpression)
		{
			var expression = binding.GetExpression();

			var visitor = new ExtractExpressionVisitor(ex => ex.NodeType == ExpressionType.Call);
			var rootCallback = visitor.Visit(expression);
			var js = SouldCompileCallback(rootCallback) ? "resultPromise.resolve(" + JavascriptTranslator.CompileToJavascript(rootCallback, binding.DataContextTypeStack) + ")" : null;
			foreach (var param in visitor.ParameterOrder.Reverse<ParameterExpression>())
			{
				if (js == null) js = $"resultPromise.resolve({param.Name})";
				var callback = $"function({param.Name}){{{js}}}";
				var method = visitor.Replaced[param] as MethodCallExpression;
				js = CompileMethodCall(method, binding.DataContextTypeStack, callback);
			}
			return "var $context = ko.contextFor(this);var sender = this;var resultPromise = new DotvvmPromise();(function(i_pageArea){with($context){" + js + "}})";
		}
예제 #3
0
        public virtual string CompileToJavascript(ResolvedBinding binding, CompiledBindingExpression expression)
        {
            var javascript = JavascriptTranslator.CompileToJavascript(binding.GetExpression(), binding.DataContextTypeStack);

            if (javascript == "$data")
            {
                javascript = "$rawData";
            }
            else if (javascript.StartsWith("$data.", StringComparison.Ordinal))
            {
                javascript = javascript.Substring("$data.".Length);
            }

            // do not produce try/eval on single properties
            if (javascript.Contains(".") || javascript.Contains("("))
            {
                return "dotvvm.evaluator.tryEval(function(){return " + javascript + "})";
            }
            else
            {
                return javascript;
            }
        }
예제 #4
0
 public virtual Expression GetExpression(ResolvedBinding binding)
 {
     return binding.GetExpression();
 }