5

我正在尝试修改此示例 http://storelocator.googlecode.com/git/examples/panel.html

javascript代码在这里: https ://gist.github.com/2725336

我遇到困难的方面正在改变这一点:

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

从一个函数创建 FeatureSet,例如,我有一个解析 JSON 对象的函数

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);
    };

所以然后将第一个代码片段更改为类似

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

返回错误:

Uncaught TypeError: Object [object Window] has no method 'setFeatures_'
4

1 回答 1

1

我认为您只需要对WPmmDataSource.prototype您的setFeatures_方法进行一些更改:

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 ) );
        });
    }
}

有了这个,您不必通过从setFeatures_;返回一个值来完成分配。它可以直接访问该FEATURES_成员。所以这一行:

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

不再需要。这也意味着稍后,当您创建 的实例时WPmmDataSource,您的代码可以像这样工作:

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

我不确定您要达到什么目标,但我相信这将使您克服当前摊位的障碍。我希望这能让你继续前进 -

于 2012-05-21T00:28:43.170 回答