Pages

Saturday, December 28, 2013

Page Life Cycle in Asp.net MVC


Page Life Cycle in Asp.net MVC

Thank you Simone Chiaretta, I enjoy flow diagrams, especially your flow diagram presented below.




This post is for the purpose of my notes only.

“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford

Tuesday, December 24, 2013

JavaScript A Great Christmas Present

Christmas December 25, 2013, I received a great Christmas present.

How all this came to be, I had a JavaScript question. Searching the Web I didn't find a satisfactory answer, so I thought maybe someone who had written books on the topic would have an answer to my itching question.

A little history, I have been reading and writing JavaScript for a good number of years. I purchased Danny Goodman's JavaScript book JavaScript & DHTML Cookbook, 2nd Edition. A great book, as I have revisited again and again.

The Christmas present, I decided emailing Danny Goodman prompting him with my question. Within the same day, Danny Goodman responded with a great answer.

The post is to thank Danny Goodman and all who take the time helping others.


Danny Goodman's answer:

If I recall correctly, IE4 was the first browser to scope HTML element IDs as global variables. But it has become standard behavior in the mainstream browsers you’d be coding for these days (even going back some generations). Additionally, because the W3C HTML specifications say an element’s ID attribute should be unique within the document tree, I would expect the DOM behavior to continue onward.

In practice, it can make life difficult because it adds lots of objects to your global scope, and you have to be careful in how you name functions and objects intended for global scope to avoid name collisions (including collisions with globals specified in .js libraries you load into a document). On the other hand, it forces me to carefully consider adding _anything_ JavaScript to the global scope.

Danny

This post is for the purpose of my notes only.

“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford

Sunday, December 22, 2013

Visual Studio TypeScript Defaults to ES5

TypeScript has jump the gun. ES5 as the default? I'm still working with clients on <= IE9.

Additionally in Visual Studio 2013, Web Essentials has stopped supporting TypeScript. Now changing between ES3 and ES5 isn’t easy as selecting tools > options > Web Essentials and changing to ES3/5.

However I needed the default to be ES3. Looking for an answer, I discovered TypeScript in Visual Studio 2012 defaulted to ES3, and there were a lot of question how to default to ES5. I reversed engineered their answer having TypeScript in Visual Studio 2012 defaulting to ES5.

Opening up the application’s program file (myProj.csproj) and appending, the code below, near the top of the project file, solved my issue. Now within Visual Studio 2013, TypeScript defualts to ES3.

   1: <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
   2:     <TypeScriptTarget>ES3</TypeScriptTarget>
   3:     <TypeScriptIncludeComments>true</TypeScriptIncludeComments>
   4:     <TypeScriptSourceMap>true</TypeScriptSourceMap>
   5:     <TypeScriptModuleKind>AMD</TypeScriptModuleKind>
   6:   </PropertyGroup>
   7:   <PropertyGroup Condition="'$(Configuration)' == 'Release'">
   8:     <TypeScriptTarget>ES3</TypeScriptTarget>
   9:     <TypeScriptIncludeComments>false</TypeScriptIncludeComments>
  10:     <TypeScriptSourceMap>false</TypeScriptSourceMap>
  11:     <TypeScriptModuleKind>AMD</TypeScriptModuleKind>
  12:   </PropertyGroup>
Because Web Essentials  is no longer supported in Visual Studio 2013, the screen isn’t split with the TypeScript file on the left and JavaScript on the right, This isn’t an issue for me.

Now after saving the TypeScript file, a hidden JavaScript file is still created, and if I want the same Web Essentials experience, I vertically split my window with my TypeScript file on the left and Javascript is placed on the right.

Capture


This post is for the purpose of my notes only.

“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford

Sunday, December 15, 2013

JavaScript Interface Class

Working on a simple JavaScript idea with implementing Interfaces. Some thought, I have come with this idea. I haven’t worked with it.

For better readability, I extended the Function class:

   1: //Enforces similar data types and has been overridden
   2: Function.prototype.interface = function () {
   3:     if (arguments.length < 1) {
   4:         throw new Error("Interface constructor called with no arguments.");
   5:     }
   6:     var dv = (document.defaultView) ? document.defaultView : document.parentWindow; //Covers < IE9
   7:  
   8:     for (var i = 0,length = arguments.length; i < length; i++) {
   9:         var v = (dv[arguments[i]])
  10:             ? new dv[arguments[i]]
  11:             : {};
  12:         for (var j in v) {
  13:             if (this.hasOwnProperty(j)) {
  14:                 continue;
  15:             }
  16:  
  17:             //Set to generic function enforcing similar types.
  18:             this.prototype[j] = (function(item) {
  19:                 return function (value, overridden) {
  20:                     if (!(overridden && overridden.OR)) {
  21:                         throw new Error("Must be overridden.")
  22:                     }
  23:                     if (Object.prototype.toString.call(value) !== Object.prototype.toString.call(item)) {
  24:                         throw new Error("Members are different types.")
  25:                     };
  26:                     return value;
  27:  
  28:                 }}(v[j]));
  29:         }
  30:     }
  31:  
  32:     return this;
  33: };
  34: Function.prototype.override = function (value) {
  35:     return this.call(null, value,{OR:true});
  36: };
Create class interface without implementation:


   1: var Class1 = function Class1() {
   2:     this.add = {};
   3:     this.subtract = [];
   4: };
   5:  
   6: var Class2 = function () {
   7:     this.times = function () { };
   8:     this.multiply = 9;
   9: };
Create composite that relies on the two interfaces:


   1: var Class3 = function () {
   2:     this.car = "blue";
   3: }.interface("Class1").interface("Class2");

The Composite could also be written:


   1: var Class3 = function () {
   2:     this.car = "blue";
   3: }.interface("Class1","Class2");
Instantiate Class3:


   1: var c = new Class3();
Must be overridden before used:


   1: c.add = c.add.override({ Kirk: "Time" });
If an attempt to use before overriding – Error is thrown. Below shows calling without first overriding resulting in an Error:


   1: c.add = c.add({ Kirk: "Time" }); //Error is thrown
A certain JavaScript design pattern, which assists organizing code.

This post is for the purpose of my notes only.

“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford

Sunday, January 6, 2013

Writing Windows Media Play List (.wpl)

I was working with a lot of media files (.mp4) located on my local drive. The media files were categorized within nth-deep sub folders. Watching the media, I was using Window Media Player, and I knew Windows Media Player can read a XML playlist, so, I wrote a small WPF application that created a Windows Play List for each folder. Interesting, while writing, I noticed one line of code can write a complete Windows Play List XML file. The line of code is below:

var mediaListFile = MakeFilePath("index.wpl", di.FullName);

XDocument xDoc = new XDocument(new XElement("smil", new XElement("head", new XElement("title", di.Name)), new XElement("body", new XElement("seq", files.Select(f => new XElement("media", new XAttribute("src", f)))))));

 

xDoc.Save(mediaListFile);


Using recursion, I created a collection of media files, which I wrote out to an XML file on my local drive. After creating the XML file, I was able to double-click on the file and Windows Media Player would begin. The XML file was a Windows Media Play List.

The code is below:


private TreeNode CreateDirectoryNode(DirectoryInfo di)

{

    var directoryNode = new TreeNode(di.Name);

 

    foreach (var directory in di.GetDirectories())

    {

        directoryNode.Nodes.Add(CreateDirectoryNode(directory));

    }

 

    var files = GetFiles(di);

    foreach (var file in files)

    {

        directoryNode.Nodes.Add(new TreeNode(file));

    }

 

    if (files.Count() > 0)

    {

        var mediaListFile = MakeFilePath("index.wpl", di.FullName);

        XDocument xDoc = new XDocument(new XElement("smil", new XElement("head", new XElement("title", di.Name)), new XElement("body", new XElement("seq", files.Select(f => new XElement("media", new XAttribute("src", f)))))));

        xDoc.Save(mediaListFile);

    }

    return directoryNode;

}

 

private bool FilterFiles(string file)

{

    Regex FilterFile = new Regex(@"^[\w\-. ]+(\.mp4|\.wmv|\.mov|\.avi|\.mp3)$");

    return FilterFile.IsMatch(file);

}

 

private string StripFile(string oldFile, string path)

{

    Regex SpecialChars = new Regex(@"[^a-zA-Z0-9._ ]+");

    if (SpecialChars.IsMatch(oldFile))

    {

        var newFile = AddFilePrefix(SpecialChars.Replace(oldFile, "_"));

        var newFilePath = MakeFilePath(newFile, path);

 

        if (!File.Exists(newFilePath))

        {

            var oldFilePath = MakeFilePath(oldFile, path);

 

            File.Copy(oldFilePath, newFilePath);

        }

        oldFile = newFile;

    }

 

    return oldFile;

}

 

private List<string> GetFiles(DirectoryInfo di)

{

    List<string> files = new List<string>();

    string stripedFile;

    string path = di.FullName;

    foreach (var file in di.GetFiles())

    {

        if (FilterFiles(file.Name))

        {

            stripedFile = StripFile(file.Name, path);

            files.Add(stripedFile);

        }

    }

 

    return files;

}

This post is for the purpose of my notes only.


“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford