0

I'm filling a sitemap array as the user travels through my app.

However my logic to fill the sitemap is flawed. Here is what I'm doing:

var siteMap = [],
    j = siteMap.length;

$(document).on( "pagebeforechange", function( e, data ) {

    if (j == 0 ){
        // add cause sitemap has no elements
        siteMap.push( { type: "external", data: data });
        } else {
            // loop through the sitemap object
            for ( var i = 0; i < j; i++) {
            // check if element is already stored
            if ( data.toPage == self.options.siteMap[i].data.toPage ){
                break;
                }   
            self.options.siteMap.push( { type: "external", data: data } );
            }           
        }
    }); 

The problem is my break does not work as expected. I wanted to loop through the sitemap and compare the data.toPage (a string like /some/page.html) to what I have already stored. If I find a match the loop should end WITHOUT pushing a new element to the sitemap. However, right now I'm looping and if an element is not found at loop postion(1), the break does not fire and I'm adding an entry on every loop until an entry matches.

Question:
How can I fix the loop, so it only adds an entry if no match exists?

Thanks for help!

EDIT:
This is what I have now:

(function( $, window) {
   $.widget("mobile.multiview",$.mobile.widget, {   
      options: {
         siteMap = {};
         },
      eventBindings: function() {

        $(document).on( "pagebeforechange", function( e, data ) {

            var self = this;
            if (!self.options.siteMap[data.toPage]){
               self.options.siteMap[data.toPage] = { type: "external", data: data };
               }
            });
        }
 }) (jQuery,this);
3
  • What's with this self.options thing? You declare "siteMap" as a simple variable with var above your function; is there code you're leaving out? Commented May 26, 2012 at 13:32
  • yes. this is inside a plugin I'm writing using the jquery widget factory. I'm declaring self.options.sitemap = {} in the global plugin options. Shouldn't make a difference, should it? Commented May 26, 2012 at 13:33
  • oh. Could be the "M" vs "m" in siteMap. 1 sec... It was! Commented May 26, 2012 at 13:42

1 Answer 1

1

You're loop performs the test on each iteration. Until it finds the thing it's looking for, it adds the new entry on every iteration.

What you need to do instead is see whether the element is in the array already, and then add it if not. Better yet, dump the idea of using an array in the first place and just use the "toPage" value as an object property.

var sitemap = {};

$(document).on( "pagebeforechange", function( e, data ) {
  if (!sitemap[ data.toPage ])
    sitemap[data.toPage] = { type: "external", data: data };
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But something is breaking the code. I'm pasting my new version above.
Works like a charm and saves a bunch of code and looping! Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.