0

我在这里做错了什么,但不知道是什么。一般来说,我对 Ember 和 Javascript 都是新手,所以请随时指出任何错误。我会很感激多一双眼睛。

我基本上有一个包含多个数据集的谷歌地图。在与视图一起使用的控制器中,我获取数据集并为每个数据集创建一个 dataSetController(ArrayController)。然后我让 dataSetController 加载数据并将其添加到它的内容和一个附加的标记数组中。但是,当该过程完成时,两个 dataSetController 都包含所有点,而不仅仅是特定数据集的点。

下面是与视图一起使用的控制器:

App.MapviewShowController = Ember.ObjectController.extend({
  dataSets: [],

  createDataSets: function() {
    'use strict';

    var self = this;

    // clean previous data
    this.get('dataSets').length = 0;

    $.ajax({
      url: '/active_data_sets.json',
      type: 'GET',
      data: {'project_id': this.get('id')},
      success: function(data) {
        data.active_data_sets.forEach(function(entry) {

          // create a new controller for this dataset
          var newds = App.AddressRecordController.create();
          self.get('dataSets').pushObject(newds);
        });
      },
      error: function() {
      }
    });
  }
});

和 dataSetController 本身:

App.AddressRecordController = Ember.ArrayController.extend({

  content: [],
  isActive: true,
  dataSetId: 0,
  markerColor: '',
  datasetName: '',
  map: null,
  map_nelat: null,
  map_nelng: null,
  map_swlat: null,
  map_swlng: null,
  markerIcon: null,
  markers: [],
  mapBinding: 'App.MapData.map',
  map_nelatBinding: 'App.MapData.ne_lat',
  map_nelngBinding: 'App.MapData.ne_lng',
  map_swlatBinding: 'App.MapData.sw_lat',
  map_swlngBinding: 'App.MapData.sw_lng',

  getAddresses: function(ne_lat, ne_lng, sw_lat, sw_lng) {
    "use strict";

    var self = this;

    $.ajax({
      url: '/address_records.json',
      type: 'GET',
      data: {'dataset_id': this.get('dataSetId'), 'ne_lat': ne_lat, 'ne_lng': ne_lng, 'sw_lat': sw_lat, 'sw_lng': sw_lng},
      success: function(data) {
        data.address_records.forEach(function(new_address) {
          if (!self.findProperty('id', new_address.id)) {
            // add to the content
            self.content.addObject(App.AddressRecord.create(new_address));

            // add the marker
            var marker = new google.maps.Marker({
              position: new google.maps.LatLng(new_address.lat, new_address.long),
              map: self.get('map'),
              animation: google.maps.Animation.DROP,
              title: 'marker',
              id: new_address.id
            });

            // add the marker for later reference
            self.markers.push(marker);
          }
        });
      },
      error: function() {
      }
    });
  },

  newBounds: function() {
    "use strict";

    this.getAddresses(this.map_nelat, this.map_nelng, this.map_swlat, this.map_swlng);
  }.observes('map_swlng'),

  clean: function() {
    'use strict';

    // clean the objects in arracycontroller
    this.forEach(function(el) {
      el.destroy();
    });

    // clean the markers
    this.markers.length = 0;
  },

  showMarkers: function() {
    'use strict';

    var self = this;

    if(this.get('isActive')) {
      this.markers.forEach(function(mkr) {
        mkr.setMap(self.get('map'));
      });
    } else {
      this.markers.forEach(function(mkr) {
        mkr.setMap(null);
      });
    }
  }.observes('isActive')
}); 

更新

进一步调试后,我发现多个 AddressRecordControllers 除了markers array. 为了规避这个问题,我现在将标记存储为内容并且效果很好。仍然不清楚为什么标记数组在不同的控制器上共享。

4

1 回答 1

0

我相信 create 方法更像是一个单例,因此它要么创建对象,要么返回指向对象的指针。所以你只是添加到同一个控制器。你可以试试。Ember 也有一个 Mixin 东西,我也不确定它是如何工作的。

var newds = App.AddressRecordController.extend();
于 2013-04-11T21:48:39.650 回答