OOP In WinJS #2: Nested Namespaces

In our previous article, we talked about how to define and use Namespaces in WinJS with a Universal Windows Apps demo. And now, it's time to dig deeper in WinJS Namespaces, which inclines me towards Nested Namespaces.

Nested Namespaces 

This article covers more insights as we're going deeper in namespaces structure by providing Nested Namespaces. Nested Namespaces, as you can understand, is a child namespace in a parent namespace.


Some examples  regarding Nested Namespaces can be:

  • Product/Categories/SubCategories
  • League/Teams/Players
  • Program/Product/Project
  • Company/Departments/Employees
  • University/Faculties/Classes/Students
And many more.

In our example we'll extend a namespace with a subnamespace which we call Nested Namespaces.

Let's start building!

Building the App

It's better if we use the same app we've created on our previous article as I linked above.

Since we need to extend the functionality of an existing namespace, I'm planning on taking "ShareJob" namespace.

What can be a child of ShareJob? I thought about a few, for instance:
  • Facebook, Twitter, LinkedIn, Google+
  • Share via Email
  • Share via SMS
So, let's create these sub jobs:
  1. WinJS.Namespace.defineWithParent(ShareJob, "Facebook", {  
  2.    Job: {  
  3.             PostData: function ()  
  4.             {  
  5.                 return "Sharing with Facebook...";  
  6.             }  
  7.         }  
  8.     }  
  9. );  
  10.   
  11. WinJS.Namespace.defineWithParent(ShareJob, "Twitter", {  
  12.     Job: {  
  13.         PostData: function () {  
  14.             return "Sharing with Twitter...";  
  15.         }  
  16.     }  
  17. }  
  18. );  
  19.   
  20. WinJS.Namespace.defineWithParent(ShareJob, "LinkedIn", {  
  21.     Job: {  
  22.         PostData: function () {  
  23.             return "Sharing with LinkedIn...";  
  24.         }  
  25.     }  
  26. }  
  27. );  
  28.   
  29. WinJS.Namespace.defineWithParent(ShareJob, "Email", {  
  30.     Job: {  
  31.         PostData: function () {  
  32.             return "Sharing with Email";  
  33.         }  
  34.     }  
  35. }  
  36. );  
  37.   
  38. WinJS.Namespace.defineWithParent(ShareJob, "SMS", {  
  39.     Job: {  
  40.         PostData: function () {  
  41.             return "Sharing with SMS...";  
  42.         }  
  43.     }  
  44. }  
  45. );  
Then let's call these functions in our js file:
  1. console.log(ShareJob.Twitter.Job.PostData());  
  2. console.log(ShareJob.Facebook.Job.PostData());  
  3. console.log(ShareJob.LinkedIn.Job.PostData());  
  4. console.log(ShareJob.Email.Job.PostData());  
  5. console.log(ShareJob.SMS.Job.PostData());  
And run,



That's it! We have implemented a namespace within an existing namespace and extended its functionality. This is an awesome feature of WinJS as you will be widely using it for separating jobs in your project.
 
Read more articles on Universal Windows Apps:


Similar Articles