从 sproutcore 中的父视图获取数组

getting an array from a parentView in sproutcore

本文关键字:视图 获取 数组 sproutcore      更新时间:2024-02-25
App.MapView = App.ChartView.extend({
  data: undefined,
  dataLoaded: NO,
  markersArray: undefined,
  finishloadingdata: function (rawdata) {
    var latitude, longitude;
    var array = new Array();
    $(rawdata).find("Book").each(function () {
      $(this).find("Location");
      latitude = $(this).find("Latitude").text();
      longitude = $(this).find("Longitude").text();
      array.push(new Array(latitude, longitude));
    });
    this.set('markersArray', array);
    this.set('data', rawdata);
    this.set('dataLoaded', YES);
  },
  optionsView: SC.View.extend({
    layout: {
      left: 0,
      top: 0,
      right: 0,
      bottom: 50
    },
    map: null,
    center: new google.maps.LatLng(46.48, 7.9),
    zoom: 14,
    array: this.get('markersArray'),
    didCreateMap: null,
    render: function (ctx, first) {
      if (first) {
        ctx.push('<div style="position:absolute;top:0;left:0;right:0;bottom:0;"></div>');
      }
    },
    didCreateLayer: function () {
      this.invokeLast('_createMap');
    },
    _createMap: function () {
      if (this._map) {
        google.maps.event.trigger(this._map, 'resize');
      } else {
        var center = this.get('center');
        var opts = {
          zoom: this.get('zoom'),
          center: center,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var div = this.$('div')[0];
        var map = new google.maps.Map(div, opts);
        this._map = map;
        this.set('map', map);
        //i want to make an array of markers
        if (this.didCreateMap) this.didCreateMap(map);
        var marker = new google.maps.Marker({
          position: new google.maps.LatLng(46.48, 7.9),
          map: map
        });
      }
    }

MapView是一个扩展ChartView的视图,我正在覆盖选项View以使其显示谷歌地图。 它工作得很好,我只想用从XML文件中检索到的坐标在地图上放置标记。我得到阵列很好。我的问题是,当我想给选项查看该数组的值时,它给了我错误:对象 [对象窗口] 没有方法"get"。

我怎样才能给选项的数组属性查看标记数组的值,以便我可以使用它来将标记放到我的地图上。

非常感谢!!

而不是

array: this.get('markersArray')

你可能想做

arrayBinding: 'parentView.markersArray'

这使用 SproutCore 的绑定功能,并将确保 array 属性始终指向父视图的 markersArray 属性。但是,您可能还需要通过将以下内容添加到图表视图的顶部来指定optionsView是父项的子项:

App.MapView = App.ChartView.extend({
  childViews: ['optionsView'],
  ...

希望这有帮助!如果您对此问题有任何其他问题,请添加评论,我会尽力提供帮助。