Guest User

how to create template for json

a guest
Jan 20th, 2015
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.             var Element = (function (tag) {
  2.                 this.tag = tag;
  3.                 this.object = "";
  4.                 this.clas = "";
  5.                 this.style = "";
  6.                 this.id = "";
  7.                 this.content = "";
  8.                 this.href = "";
  9.  
  10.                 this.create = function () {
  11.                     this.object = document.createElement(this.tag);
  12.                     return this;
  13.                 };
  14.  
  15.                 this.setContent = function (content) {
  16.                     this.content = content;
  17.                     this.object.innerHTML = this.content;
  18.                     return this;
  19.                 };
  20.  
  21.                 this.setId = function (id) {
  22.                     this.id = id;
  23.                     this.object.setAttribute("id", id);
  24.                     return this;
  25.                 };
  26.  
  27.                 this.setHref = function (href) {
  28.                     this.href = href;
  29.                     this.object.setAttribute("href", href);
  30.                     return this;
  31.                 };
  32.  
  33.                 this.setClass = function (clas) {
  34.                     this.clas = clas;
  35.                     this.object.setAttribute("class", clas);
  36.                     return this;
  37.                 };
  38.  
  39.                 this.setStyle = function (style) {
  40.                     this.style = style;
  41.                     this.object.setAttribute("style", style);
  42.                     return this;
  43.                 };
  44.  
  45.                 this.appendTo = function (parent) {
  46.                     parent.appendChild(this.object);
  47.                     return this;
  48.                 };
  49.  
  50.                 this.o = function () {
  51.                     return this.object;
  52.                 };
  53.  
  54.                 this.create();
  55.  
  56.                 return this;
  57.  
  58.             });
  59.            
  60.             var container = document.getElementById("divs");
  61.    
  62.     // start loop here for json data
  63.             var div1 = new Element("div").setClass("span4").setStyle("margin-left: 0.8em;").appendTo(container);
  64.             var div1h2 = new Element("h2").setContent(data["title"]).appendTo(div1.o());
  65.             var div1p = new Element("p").setContent(data["content"]).appendTo(div1.o());
  66.             var div1p2 = new Element("p").appendTo(div1.o());
  67.             var div1p2a = new Element("a").setContent('Read more »').setHref('somurl').setClass("btn").appendTo(div1p2.o());
Advertisement
Add Comment
Please, Sign In to add comment