Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google maps store locator modify hardcoded initialization to dynamic

I'm trying to modify this example http://storelocator.googlecode.com/git/examples/panel.html

the javascript code is here: https://gist.github.com/2725336

the aspect I'm having difficulties with is changing this:

MedicareDataSource.prototype.FEATURES_ = new storeLocator.FeatureSet(
  new storeLocator.Feature('Wheelchair-YES', 'Wheelchair access'),
  new storeLocator.Feature('Audio-YES', 'Audio')
);

to create the FeatureSet from a function, so for example I have this function which parses a JSON object

WPmmDataSource.prototype.setFeatures_ = function(json) {
    var features = [];

    // convert features JSON to js object
    var rows = jQuery.parseJSON(json);

    // iterate through features collection
    jQuery.each(rows, function(i, row){

    var feature = new storeLocator.Feature(row.slug + '-YES', row.name)

    features.push(feature);
    });

    return  new storeLocator.FeatureSet(features);
    };

so then change the first code snippet to something like

WPmmDataSource.prototype.FEATURES_ = this.setFeatures_(wpmm_features);

which returns an error:

Uncaught TypeError: Object [object Window] has no method 'setFeatures_'
like image 348
paul Avatar asked Nov 13 '22 05:11

paul


1 Answers

I think you just have to make some changes to the WPmmDataSource.prototype and your setFeatures_ method:

WPmmDataSource.prototype = {
    FEATURES_ : null,        
    setFeatures_ : function( json ) {
        //Set up an empty FEATURES_ FeatureSet
        this.FEATURES_ = new storeLocator.FeatureSet();
        //avoid the use of "this" within the jQuery loop by creating a local var
        var featureSet = this.FEATURES_;
        // convert features JSON to js object
        var rows = jQuery.parseJSON( json );
        // iterate through features collection
        jQuery.each( rows, function( i, row ) {
            featureSet.add(
                new storeLocator.Feature( row.slug + '-YES', row.name ) );
        });
    }
}

And with this, you don't have to do the assignment by returning a value from setFeatures_; it has direct access to the FEATURES_ member. So the line:

WPmmDataSource.prototype.FEATURES_ = this.setFeatures_(wpmm_features);

is no longer necessary. This also means that later, when you have created an instance of WPmmDataSource, your code can work like this:

var wpmd = new WPmmDataSource( /* whatever options, etc. you want */ );
wpmd.setFeatures_( json );
// Thereafter, wpmd will have its FEATURES_ set

I'm not exactly sure what you are trying to achieve, but I believe this will get you over the hurdle of your current stall. I hope this gets you moving forward -

like image 90
Sean Mickey Avatar answered Nov 16 '22 03:11

Sean Mickey