Attribute disappearing in iteration

10 views
Skip to first unread message

Javier Calzado Sánchez

unread,
Jan 9, 2012, 8:59:42 AM1/9/12
to JavaScript Templates Engine PURE
Hi again,

I'm trying to access to an element of the array and the attribute that
i marked to substitute is disappearing,

the stage:

json data: from a cakephp application rendering the data like:
response(<?= json_encode( $contents) ?>);
a function named response is rendered with the data in
json format like:

response([{"Activity":
{"id":"44","type_of_content":"Activities","type":null,"category_id":null,"relative_id":null,"far_relative_id":null,"order":"0","permalink":"isidoro" ...

the html code:

<div id="template">
<span></span>
</div>

<script type="text/javascript" charset="utf-8">
function response(data){
var directive = {
'span':{
'it<-response':{
'+.':'it.Activity.id'
}
}
};
$('#template').render(data, directive);
}

$.getJSON("http://myapp/mymethod?jsoncallback=?");

</script>

The result: span is erased from the code for that reason pure is
telling that span attribute is not in the div template.

My theories: I'm not accessing well to the element in the array, the
function response is disturbing pure.js or is not a well method to
display json data

Someone have the same problem?

Thank you very much

Mic (BeeBole)

unread,
Jan 10, 2012, 10:44:52 AM1/10/12
to JavaScript Templates Engine PURE
You don't have a node response in your JSON.
eg: {response:[...]}, thus the loop removes the node (SPAN)

On Jan 9, 2:59 pm, Javier Calzado Sánchez <jcalzadosanc...@gmail.com>
wrote:
> Hi again,
>
> I'm trying to access to an element of the array and the attribute that
> i marked to substitute is disappearing,
>
> the stage:
>
> json data: from a cakephp application rendering the data like:
> response(<?= json_encode( $contents) ?>);
>                a function named response is rendered with the data in
> json format like:
>
> response([{"Activity":
> {"id":"44","type_of_content":"Activities","type":null,"category_id":null,"r elative_id":null,"far_relative_id":null,"order":"0","permalink":"isidoro" ...

Javier Calzado Sánchez

unread,
Jan 11, 2012, 9:59:59 AM1/11/12
to JavaScript Templates Engine PURE
wuops!

yes, i think that the problem is the json array data, i have a
question about this.

In other json arrays with this structure i have no problem:

var data = {
animals:[
{name:'dog', legs:4},
{name:'cat', legs:4},
{name:'bird', legs:2},
{name:'mouse', legs:4}
]
};

var directive = {
'li':{
'animal<-animals':{
'.':'animal.name'
}

but my json array have this structure:

[{"Activity":{"id":"1688","title":"mytitle"}], "Response":[]},
{"Activity":{"id":"1688","title":"mytitle"}], "Response":[]}, ...

i tried this:

var directive = {
'li':{
'it<-Activity':{
'.':'it.id'
}
with no positive result.

Salutti

Mic (BeeBole)

unread,
Jan 12, 2012, 1:59:13 AM1/12/12
to JavaScript Templates Engine PURE
If your json is an array, you can use 'it<-'

On Jan 11, 3:59 pm, Javier Calzado Sánchez <jcalzadosanc...@gmail.com>

Javier Calzado Sánchez

unread,
Jan 13, 2012, 7:07:10 AM1/13/12
to JavaScript Templates Engine PURE
Thank you for the contribution for json as array: 'it<-'

I reduced the array for testing as:

[{"Activity":{"id":"1663"}},{"Activity":{"id":"1659"}},{"Activity":
{"id":"1657"}},{"Activity":{"id":"1698"}},{"Activity":{"id":"1699"}}]

and pure code as:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/
jquery.min.js"></script>
<script src="http://github.com/pure/pure/raw/master/libs/pure.js"></
script>
</head>
<body>
<div class="template">
<li></li>
</div>

<script type="text/javascript" charset="utf-8">
var data = $.getJSON("http://my.site/mymethod?jsoncallback=?",
function(json){
var directive = {
'li':{
'it<-' :{
'+.' : 'it.Activity.id',
}
}
};

$('div.template').render(json, directive);
});
</script>

</body>
</html>

But i'm not accessing to the elements of the array.

Cheers

Mic (BeeBole)

unread,
Jan 14, 2012, 7:15:54 PM1/14/12
to JavaScript Templates Engine PURE
When something doesn't work, I use a function directive like the one
below:

var directive = {
'li':{
'it<-' :{
'.' :function(a){
//debugger; /*to stop and inspect*/
console.log(this); /* "this" is the current loop
item, check for: Activity.id */
console.log(a); /* gives you the JSON, the loop,
the current index,... */
},
}
}
};

It should drive you quickly to the cause of the problem.



On Jan 13, 1:07 pm, Javier Calzado Sánchez <jcalzadosanc...@gmail.com>
wrote:

Javier Calzado Sánchez

unread,
Jan 16, 2012, 6:00:06 AM1/16/12
to JavaScript Templates Engine PURE
Hi again,

i tested the console.log method but i can't see what is happening.

I tried to embed my data to test is the structure of the array is
correct, making little modifications i got a positive result:

var data =
{
proves: [{"Activity":{"id":"1663","title":"Andrea"}},{"Activity":
{"id":"1659","title":"Nines"}}]
};

The problem i think is in the getJSON petition, when i put the array
json data in a file like data.json pure is not working, the same
occours if your petition is to a server.

Cheers

Mic (BeeBole)

unread,
Jan 17, 2012, 8:40:16 AM1/17/12
to JavaScript Templates Engine PURE
Did you put a console.log or alert just before $('div.template')... ?
I'm not sure this is related to pure.js

On Jan 16, 12:00 pm, Javier Calzado Sánchez

Javier Calzado Sánchez

unread,
Jan 17, 2012, 9:27:50 AM1/17/12
to JavaScript Templates Engine PURE
Thank you very much for the answers,

finally i found the problem with the help of the cakephp developer.

We are solved the problem making a json array like this:

response([{"Activity":{"id":"1688"}}, {"Activity":{"id":"1688"}} ])

and in the html code putting this:

<script>

function response(data) {
var directive = {
'li':{
'it<-':{
'+.' : 'it.Activity.title',
}
}
};

$('div.template').render(data, directive);
}

$.getJSON("http://my.site/activities/get?jsoncallback=?")

</script>

Cheers and congratulations for pure
Great work!
Reply all
Reply to author
Forward
0 new messages