Jul 23, 2013

Javascript OO concepts

Good reading for getting deeper picture on what is JSON and why are function's first class citizens in Javascript.
Must read for any use of JQuery  past the basics.

MANNING - Bear Bibeault, Yehuda Katz "JQuery in Action" - appendix: JavaScript that you need to know but might not!

Jul 18, 2013

Repository pattern

During my experience as .NET developer I've embraced "Repository" pattern from senior developers.
This is rarely used and raises question mark on people faces.
Hence here is not to forget guy who speaks more theoretically on matter.

http://www.codeproject.com/Articles/586364/UsingplusASP-NetplusWebAPIpluswithplusWebplusForms

Jul 17, 2013

IE7 and IE8 Control Focus issue on Page Load

This is here so I don't forget it ...

http://carstent.com/2009/12/31/ie-7-and-8-control-focus-issue-on-page-load/

Dynamic Data Localization

If you use Dynamic Data sooner or later you'll need localization.
Just to note I'm using Entity Framework.
First part shows how to localize model table names.

So here is my inspiration:

http://carstent.com/2009/12/23/aspnet-dynamic-data-4-ui-table-localization/


I've simple translated it to C#. Tested, it works, like charm.

Oh yes, I'm using Log4Net.

Table names

 /// <summary>
    /// Supports localization
    /// </summary>
    public class BackOfficeMetaTable : MetaTable
    {
        private readonly ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public BackOfficeMetaTable(MetaModel metaModel, TableProvider tableProvider) : base(metaModel,tableProvider)
        {
        }
        public BackOfficeMetaTable(MetaModel metaModel, TableProvider tableProvider, string globalResourceClassNameParam):base(metaModel,tableProvider)
        {      
            globalResourceClassName = globalResourceClassNameParam;
        }
        private readonly string globalResourceClassName;
        protected override void Initialize()
        {
            base.Initialize();
        }
        public override string DisplayName
        {
            get
            {
                Log.Info("Try obtaining display name.");
                var baseDisplayName = base.DisplayName;
                if (string.IsNullOrWhiteSpace(globalResourceClassName)) return baseDisplayName;
                string resourceKey = string.Concat("DDtable_", baseDisplayName);
                try
                {                  
                    return HttpContext.GetGlobalResourceObject(globalResourceClassName, resourceKey).ToString();
                }
                catch (System.Exception e)
                {
                    Log.Error(string.Format("Can't find resource key->{0}", resourceKey),e );
                }
                return baseDisplayName;
            }
        }
    }
 /// <summary>
    /// Support for DD localization
    /// </summary>
    public class BackOfficeMetaModel : MetaModel
    {
        public BackOfficeMetaModel() : base()
        {          
        }
        public BackOfficeMetaModel(string globalResourceClassNameParam)
        {
            globalResourcesClassName = globalResourceClassNameParam;
        }
        private readonly string globalResourcesClassName;
        protected override MetaTable CreateTable(System.Web.DynamicData.ModelProviders.TableProvider provider)
        {
            if (string.IsNullOrWhiteSpace(globalResourcesClassName)) return base.CreateTable(provider);
                else return new BackOfficeMetaTable(this, provider, globalResourcesClassName);
        }
    }

Fields


Second part shows how to localize Fields.
I've tried using System.ComponentModel.DataAnnotations.Display attribute.
This required making global resources files public :

http://holyhoehle.wordpress.com/2010/02/20/making-global-resources-public/

It looked all fine on local IIS but on production Release version I keep getting error that res.key can't be found. Don't use this unless you can figure out how to make it work.
Here it is code that uses Display and may give you trouble:

 [ScaffoldTable(true)]
    public class Customer_Metadata
    {
        [Display(ResourceType = typeof(GlobalLocalization), Name = "MenuCustomers", ShortName = "MenuCustomers")]
        public object Name { get; set; }
    }

  [MetadataType(typeof(Customer_Metadata))]
    public partial class Customer
    {      
    } 
GlobalLocalization is Global Resource class name.

I was lucky since my coworker wrote attribute for localization:

Here it is:

public class ResourceDisplayNameAttribute : DisplayNameAttribute
    {
        #region Static Fields
        /// <summary>
        /// The resourceManagers Field
        /// </summary>
        private static readonly SortedList<string, ResourceManager> ResourceManagers = new SortedList<string, ResourceManager>();
        /// <summary>
        /// The syncLock Field
        /// </summary>
        private static readonly object SyncLock = new object();
        #endregion
        #region Fields
        /// <summary>
        ///     replaced field
        /// </summary>
        private bool replaced;
        /// <summary>
        /// The resourceType Field
        /// </summary>
        private readonly string resourceType = "Resources.Shared";
        #endregion
        #region Constructors and Destructors
        /// <summary>
        ///     Initializes a new instance of the <see cref="ResourceDisplayNameAttribute" /> class.
        /// </summary>
        /// <param name="resourceKey">The resource key used for display name.</param>
        public ResourceDisplayNameAttribute(string resourceKey)
            : base(resourceKey)
        {
        }
        public ResourceDisplayNameAttribute(string resourceKey, string resourceType)
            : base(resourceKey)
        {
            this.resourceType = resourceType;
        }
        #endregion
        #region Public Properties
        /// <summary>
        ///     Gets the display name for a property, event, or public void method that takes no arguments stored in this
        ///     attribute.
        /// </summary>
        /// <value></value>
        /// <returns>The display name.</returns>
        public override string DisplayName
        {
            get
            {
                if (!this.replaced)
                {
                    this.replaced = true;
                    this.DisplayNameValue = this.GetString();
                }
                return base.DisplayName;
            }
        }
        /// <summary>
        /// Gets the string.
        /// </summary>
        /// <returns></returns>
        private string GetString()
        {
            ResourceManager resourceManager = GetResourceManager(this.resourceType);
            return resourceManager.GetString(base.DisplayName);
        }
        /// <summary>
        /// Gets the resource manager.
        /// </summary>
        /// <param name="resourceType">Type of the resource.</param>
        /// <returns></returns>
        private static ResourceManager GetResourceManager(string resourceType)
        {
            lock (SyncLock)
            {
                if (ResourceManagers.ContainsKey(resourceType))
                {
                    return ResourceManagers[resourceType];
                }
                ResourceManager resourceManager = new ResourceManager(resourceType, System.Reflection.Assembly.Load("App_GlobalResources"));
                if (!ResourceManagers.ContainsKey(resourceType))
                {
                    ResourceManagers.Add(resourceType, resourceManager);
                }
                return ResourceManagers[resourceType];
            }
        }
        /// <summary>
        ///     When implemented in a derived class, gets a unique identifier for this <see cref="T:System.Attribute" />.
        /// </summary>
        /// <value></value>
        /// <returns>An <see cref="T:System.Object" /> that is a unique identifier for the attribute.</returns>
        public override object TypeId
        {
            get
            {
                return typeof(DisplayNameAttribute);
            }
        }
        #endregion
    }
You can use it like this:

  [ScaffoldTable(true)]
    public class Customer_Metadata
    {      
        [ResourceDisplayName("CustomerName", "Resources.DBColumns")]
        public string Name { get; set; }    

Foreign key

By default each list has on top list of foreign key filters. You localize them same way as fields above just reference correct property from model like bellow or you could use above custom attribute.

  [MetadataType(typeof(User_Metadata))]
    public partial class User
    {
    }
[ScaffoldTable(true)]
    public class User_Metadata
    {
        [Display(ResourceType=typeof(App_GlobalResources.GlobalLocalization),Name="MenuCustomers",ShortName="MenuCustomers")]
        public virtual Customer Customer { get; set; }
    }

Jul 8, 2013

Phonegap build - how to build app that uses Cordova API?

Key power of Phonegap project is its cloud base build service.
You create website, build it using Phonegap Build and you get native app.
Great!
But another part of Phonegap project is Phonegap API.
You use it to access mobile device's resources (GPS, storage etc.).
So you extend your website/app with support for Phonegap API.
But how to build it ?
Official docs guides, depending on OS development platform, install Eclipse, SDK's etc.
In this approach we are responsible to build app.

So you say no! I want to build my website/api that uses Cordova (phonegap) API using Phonegap Build cloud service.

Now here comes the catch 22 !
When you develop website with Cordova API and build it on your own you are expected to use something like this:

<script type="text/javascript" src="js/cordova-2.5.0.js"></script>

But when you want the very same code to build on Phonegap Build service you must drop above line and use this :

<script type="text/javascript" src="phonegap.js"></script>

Already twice I've lost some time to figure this out.
I don't know am I dumb or what but this info is not on any get started pages or I did not find them.

Here is officially Github repo of phonegap examples that uses above approach:

https://github.com/phonegap/phonegap-app-hello-world

Jul 5, 2013

Delaying javascript execution

Delaying execution of javascript until certain condition is fulfilled :

    function WaitForAnsycPostback(cb) {
        var requestManager = Sys.WebForms.PageRequestManager.getInstance();
        if (requestManager.get_isInAsyncPostBack()) {
            setTimeout(function () {
                WaitForAnsycPostback(cb);
            }, 250);
        }
        else {
            if (typeof(cb) == 'function') cb();
        }
    }