Darlyn
Darlyn

Reputation: 4938

Appending content into template tag

I am trying to append content into template tag. I am using

getcontent("clients.json").then(function(x){
   obj = JSON.parse(x);
}).then(function(x){

   var x = createFullList(obj,docFragment);
   filtered = obj;
   return x;
}).then(function(x){
   template   .appendChild(x);
   var clone = document.importNode(template.content, true);
   alert(clone.children.length);
   aside.appendChild(clone)
});

getcontent is ajax call that returns response , the response is parsed and stored as object in obj variable ,

createFullList creates divs for every element in ajax response and append it into fragment , fragment is then appended into div that is also returned ,

function createFullList( obj , fragment ){
   var aside       = document.getElementsByClassName("one")[0];
   for( x in obj ){
      createInfoElement( fragment , obj[x].general , obj[x].job , x);
   }
   var div = document.createElement("div");
   div.appendChild(fragment);
   deleteKid( aside , 1 );
   return div;
}

what troubles me is appending into template , it does nothin , when i try to copy its content , it says it has 0 children , altought if i print children of div it prints correct number.

Why isnt content appended into template? Is this a right way or is there any specific way how to correctly append content into template?

Upvotes: 3

Views: 846

Answers (2)

Supersharp
Supersharp

Reputation: 31171

You should append the fragment in template.content instead of template:

template.content.appendChild( x )

Upvotes: 1

Sergio Moura
Sergio Moura

Reputation: 4942

What if you use just one then(), like so:

var obj;
getcontent("clients.json").then(function(x){
   obj = JSON.parse(x);
}).then(function(){
  var x = createFullList(obj, docFragment);
  filtered = obj;

  template.appendChild(x);
  var clone = document.importNode(template.content, true);
  alert(clone.children.length);
  aside.appendChild(clone)

  return(x);
});

I believe you're trying to reference a variable that is not available in the scope that template.appendChild() is referring it to.

Upvotes: 0

Related Questions