sashomaga

Class creation pattern

Jun 1st, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Class = (function () {
  2.     function createClass(properties) {
  3.         var f = function () {
  4.             this.init.apply(this, arguments);
  5.         }
  6.         for (var prop in properties) {
  7.             f.prototype[prop] = properties[prop];
  8.         }
  9.         if (!f.prototype.init) {
  10.             f.prototype.init = function () { }
  11.         }
  12.         return f;
  13.     }
  14.  
  15.     Function.prototype.inherit = function (parent) {
  16.         var oldPrototype = this.prototype;
  17.         var prototype = new parent();
  18.         this.prototype = Object.create(prototype);
  19.         this.prototype._super = prototype;
  20.         for (var prop in oldPrototype) {
  21.             this.prototype[prop] = oldPrototype[prop];
  22.         }
  23.     }
  24.  
  25.     return {
  26.         create: createClass,
  27.     };
  28. }());
  29.  
  30. var FSB = (function () {
  31.     var data = [];
  32.     function add(folder) {
  33.         console.log(folder.urls);
  34.         data.push(folder.urls);
  35.     }
  36.     function render() {
  37.         var node = document.getElementById("cont");
  38.         var documentFragmen = document.createDocumentFragment();
  39.         for (folder in data) {            
  40.             var ul = document.createElement("ul");
  41.             for (url in data[folder]) {              
  42.                 var li = document.createElement("li");
  43.                 var a = document.createElement("a");                
  44.                 a.setAttribute("href", data[folder][url].url);
  45.                 a.setAttribute("target", "_blank");
  46.                 a.innerHTML = data[folder][url].title;
  47.                 li.appendChild(a);
  48.                 ul.appendChild(li);
  49.             }
  50.             documentFragmen.appendChild(ul);
  51.         }
  52.         node.appendChild(documentFragmen);
  53.     }
  54.     return {
  55.         add: add,
  56.         render: render
  57.     };
  58. }());
  59.  
  60. var Url = Class.create({
  61.             init: function (title, url) {
  62.                 this.title = title;
  63.                 this.url = url;
  64.             }
  65.         });
  66.  
  67.         var Folder = Class.create({
  68.             init: function (title, urls) {
  69.                 this.title = title;
  70.                 this.urls = urls;
  71.             }
  72.         });
  73.  
  74.         var url1 = new Url("yahoo", "http://www.yahoo.com");
  75.         var url2 = new Url("dir-bg", "http://www.dir.bg/");
  76.         var urlArray = [url1, url2];
  77.         var links1 = new Folder("links", urlArray);
  78.  
  79.         var url3 = new Url("abv", "http://www.abv.bg");
  80.         var url4 = new Url("hotmail", "http://www.hotmail.com/");
  81.         var urlArray2 = [url3, url4];
  82.         var links2 = new Folder("links", urlArray2);
  83.  
  84.         //console.log(links);
  85.         FSB.add(links1);
  86.         FSB.add(links2);
  87.         FSB.render();
Advertisement
Add Comment
Please, Sign In to add comment