function used in loop

2 views
Skip to first unread message

adv...@gmail.com

unread,
Nov 27, 2015, 9:27:55 AM11/27/15
to JavaScript Templates Engine PURE
Hello everyone,
I want to use a renderer function (to format dates) inside a loop.
the directive looks like this:
       'li': {
               
'exp<-': {
                   
'@id': 'exp.exp_id',
                   
'span': function(input) {

                        console
.dir(input);
                   
}

               
}
           
}

and the output is:
Object
> context: Array[1]
> item: undefined
> items: undefined
> length: 1
> pos: undefined
> __proto__: Object

I thought the context should be one element in the array (my data is an array of objects)
but also, item, items and pos are all undefined.

what am I doing wrong?

Mic (BeeBole)

unread,
Nov 27, 2015, 9:41:59 AM11/27/15
to JavaScript Templates Engine PURE
If you put :  console.dir(this);
Inside the function what do you see?

If you can make a small gist like this one: https://gist.github.com/beebole/bb9e94d596f49fd44c41 with an example of your data and HTML
It can be quickly solved.

Alex Popescu

unread,
Nov 27, 2015, 10:24:32 AM11/27/15
to JavaScript Templates Engine PURE
Hi,
if I use "this" then I get the actual item but it's not set in the parameter, which means I have no "pos" available

Mic (BeeBole)

unread,
Nov 27, 2015, 10:36:19 AM11/27/15
to JavaScript Templates Engine PURE
When I try this

'.': '#{exp.exp_date}-#{exp.pos}',

or

'.': function(input) {
  return input.item.exp_date + '-' + input.pos;
}


I get the same expected result.

If you do not get it.
What browser are you using? Did you try with another one?

Alex Popescu

unread,
Nov 27, 2015, 11:07:28 AM11/27/15
to JavaScript Templates Engine PURE
this is really weird:
if I use input.item.exp_date it works but if I do console.dir(input) then this is what Chrome shows in the console:

Object
- context: Object
- item: undefined
- items: undefined
- length: 1
- pos: undefined
- __proto__: Object


and if I do console.dir(input.item) this is what I get

  1. Object
    1. exp_date: 655296777
    2. exp_id: 55780
    3. __proto__: Object

I can't explain this though

Mic (BeeBole)

unread,
Nov 27, 2015, 11:29:23 AM11/27/15
to JavaScript Templates Engine PURE
If you put a breakpoint just after console.dir,  the data in the console are ok at that moment.
The internal object get probably cleaned out after rendering.

If you put this:
console.log(JSON.stringify( input, null, 2) );
You get the data as they were at render time.
Reply all
Reply to author
Forward
0 new messages