2012-05-18 125 views
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_' 
+0

我在這裏放一個這樣的演示:http://demo.wpconsult.net/你可以看到在控制檯 – paul

回答

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 

我不太確定你想達到什麼樣的,但我相信這將讓你過你目前失速的障礙。我希望這讓你前進 -

+0

謝謝你的錯誤消息,但FEATURES_需要可以訪問類方法,正如您看到的,它正用於將功能分配給parse_函數中的商店。 如果我使用你的代碼,features.add(this.FEATURES_.getById('Wheelchair-'+ row.Wheelchair)); 函數在getById上引發錯誤,因爲FEATURES_未定義。 有什麼建議嗎? – paul

+0

當然。樂意效勞。我已經對上面的代碼進行了調整,以正確地創建'FEATURES_'並確保它可用於'WPmmDataSource' JavaScript類中的所有方法。 –

+0

感謝肖恩,我終於在你的幫助下找到了它。欣賞努力。我只需將FEATURES_保存到parse_函數中的一個變量即可使其最終工作。 – paul