F
F
Finom2011-12-03 15:55:00
JavaScript
Finom, 2011-12-03 15:55:00

How do I get the index of an array being processed in jQuery.tmpl and why can't I retrieve an object property?

Good afternoon.
I'm trying to get an index in an array processed by the template. First of all, I tried to create a variable before the template by implementing the index method:

var i=1;<br>
$('#trackTmpl').tmpl(data, {<br>
  index: function() {<br>
    return i++;<br>
  }<br>
});<br>

But each iteration of the template engine also includes three invisible iterations (i is incremented four times), it turns out:
5, 9, 13, ...
Then I tried to extract the key that is in the template engine class (at least it is displayed in the console):
$('#trackTmpl').tmpl(data, {<br>
  index: function() {<br>
    console.log(this);<br>

m_20111202-pb2j-41kb.jpg

But here's the problem:
console.log(this.key); //undefined<br>
    return this.key; //undefined<br>
  }<br>
});<br>


Here the person has the same problem: stackoverflow.com/a/5158869/968167
But I can't understand the reason, if there is something in the object, then it should be returned.

In addition, I tried to call "key" in the template itself:
${$item.key}
Which also did not bring success, although it
${console.log($item)}
shows the same result as in the screenshot above.

I have two questions: how not to get the index asshole ( jQ.tmpl question) and why does this behave in such a strange way (general JS question)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anatoly, 2011-12-03
@taliban

There is no chrome under the other, but it seems to me that they have the same principle with ff:


var obj = {}
console.log(obj)
obj.q = 2;
console.log(obj)

Will output in the console:
object {}
Object {q=2}

But if you look at the properties of the first object, there will be a property q with a value of 2 (although it is not visible). Because it outputs to the console not a dump of the object, but a link to it, and everything that is added later in the console will also appear, perhaps this is why there is a key in the console and you don’t (yet) in the code.

S
sdevalex, 2011-12-03
@sdevalex

And what did the native syntax not please (pass an array to the template)?
{{each(key, item) items}}{{/each}}

D
dmitryklerik, 2011-12-03
@dmitryklerik

Very similar to the problem of closures - javascript.ru/basic/closure

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question