how to build store model for complex objects (objects has objects as property)

  1. #1

    how to build store model for complex objects (objects has objects as property)

    I'm trying to implement LocalStorage for store some data on user's browser but i have a problem,
    I want to use complex object as store model like

    // sample dto models
    public class PersonInfoForLocalStorage
    {
          public string NameSurname { get; set; }
          public int PersonId { get; set; }
          public PersonalInfo PersonalInfo { get; set; }
          public ExaminationInfo ExaminationInfo { get; set; }
          
          public PersonInfoForLocalStorage()
          {
                this.PersonalInfo = new PersonalInfo ();
                this.ExaminationInfo = new ExaminationInfo ();
          }
    }
    
    public class PersonalInfo 
    {
          public DateTime DateOfBirth { get; set; }
          public string Gender  { get; set; }
          public string FathersName  { get; set; }
          public string MothersName  { get; set; } 
    }
    
    public class ExaminationInfo 
    {
          public DateTime OperationTime { get; set; }
          public List<PartInfo> PartList     { get; set; }  
    }
    
    public class PartInfo
    {
        public int PartId  { get; set; } 
        public string PartName  { get; set; } 
    }
    and i prepared extension methods for Model and Store classes as
    public static class StoreExtensions
    {
          public static void AddFieldsByType(this Model model,Type t)
            {
                model.Name =  "MyApp.Models."+t.Name;
                foreach (var p in t.GetProperties())
                {
                    ModelFieldType mft = ModelFieldType.Auto;
                    ModelField mf = null;
                    Type pt = p.PropertyType;
                    if ((pt.IsAnsiClass) && (pt.IsValueType) && (pt.Name == "Nullable`1"))
                    {
                        pt = pt.GenericTypeArguments[0];
                    }
                    if (pt == typeof(int))
                        mft = ModelFieldType.Int;
                    else if (pt == typeof(bool))
                        mft = ModelFieldType.Boolean;
                    else if (pt == typeof(DateTime))
                        mft = ModelFieldType.Date;
                    else if (pt == typeof(decimal))
                        mft = ModelFieldType.Float;
                    else if (pt == typeof(string))
                        mft = ModelFieldType.String;
                    else if ((pt.IsAnsiClass) && (!pt.IsArray) && (!pt.IsValueType) && (!pt.IsGenericType))
                    {
                        mft = ModelFieldType.Object;
                        mf = new ModelField(p.Name, mft);
                        
                        Model m2 = new Model();
                        m2.AddFieldsByType(pt);
                        if (!Model.IsRegistered(m2.Name))
                            m2.Register(false);
                        //m2.RegisterOnClient();
                        //mf.ModelName = m2.Name;
                        mf.Fields.AddRange(m2.Fields);
                        //mf.Model.Add(m2);
                    }
                    
                    if (mf == null)
                        mf = new ModelField(p.Name, mft);
                    model.Fields.Add(mf);
                    if (!Model.IsRegistered(model.Name))
                        model.Register(false);
                   // model.RegisterOnClient();
                }
            }
    
             public static void SetStoreModel(this Store store, Type modelType)
            {
                var m = new Model();
                m.AddFieldsByType(modelType);
                store.Model.Clear();
                store.Model.Add(m);
            }
    
           public static void BuildStore(this Store store, string storageId, Type modelType, bool storeAutoLoad)
            {
                var proxy = new LocalStorageProxy();
                proxy.StorageID = storageId;
                store.Proxy.Clear();
                store.Proxy.Add(proxy);
                store.AutoLoad =store.AutoSync=  storeAutoLoad;
                store.SetStoreModel(modelType);
            }
    
           public static TBuilder AddFieldsByType<TModel, TBuilder>(this Model.Builder<TModel, TBuilder> builder, Type modelType)
                where TModel : Ext.Net.Model
                where TBuilder : Ext.Net.Model.Builder<TModel, TBuilder>
            {
                builder.ToComponent().AddFieldsByType(modelType);
                return builder as TBuilder;
            }
    
            public static  TBuilder SetStoreModel<TStore, TBuilder>(this Store.Builder<TStore, TBuilder> builder, Type modelType)
                where TStore : Ext.Net.Store
                where TBuilder : Ext.Net.Store.Builder<TStore, TBuilder>
            {
                builder.ToComponent().SetStoreModel(modelType);
                return builder as TBuilder;
            }
    
           public static  TBuilder BuildStore<TStore, TBuilder>(this Store.Builder<TStore, TBuilder> builder, 
                   string storageId, Type modelType, bool storeAutoLoad)
                where TStore : Ext.Net.Store
                where TBuilder : Ext.Net.Store.Builder<TStore, TBuilder>
            {
                builder.ToComponent().BuildStore(storageId,modelType,storeAutoLoad);
                return builder as TBuilder;
            }
    }
    after all, i create store in my view as

    @(Html.X().Store().ID("MyLocalStore").BuildStore("myLocalStorage", typeof(PersonInfoForLocalStorage), true))
    when i run my project and create model object in js i get only NameSurname and PersonId of my model,
    console.log(new App.MyLocalStore.model());
  2. #2
    Basically, i need to create my model object on javascript side, which is a object of a complex class.
    how should i define my ext.net model ???

Similar Threads

  1. Replies: 8
    Last Post: Jun 25, 2014, 7:59 AM
  2. Replies: 3
    Last Post: May 29, 2014, 8:26 PM
  3. [CLOSED] Add/Save Complex Objects?
    By peter.campbell in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Aug 04, 2011, 5:35 PM
  4. Saving grid changes with complex objects
    By fquintero in forum 1.x Help
    Replies: 3
    Last Post: Oct 13, 2009, 1:19 PM
  5. mapping more complex objects to a Store
    By principal_X in forum 1.x Help
    Replies: 2
    Last Post: Jan 26, 2009, 6:16 PM

Posting Permissions