Ejemplo n.º 1
0
        protected static string GetBindingScope(ClientBindingLevel bindingLevel)
        {
            switch (bindingLevel)
            {
            case ClientBindingLevel.Self: return("$data");

            case ClientBindingLevel.Parent: return("$parent");

            default: return("$root");
            }
        }
Ejemplo n.º 2
0
        public string BuildBindingContextFor(Expression expression, ClientBindingLevel bindingLevel = ClientBindingLevel.Self)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            switch (expression.NodeType)
            {
            case ExpressionType.Parameter:
            case ExpressionType.MemberAccess:
                return(RecursExpressionToBuildBindingContext(expression, bindingLevel));

            case ExpressionType.Lambda:
                return(RecursExpressionToBuildBindingContext(((LambdaExpression)expression).Body, bindingLevel));
            }

            throw new NotSupportedException(string.Format("The expression type '{0}' is not supported. Expression should be a Lambda or MemberAccess Expression.", expression.NodeType));
        }
Ejemplo n.º 3
0
        private static string AddScopeBindingContext(string context, ClientBindingLevel bindingLevel)
        {
            var contextScope = "";

            switch (bindingLevel)
            {
            case ClientBindingLevel.Parent:
                contextScope = "$parent";
                break;

            case ClientBindingLevel.Root:
                contextScope = "$root";
                break;
            }

            return(string.Format("{0}{1}{2}",
                                 contextScope,
                                 string.IsNullOrEmpty(contextScope) ? string.Empty : ".",
                                 context));
        }
        private static DataBindingAttributeDictionary BuildDataBindingAttributeDictionary(string dataSourceName, string valuePropertyName, ClientBindingLevel bindingLevel, string displayPropertyName, string optionsCaption)
        {
            if (dataSourceName == null) { throw new ArgumentNullException("dataSourceName"); }
            if (string.IsNullOrWhiteSpace(dataSourceName)) { throw new ArgumentException("Data source name cannot be empty.", "dataSourceName"); }

            if (displayPropertyName == null) { throw new ArgumentNullException("displayPropertyName"); }
            if (string.IsNullOrWhiteSpace(displayPropertyName)) { throw new ArgumentException("Display property name cannot be empty.", "displayPropertyName"); }

            var dataBindingAttributeDictionary = new DataBindingAttributeDictionary();
            dataBindingAttributeDictionary.SetAttribute("options", string.Format("{0}.{1}", GetBindingScope(bindingLevel), dataSourceName));
            dataBindingAttributeDictionary.SetAttribute("optionsText", string.Format("'{0}'", displayPropertyName));

            if (!string.IsNullOrWhiteSpace(valuePropertyName))
            {
                dataBindingAttributeDictionary.SetAttribute("optionsValue", string.Format("'{0}'", valuePropertyName));
            }

            if (!string.IsNullOrEmpty(optionsCaption))
            {
                dataBindingAttributeDictionary.SetAttribute("optionsCaption", string.Format("'{0}'", optionsCaption));
            }

            return dataBindingAttributeDictionary;
        }
 /// <summary>
 /// Marks the property to be rendered as a DropDownList with client-side binding attributes.
 /// </summary>
 /// <param name="dataSourceName">The name of the property from which the DropDownList will populate it's options.</param>
 /// <param name="displayPropertyName">The name of the property on the data source object which contains the values to be displayed as the select list option text value.</param>
 /// <param name="valuePropertyName">The optional name of the property on the data source object which contains the values to be bound to the select list's value. When empty, null, or whitespace, the associated attribute will be left off. In Knockout, this will result in the property being bound to the JS object.</param>
 /// <param name="optionsCaption">The optional caption to be displayed as the default (empty) selection of the select list. If null or empty, no caption item will be added.</param>
 /// <param name="bindingLevel">The level at which the view model will find the data source object. By default, the framework will look for the property within the view model's own members.</param>
 public ClientBoundSelectListAttribute(string dataSourceName, string displayPropertyName, 
     string valuePropertyName = null, string optionsCaption = null,
     ClientBindingLevel bindingLevel = ClientBindingLevel.Self)
     : this(BuildDataBindingAttributeDictionary(dataSourceName, valuePropertyName, bindingLevel, displayPropertyName, optionsCaption), "SelectList")
 {
 }
        /// <summary>
        /// Constructs a DataBindingAttributeDictionary consisting of all data binding attributes for supplied expression.
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="expression">An expression indicating the property for which the build the DataBindingAttributeDictionary.</param>
        /// <param name="viewData"></param>
        /// <param name="mode"></param>
        /// <param name="bindingLevel"></param>
        /// <returns></returns>
        public DataBindingAttributeDictionary BuildDataBindingAttributesFor <TModel, TValue>(Expression <Func <TModel, TValue> > expression, ViewDataDictionary <TModel> viewData, DataBindingMode mode, ClientBindingLevel bindingLevel)
        {
            var dataBindingAttributes = new DataBindingAttributeDictionary();

            var dataBindingInfoFactory = new DataBindingInfoFactory();
            var bindingInfo            = dataBindingInfoFactory.CreateDataBindingAttributeInfoFor(expression, mode, bindingLevel);

            dataBindingAttributes.MergeAttribute(bindingInfo);

            //GetClientSideDataBindingAttributesFor(dataBindingAttributes, expression, mode);
            GetClientSideDataBindingAttributesFor(dataBindingAttributes, expression, mode);
            //dataBindingAttributes.MergeAttributes(clientSideDataBindingAttributes);

            return(dataBindingAttributes);
        }
        /// <summary>
        /// Constructs an anonymous object containing the data Knockout.JS binding property and all data binding attributes appropriate for the expression.
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="expression"></param>
        /// <param name="viewData"></param>
        /// <param name="mode"></param>
        /// <param name="bindingLevel"></param>
        /// <param name="additionalViewData"></param>
        /// <returns></returns>
        public KnockoutDataBindingObject BuildHtmlAttributeObjectFor <TModel, TValue>(Expression <Func <TModel, TValue> > expression, ViewDataDictionary <TModel> viewData, DataBindingMode mode, ClientBindingLevel bindingLevel, object additionalViewData)
        {
            var dataBindingAttributes = BuildDataBindingAttributesFor(expression, viewData, mode, bindingLevel);

            return(dataBindingAttributes == null || !dataBindingAttributes.Any()
                       ? null
                       : new KnockoutDataBindingObject(dataBindingAttributes, additionalViewData));
        }
 public static MvcHtmlString BoundDisplayFieldsetFor <TModel, TValue>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TValue> > expression, ClientBindingLevel bindingLevel = ClientBindingLevel.Self, object additionalViewData = null)
 {
     return(htmlHelper.ToKnockoutUIBuilder().DisplayFieldsetFor(expression, bindingLevel, additionalViewData));
 }
Ejemplo n.º 9
0
 public MvcHtmlString DisplayFieldsetFor <TValue>(Expression <Func <TModel, TValue> > expression, ClientBindingLevel bindingLevel, object additionalViewData)
 {
     bindingLevelCurrentElement = bindingLevel;
     return(DisplayFieldsetFor(expression, additionalViewData));
 }
Ejemplo n.º 10
0
        private static string RecursExpressionToBuildBindingContext(Expression expression, ClientBindingLevel bindingLevel, string contextChain = null)
        {
            contextChain = contextChain ?? string.Empty;

            if (expression.NodeType == ExpressionType.Parameter)
            {
                return(AddScopeBindingContext(contextChain, bindingLevel));
            }

            if (expression.NodeType == ExpressionType.MemberAccess)
            {
                // NOTE: Because recursion will walk the chain from the outer-most nesting to the root,
                // the extendedChain prepends the member of the current node to the beginning of the string.
                var extendedChain = string.Format("{0}{1}{2}",
                                                  ((MemberExpression)expression).Member.Name,
                                                  string.IsNullOrWhiteSpace(contextChain) ? "" : ".",
                                                  contextChain);

                return(RecursExpressionToBuildBindingContext(((MemberExpression)expression).Expression, bindingLevel, extendedChain));
            }

            throw new NotSupportedException("Expression is not supported.");
        }
Ejemplo n.º 11
0
        public DataBindingInfo CreateDataBindingAttributeInfoFor <TModel, TValue>(Expression <Func <TModel, TValue> > expression, DataBindingMode mode, ClientBindingLevel bindingLevel)
        {
            if (typeof(TValue).IsComplexType())
            {
                throw new NotSupportedException("Creation of data binding attributes is not supported for complex types.");
            }

            if (expression.Body.NodeType == ExpressionType.Parameter)
            {
                throw new ArgumentException("DataBindingAttributeInfo cannot be created from a self referencing Expression (such as m => m).");
            }

            var bindingContext = _bindingContextHelper.BuildBindingContextFor(expression, bindingLevel);

            _customizations.Where(c => c.IsValidForContext(mode, expression)).ToList()
            .ForEach(c => bindingContext = c.ApplyCustomization(bindingContext));
            var dataBindingInfo = DataBindingInfo.Create(mode, bindingContext);

            //todo: enable customization of DataBinding expression. Examples: click, hasFocus, valueUpdate

            return(dataBindingInfo);
        }