On Nov 17, 2005, at 1:38 PM, Brendan Crosser-McGay wrote:


I love MochiKit, between prototype, and some others, I think Mochi is
the most complete and pretty well documented library, that being said.
I'm having problems using iteration, I want to replace a current
function with the iteration functions, but I'm not sure how.

Thanks! What do you think needs to be enhanced in the documentation? Want to help write some? ;)

Here's the code I want to use for iteration:

function news_callback(e) {

  var news_regex =
/(\/news\/item\.tcl\?news_item_id=[0-9]+|\/poll\/one-poll\.tcl\? poll_id=[0-9]+)/g
  temp = new Array();
  news_obj = getElement('newspace').innerHTML
  news_array=news_obj.match(news_regex);
  // how can I use the iteration functions to replace this section?
  for (i=0;i<e.length;i++) {
    temp[i]=e[i].match(news_regex)[0];
  }
  test=compare(news_array,temp);
  // build_news_table simply does all the table work
  if (test!=0) {var
np=getElement('newspane');np.innerHTML=build_news_table(e);
}

function check_news() {
        var o = loadJSONDoc('ajax/news.tcl')
        o.addCallback(function(e) {news_callback(e)})
        o.addErrback(function(e) {alert('Unable to connect - Error:'+e)})
}

Here's how I'd translate that code, literally:

var NEWS_REGEX = /.../;
function news_match(s) {
        return s.match(NEWS_REGEX)[0];
};

function news_callback(e) {
        var news_obj = getElement('newspace').innerHTML;
        var news_array = news_obj.match(NEWS_REGEX);
        var test = map(news_match, e);
        if (compare(news_array, text) != 0) {
                ...
        }
};

I said literally because I don't think your code is correct... Unless you're being really really clever and doing strange things, that comparison should always be false. You might want to look up what the return value of String.prototype.match is and play with the MochiRegExp example.

http://developer.mozilla.org/en/docs/ Core_JavaScript_1.5_Reference:Objects:String:match
http://mochikit.com/examples/mochiregexp/index.html

Also note that "[]" is the same as "new Array()", just like "{}" is the same as "new Object()".. kinda silly to type all that when the language has syntax for it.

-bob

Reply via email to