Forum: Rails Spinoffs (closed, excessive spam) Template usage

Posted by Namotco (Guest)
on 2008-06-26 00:22
(Received via mailing list)
I'm confused about how to use Template:

$('curMedList').innerHTML=[conversion1, conversion2,
conversion3].each( function(conv){templ.evaluate(conv);});

this outputs "[object,Object],[object,Object],[object,Object]"

What am I doing wrong?
Posted by Frederick Polgardy (Guest)
on 2008-06-26 01:36
(Received via mailing list)
You're assigning the return value of each() to innerHTML, which is the 
array
you're iterating over.  What are you trying to do?

-Fred

On Wed, Jun 25, 2008 at 5:22 PM, Namotco <namotco@gmail.com> wrote:

>
> I'm confused about how to use Template:
>
> $('curMedList').innerHTML=[conversion1, conversion2,
> conversion3].each( function(conv){templ.evaluate(conv);});
>
> this outputs "[object,Object],[object,Object],[object,Object]"
>
> What am I doing wrong?


--
Science answers questions; philosophy questions answers.
Posted by Namotco (Guest)
on 2008-06-26 02:43
(Received via mailing list)
Put the resulting HTML inside the element $('curMedList').  What
should I change?
Posted by Namotco (Guest)
on 2008-06-26 04:04
(Received via mailing list)
I changed my code to add the HTML inside of the function.  Now my
problem is the first time it's always undefined and I get that in my
output:
"undefined"
<li>list item 1</li>
<li>list item 2</li>
Posted by kangax (Guest)
on 2008-06-26 04:16
(Received via mailing list)
What exactly are you trying to do?

- kangax
Posted by Namotco (Guest)
on 2008-06-26 04:39
(Received via mailing list)
given a JSON object and this:

<div id="curMedContainer">
  <ul id="curMedList"></ul>
  <br style="clear:both;" />
  <div id="MedHistoryDisplay"></div>
</div>


I'm trying to use Template to fill in the list:

var cmHeader = new Template('<li class="#{active} #{RxNumber}"><a
href="\#Rx#{RxNumber}" id="cm_#{RxNumber}"
onclick="activateMedList(\'#{RxNumber}\');">#{ShortDate}</a></li>');
    var mlhtml;
     My_Array_Of_Objects.each( function(conv){mlhtml
+=cmHeader.evaluate(conv);});
     $('curMedList').innerHTML=mlhtml;
Posted by Namotco (Guest)
on 2008-06-26 04:41
(Received via mailing list)
I fixed it be defining my variable first (var mlhtml; --> var
mlhtml=''; ).  Thanks for the help anyway.
Posted by Matt Foster (Guest)
on 2008-06-26 20:12
(Received via mailing list)
Template#evaluate method returns a string, you can assign this string
to an innerHTML object.  Remember, "each" doesn't return anything so
assigning a value to the execution doesn't really work the way you're
anticipating it to in your first example.  You could however use the
"collect" method and get some decent results like this.

$("curMedList").innerHTML = [obj1, obj2, obj3].collect(function(obj)
{ return myTemplate.evaluate(obj)});

--
Matt Foster
Ajax Engineer
Nth Penguin, LLC
http://www.nthpenguin.com
Posted by Frederick Polgardy (Guest)
on 2008-06-26 21:10
(Received via mailing list)
Enumerable#collect() still returns an array though.  If the template is
HTML, you could create a list with something like:

$("curMedList").innerHTML = [obj1, obj2, obj3].inject('<ul>', 
function(s,
obj) {
  return s + '\n<li>' + myTemplate.evaluate(obj) + '</li>';
}) + '\n</ul>';

You get the idea.

-Fred

On Thu, Jun 26, 2008 at 1:01 PM, Matt Foster <mattfoster01@gmail.com> 
wrote:

>
> Template#evaluate method returns a string, you can assign this string
> to an innerHTML object.  Remember, "each" doesn't return anything so
> assigning a value to the execution doesn't really work the way you're
> anticipating it to in your first example.  You could however use the
> "collect" method and get some decent results like this.
>
> $("curMedList").innerHTML = [obj1, obj2, obj3].collect(function(obj)
> { return myTemplate.evaluate(obj)});


--
Science answers questions; philosophy questions answers.
This topic is locked and can not be replied to.