jquery - How do I render a list in Jade with a distinct javascript function for each item? -
i'm using express , jade on top of node.js build web app. have express route pulls list of words database , sends list jade array object rendering. want render jade page on client side each word, when clicked on, send distinct message server.
my code far looks this...
./routes/show_words_to_user.js:
exports.render_page = function(req, res){ dbclient.query('select word words;', function(err, result){ res.render('pagetitle',{wordlist:result,userdata:userobj}); }); };
./views/words.jade:
ul each word in wordlist li= word
./routes/update_preference.js (expects receive message client):
exports.update_word = function(req, res){ dbclient.query('update words set userliked = 1 word=($1) , userid=($2)', [clickedword,userid],function(err, result){ res.send('your word updated.'); }; };
i need each word rendered in jade list act hyperlink send message server containing 1) name of word clicked on , 2) user id server passed jade.
my first thought render each list item hyperlink , use jquery bind response function each item. however, when used jquery on client side, lost ability specify distinct function each item in list.
my second thought inline javascript , set onclick= function can pass text of list item to, so:
ul each word in wordlist a(href='#',onclick='js_that_sends_word_to_update_preference(\'' + word + '\')')= word
this had undesirable behavior well, page jumps top every time click on link. i'd thought stop this, not able call "e.preventdefault" function hyperlink because passing string, , not event, onclick function.
i feel i'm going in wrong way. there's clean method rendering iterative functions in jade, it's escaping me.
so problem isn't node or jade, html and/or javascript want generate?
a simple way normal link, orignally considered:
each word in wordlist a(href='/update_preference?clicked_word=' + encodeuricomponent(word), class='contact_server')= word
(i hope jade syntax ok, have little expierence it)
Comments
Post a Comment