how to extend builder classes

  1. #1

    how to extend builder classes

    i am using ext.net 3.0 and mvc on my project, i'm extending ext.net components to get my own components, i added some extension methods to TextFieldBase for simplifying to create my views, i added to TextFieldBase for getting same functionality in TextField and Combobox,

    i'm creating builder classes but i dont want to add same methods for combobox.builder class and textbox.builder class one by one.

    how can i extend TextFieldBase.Builder class?
  2. #2
    Hello,

    Maybe the following thread can help:

    http://forums.ext.net/showthread.php...ll=1#post35928
    Geoffrey McGill
    Founder
  3. #3
    Hello,

    This might be helpful as well.
    Creating Razor helper methods for custom controls
  4. #4
    thank you for messages, Daniil's that post was my starting point in the first place,
    let me explain my case


    MyTextField.cs

    public partial class MyTextField:Ext.Net.TextField
    {
            public  MyTextField():base()
            {
                this.SetFieldClass();
            }
    
           //.....
          // override InstanceOf, XType, Resources properties
          //.... any other individual functions
    }
    MyComboBox.cs
    public partial class MyComboBox:Ext.Net.ComboBox
    {
            
            public MyComboBox():base()
            {
                this.SetFieldClass();
            }
          //.....
          // override InstanceOf, XType, Resources properties
          //.... any other individual functions
    }
    MyGridPanel.cs
    public partial class MyGridPanel:Ext.Net.GridPanel
        {
            public MyGridPanel():base()
            {
               
            }
             //.....
          // override InstanceOf, XType, Resources properties
          //.... any other individual functions
    }
    FieldExtensions.cs
    public static class FieldExtensions
    {
            public static void SetFieldClass(this Field field)
            {
                field.FieldCls = "form-control";
                if (field is TextFieldBase)
                    (field as TextFieldBase).TriggerWrapCls = "my-form-trigger-wrap";
                field.InvalidCls = "my-form-invalid";
            }
    }
    TextFieldBaseExtensions.cs
    public static class TextFieldBaseExtensions
    {
            public static void AddTrigger(this TextFieldBase owner, FieldTrigger.Config config)
            {
                owner.Triggers.Add(new FieldTrigger(config));
            }
    
            public static void AddTrigger(this TextFieldBase owner, TriggerIcon icon, string handler, string qTip = "", string tag = "")
            {
                
                owner.Triggers.Add(
                    new FieldTrigger(
                        new FieldTrigger.Config()
                        {
                            Icon = icon,
                            Handler = handler,
                            QTip = qTip,
                            Tag = tag
    
                        }
                   )
                );
                owner.TriggerWrapCls = "";
            }
    
    }
    StoreExtensions.cs
    public static class StoreExtensions
    {
            public static void BuildStore(this IStore<Store> storeOwner, string url, IEnumerable<StoreParameter> paramList)
            {
    
                var store = (storeOwner.Store.Count == 1) ? (storeOwner.Store[0]) : (new Store());
                var proxy = new AjaxProxy();
                proxy.Reader.Add(new JsonReader() { RootProperty = "data" });
                proxy.Url = url;
                store.Proxy.Add(proxy);
    
                if (paramList != null)
                {
                    foreach (var item in paramList)
                    {
                        store.Parameters.Add(item);
                    }
                }
    
                if (storeOwner.Store.Count == 1)
                {
                    // storeOwner.Store[0] = store;
                }
                else
                {
                    storeOwner.Store.Add(store);
                }
            }
    
            public static void BuildStore(this IStore<Store> storeOwner, string url, StoreParameter param)
            {
                List<StoreParameter> pl = null;
                if (param != null)
                {
                    pl = new List<StoreParameter>() { param };
                }
    
                BuildStore(storeOwner, url, pl);
            }
    
            public static void BuildStore(this IStore<Store> storeOwner, string url)
            {
                BuildStore(storeOwner, url, (StoreParameter)null);
            }
    
            public static void SetStoreID(this IStore<Store> storeOwner, string storeID)
            {
                if ((storeOwner.Store != null) && (storeOwner.Store.Count == 1))
                {
                    storeOwner.Store[0].ID = storeID;
                }
            }
    
            public static void AddStoreParam(this IStore<Store> storeOwner, StoreParameter p)
            {
                if ((storeOwner.Store != null) && (storeOwner.Store.Count == 1))
                {
                    storeOwner.Store[0].Parameters.Add(p);
                    storeOwner.Store[0].AutoLoad = false;
                }
            }
    
            public static void SetStoreModel(this IStore<Store> storeOwner, Type modelType)
            {
                var store = (storeOwner.Store.Count == 1) ? (storeOwner.Store[0]) : (new Store());
    
                var m = new Model();
    
    
                foreach (var p in modelType.GetProperties())
                {
                    ModelFieldType mft = ModelFieldType.Auto;
    
                    if (p.PropertyType == typeof(int))
                        mft = ModelFieldType.Int;
    
                    m.Fields.Add(new ModelField(p.Name, mft));
                }
    
                store.Model.Add(m);
    
    
                if (storeOwner.Store.Count == 1)
                {
                    // storeOwner.Store[0] = store;
                }
                else
                {
                    storeOwner.Store.Add(store);
                }
            }
    
            public static void SetStoreModel<TModelType>(this IStore<Store> storeOwner)
            {
                SetStoreModel(storeOwner, typeof(TModelType));
            }
    
            public static void SetStoreModel(this IStore<Store> storeOwner, Model model)
            {
                var store = (storeOwner.Store.Count == 1) ? (storeOwner.Store[0]) : (new Store());
                store.Model.Add(model);
    
                if (storeOwner.Store.Count == 1)
                    storeOwner.Store[0] = store;
                else
                    storeOwner.Store.Add(store);
            }
    
            public static void ItemsFromAction(this IStore<Store> storeOwner, string url, StoreParameter param)
            {
                storeOwner.BuildStore(url, param);
    
                string id = (storeOwner is BaseControl) ? ((storeOwner as BaseControl).ID) : null;
    
                if (id != null)
                    storeOwner.GetStore().ID = id + "Store";
            }
    
            public static void ItemsFromAction(this IStore<Store> storeOwner, string url)
            {
                storeOwner.BuildStore(url);
                
                string id = (storeOwner is BaseControl) ? ((storeOwner as BaseControl).ID) : null;
    
                if (id != null)
                    storeOwner.GetStore().ID = id + "Store";
            }
    
            public static void ItemsFromAction(this IStore<Store> storeOwner, string url, IEnumerable<StoreParameter> paramList)
            {
                storeOwner.BuildStore(url, paramList);
                string id = (storeOwner is BaseControl) ? ((storeOwner as BaseControl).ID) : null;
    
                if (id != null)
                    storeOwner.GetStore().ID = id + "Store";
            }
    
            public static void StoreUrl(this IStore<Store> storeOwner, string url)
            {
                if ((storeOwner.Store != null) && (storeOwner.Store.Count == 1))
                {
                    var proxy = new AjaxProxy();
                    proxy.Reader.Add(new JsonReader() { RootProperty = "data" });
                    proxy.Url = url;
                    storeOwner.Store[0].Proxy.Add(proxy);
                }
            }
    
    }

    i want MyTextBox.Builder and MyComboBox.Builder has methods for implemented in TextFieldBaseExtensions
    and also MyGridPanel.Builder and MyComboBox.Builder has methods for implemented in StoreExtensions.

    i can add methods for builder classes one by one, but i want to learn can i extend base builder classes (TextFieldBase.Builder, IStore.Builder) and get rid of copy/paste
  5. #5
    any ideas?
  6. #6
    Hi

    I don't know if it helps but chapter 9 of my Ext.NET book covers this with the section on

    Reusing Ext.NET's builder pattern for a richer custom MVC framework for your own application

    https://www.packtpub.com/web-develop...on-development

    I am not at my desk right now so cannot try your actual example but I hope that helps.

    Although the book targets Ext.NET 2.X the same principles will apply with hardly any code changes for 3.x. The Ext.NET team helped a lot so I think that should be a solid chapter and hopefully be what you are looking for?
    Last edited by anup; Jan 21, 2015 at 4:41 AM.
  7. #7
    Also, I just remembered that the chapter I referred to was influenced by a couple of forum posts on this:

    http://forums.ext.net/showthread.php...as-MVC-Helpers
    http://forums.ext.net/showthread.php...Loader-problem

    You might need subtle changes for 3.x but I think those principles still apply more generally.

    Hope that helps (and may save you from having to buy the book :))
  8. #8
    thanks for answers,
    it was hard to get understand both Builder pattern and generic type arguments work with, but finally i got it,

    FieldExtensions.cs
    public static class FieldExtensions
        {
            public static void SetFieldClass(this Field field)
            {
                field.FieldCls = "form-control";
                if (field is TextFieldBase)
                    (field as TextFieldBase).TriggerWrapCls = "saye-form-trigger-wrap";
                field.InvalidCls = "saye-form-invalid";
            }
    
            public static ComponentListener GetChangeListener(this Field field)
            {
                if (field is TextField)
                    return (field as TextField).Listeners.Change;
                else if (field is TextArea)
                    return (field as TextArea).Listeners.Change;
                else if (field is ComboBox)
                    return (field as ComboBox).Listeners.Change;
                else if (field is DateField)
                    return (field as DateField).Listeners.Change;
                else if (field is DropDownField)
                    return (field as DropDownField).Listeners.Change;
                else if (field is NumberField)
                    return (field as NumberField).Listeners.Change;
                else if (field is SpinnerField)
                    return (field as SpinnerField).Listeners.Change;
                else if (field is Hidden)
                    return (field as Hidden).Listeners.Change;
                else
                    throw new Exception("Field type error. Field: " + field.ClassName);
            }
    
            public static void ControlToSetValueOnChange(this Field field, string controlId)
            {
                ComponentListener change = field.GetChangeListener();
                controlId = "App." + controlId;
    
    
                if (change != null)
                    change.Handler += " if (" + controlId + ") { " + controlId + ".setValue(this.getValue()); } ";
            }
    
            public static void FormToResetOnChange(this Field field,string formId)
            {
                var change = field.GetChangeListener();
                if (change != null)
                    change.Handler += " var form=App." + formId + ".getForm(); form.reset();";
            }
        }
    
        public static class FieldBuilderExtensions
        {
            public static TBuilder ControlToSetValueOnChange<TField, TBuilder>(this Field.Builder<TField, TBuilder> builder, string controlId)
                where TField : Ext.Net.Field
                where TBuilder : Ext.Net.Field.Builder<TField, TBuilder>
            {
                builder.ToComponent().ControlToSetValueOnChange(controlId);
                return builder as TBuilder;
            }
    
            public static TBuilder FormToResetOnChange<TField, TBuilder>(this Field.Builder<TField, TBuilder> builder, string formId)
                where TField : Ext.Net.Field
                where TBuilder : Ext.Net.Field.Builder<TField, TBuilder>
            {
                builder.ToComponent().FormToResetOnChange(formId);
                return builder as TBuilder;
            }
        }
    TextFieldBaseExtensions.cs
    public static class TextFieldBaseExtensions
        {
            public static void AddTrigger(this TextFieldBase owner, FieldTrigger.Config config)
            {
                owner.Triggers.Add(new FieldTrigger(config));
            }
    
            public static void AddTrigger(this TextFieldBase owner, TriggerIcon icon, string handler, string qTip = "", string tag = "")
            {
                owner.Triggers.Add(
                    new FieldTrigger(
                        new FieldTrigger.Config()
                        {
                            Icon = icon,
                            Handler = handler,
                            QTip = qTip,
                            Tag = tag
                        }
                   )
                );
                owner.TriggerWrapCls = "";
            }
    
        }
    
        public static class TextFieldBaseBuilderExtensions        
        {
            public static TBuilder AddTrigger<TField, TBuilder>(this TextFieldBase.Builder<TField, TBuilder> builder, FieldTrigger.Config config)
                where TField : Ext.Net.TextFieldBase
                where TBuilder : Ext.Net.TextFieldBase.Builder<TField, TBuilder>
            {
                builder.ToComponent().AddTrigger(config);
                return builder as TBuilder;
            }
    
            public static TBuilder AddTrigger<TField, TBuilder>(this TextFieldBase.Builder<TField, TBuilder> builder, TriggerIcon icon, string handler, string qTip = "", string tag = "")
                where TField : Ext.Net.TextFieldBase
                where TBuilder : Ext.Net.TextFieldBase.Builder<TField, TBuilder>
            {
                builder.ToComponent().AddTrigger(icon, handler, qTip, tag);
                return builder as TBuilder;
            }
       
        }
    TablePanelExtensions.cs
    public static class TablePanelExtensions
        {
            public static ColumnBase GetColumnByDataIndex(this TablePanel control, string dataIndex)
            {
                ColumnBase result = null;
                try
                {
                    result = control.ColumnModel.GetColumnByDataIndex(dataIndex);
                }
                catch
                {
    
                }
    
                if (result == null)
                {
                    foreach (var item in control.ColumnModel.Columns)
                    {
                        if ((item.DataIndex != null) && (item.DataIndex == dataIndex))
                        {
                            result = item;
                            break;
                        }
                    }
                }
    
                return result;
            }
    
    
            public static void HideColumns(this TablePanel control, string columns)
            {
                string[] columnList = columns.Split(',');
    
                foreach (var column in columnList)
                {
                    var col = GetColumnByDataIndex(control, column);
                    if (col != null)
                        col.Hidden = true;
                }
            }
    
            public static void SetColumnTitle(this TablePanel control, string columnName, string title)
            {
                var col = GetColumnByDataIndex(control, columnName);
                if (col != null)
                    col.Text = title;
            }
    
            public static void HideIdColumns(this TablePanel control)
            {
                foreach (var item in control.ColumnModel.Columns)
                {
                    if ((item.DataIndex != null) && (item.DataIndex.EndsWith("Id")))
                    {
                        item.Hidden = true;
                    }
                }
            }
    
            public static void AddRowEditingPlugin(this TablePanel control)
            {
                var cl = control.Plugins.Find(p => (p.PType == "rowediting"));
                if (cl == null)
                    control.Plugins.Add(new RowEditing());
            }
    
        }
    
        public static class TablePanelBuilderExtensions
        {
            public static TBuilder HideColumns<TTablePanel, TBuilder>(this TablePanel.Builder<TTablePanel, TBuilder> builder, string columns)
                where TTablePanel : Ext.Net.TablePanel
                where TBuilder : Ext.Net.TablePanel.Builder<TTablePanel, TBuilder>
            {
                builder.ToComponent().HideColumns(columns);
                return builder as TBuilder;
            }
    
            public static TBuilder SetColumnTitle<TTablePanel, TBuilder>(this TablePanel.Builder<TTablePanel, TBuilder> builder, string columnName, string title)
                where TTablePanel : Ext.Net.TablePanel
                where TBuilder : Ext.Net.TablePanel.Builder<TTablePanel, TBuilder>
            {
                builder.ToComponent().SetColumnTitle(columnName, title);
                return builder as TBuilder;
            }
    
            public static TBuilder HideIdColumns<TTablePanel, TBuilder>(this TablePanel.Builder<TTablePanel, TBuilder> builder)
                where TTablePanel : Ext.Net.TablePanel
                where TBuilder : Ext.Net.TablePanel.Builder<TTablePanel, TBuilder>
            {
                builder.ToComponent().HideIdColumns();
                return builder as TBuilder;
            }
    
            public static TBuilder AddRowEditingPlugin<TTablePanel, TBuilder>(this TablePanel.Builder<TTablePanel, TBuilder> builder)
                where TTablePanel : Ext.Net.TablePanel
                where TBuilder : Ext.Net.TablePanel.Builder<TTablePanel, TBuilder>
            {
                builder.ToComponent().AddRowEditingPlugin();
                return builder as TBuilder;
            }
        }

    etc.


    thread can be closed, thank you again

Similar Threads

  1. Replies: 15
    Last Post: Sep 12, 2013, 6:01 AM
  2. Replies: 8
    Last Post: Aug 13, 2012, 11:47 PM
  3. Any example of custom css classes?
    By vadym.f in forum 1.x Help
    Replies: 6
    Last Post: Jan 23, 2012, 8:09 PM
  4. [CLOSED] ItemID & Builder
    By PLoch in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Aug 26, 2011, 8:48 PM
  5. grid panel css classes
    By [WP]joju in forum 1.x Help
    Replies: 4
    Last Post: Apr 01, 2009, 9:41 AM

Posting Permissions