List Info

Thread: Re: OffTopic: Any JavaScript programmers here?




Re: OffTopic: Any JavaScript programmers here?
country flaguser name
United States
2008-07-05 02:45:00
Simple hash example:

var foo = { hat : 'Fedora', cat : 'Gattopardo' };
alert( 'The ' + foo.cat + ' in the ' + foo.hat );
// same as:
alert( 'The ' + foo['cat'] + ' in the ' + foo['hat'] );

Sorry, I don't have time to check your work right now, but
the stuff  
below does what I think you want, and is (hopefully) pretty 

straightforward. It's just an illustration and shouldn't be
taken as  
an example of JS best practices.  Also, I only checked it in
Firefox 3  
on Mac.

cheers

-- frosty

<html>
     <head>
         <title>JS example</title>
         <script type="text/javascript">
             FOO = {};           // minimalist namespace
declaration.
             FOO._origList = {}; // "hash of
lists" for the Perl  
hackers.
             FOO.sortList = function(listID) {

                 var theList =
document.getElementById(listID);
                 if (!theList) {
                     throw "no such list: " +
listID;
                 }
                 // let's assume it's all plain text.
                 var items =
theList.getElementsByTagName('li');
                 var textList = [];
                 for (var i=0; i < items.length; i++) {
                     textList[i] =
items[i].firstChild.nodeValue; //  
YMMV.
                 }
                 // cache the original the first time only
(deep copy).
                 if (!FOO._origList[listID]) {
                     FOO._origList[listID] = [];
                     for (var i in textList) {
                         FOO._origList[listID][i] =
textList[i];
                     }
                 }
                 // now set up the sorted list.
                 textList.sort();
                 FOO.setListFromArray(theList,textList);

             };
             FOO.unsortList = function(listID) {
                 var theList =
document.getElementById(listID);
                 if (!theList) {
                     throw "no such list: " +
listID;
                 }
                 // only bother if we were sorted
previously.
                 if (FOO._origList[listID]) {
                      
FOO.setListFromArray(theList,FOO._origList[listID]);
                 }
             };
             FOO.setListFromArray =
function(theList,textArray) {
                 // zero out the list
                 while (theList.childNodes.length) {
                    
theList.removeChild(theList.childNodes[0]);
                 }
                 // repopulate it with text
                 for (var i=0; i < textArray.length; i++)
{
                    var item =
document.createElement('li');
                    var textNode =  
document.createTextNode(textArray[i]);
                    item.appendChild(textNode);
                    theList.appendChild(item);
                 }
             }


             // but really, REALLY you want to use a toolkit
for this...

         </script>
     </head>
     <body
         <h1>JS example</h1>
         <ol id="sortTarget">
             <li>Uno</li>
             <li>Dos</li>
             <li>Tres</li>
         </ol>
         <p>
             <a
href="javascript:FOO.sortList('sortTarget');">s
ort  
alpha</a>
             <a
href="javascript:FOO.unsortList('sortTarget');">
;undo</a>
         </p>
         <hr />
         <ol id="another">
             <li>Eins</li>
             <li>Zwei</li>
             <li>Drei</li>
         </ol>
         <p>
             <a
href="javascript:FOO.sortList('another');">sort
alpha</ 
a>
             <a
href="javascript:FOO.unsortList('another');">un
do</a>
         </p>
     </body>
</html>

_______________________________________________
SanFrancisco-pm mailing list
SanFrancisco-pmpm.org
h
ttp://mail.pm.org/mailman/listinfo/sanfrancisco-pm

Re: OffTopic: Any JavaScript programmers here?
country flaguser name
United States
2008-07-05 02:47:24
Oops, that last bit of code had a broken <body> tag,
which might break  
sensitive browsers.  Sorry.  The attached is fixed.

-- f.



_______________________________________________
SanFrancisco-pm mailing list
SanFrancisco-pmpm.org
h
ttp://mail.pm.org/mailman/listinfo/sanfrancisco-pm

  
Re: OffTopic: Any JavaScript programmers here?
country flaguser name
United States
2008-07-05 09:45:41
I had a hard time understanding what Vicki was trying to do,
but seeing 
her code makes it clear - basically, the list is encoded in
HTML, not 
something that comes to you from the back end as a data
structure, in an 
Ajax (XHR) call for example.

So the innerHTML thing makes a lot of sense in this case (I
think).

But it really is the Hard Way to Do Things in Javascript. 
You have to 
back off and ask yourself, why do you need a sorted list in
the first 
place?  Aren't the list items coming to you from some
source, like a 
database query?  If there is only one list on the page,
what's the 
matter with sending a parameter to the back end indicating
how you want 
it to sort the data?  If there are more than one, why can't
you do an 
XHR call to bring in the data you need, where you need it? 
It really 
isn't that hard - the time you spent banging your head
against the wall 
on this problem would have been better spent learning how to
do that.

Of course if you're scraping someone else's HTML then you
don't have a 
lot of choice, and innerHTML could be your friend.  Then
again, I'm not 
sure why you're scraping HTML code in Javascript.  I can't
quite see the 
use case.

Also, of course JS has hashes.  They are called a variety of
things - 
'associative arrays', 'object literals', 'objects', etc.
depending on 
where you look.  In fact, Javascript objects are little more
than hashes 
(even moreso than in Perl).  You use objects (hashes) all
the time in JS 
(eg, the 'window' object is a hash).  If you're actually
scraping 
multiple lists, you can store all the lists in an object,
for example, 
keyed by the element ID.  Or whatever.

A very good JS reference is David Flanagan's 'Definitive
Guide', 5th 
edition.  Douglas Crockford just came out with a good book
too.

d

Vicki Brown wrote:
> At 20:31 -0700 07/04/2008, Kevin Frost wrote:
>   
>> Noooo! Don't poison the novices with innerHTML! 
That's like telling
>> people not to 'use strict' in Perl.
>>     
>
> Being a Perl programmer, I can totally understand your
trepidation.
> However... speaking as a frustrated JS noobie... the
innerHTML worked.
> (-:
>
> Less frightening variations would be gratefully
accepted.
>
> Also, if anyone has any hints about how to handle more
than one sortable
> list on a page, I'd be grateful.   The sort
function is handed an ID to
> the list to sort.
>
> In Perl I'd use a hash
>    origList[$id] = #save the list data;
>
> and then I'd just go looking for origList[$id] when I
was asked to revert
> theList[$id]
>
> Surprisingly, I don't see hashes (or 'associative
arrays') in JavaScript.
> Although something described as an 'associative array'
is present, it
> doesn't seem to be quite the same thing.
>
> Current code is here:
>    http://cfcl.com/twikipub/Learn/Tut/TWikiTutListS
ort/list_sort.js
>
> Example page
>    http://cfcl.com/twiki/bin/view/Learn/Tut/TWikiTutListSo
rt
>
> And many thanks for helping me in this educational
exercise... if any of
> you have a TWiki you're running and have questions
about how to do
> something there, just ask!!!
>   

_______________________________________________
SanFrancisco-pm mailing list
SanFrancisco-pmpm.org
h
ttp://mail.pm.org/mailman/listinfo/sanfrancisco-pm

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )