2013-04-20 95 views
5

我正在构建一个应用程序,使用HTML5地理位置找到用户的位置。一旦他们的位置被检索到,他们的位置就被绘制在地图上(父路线)。然后,他们可以选择查看在该位置周围发布的推文列表(子路由)。Ember.js:如何在父路由的控制器和子路由之间传递数据?

因此,我需要将我在父路由(存储在控制器上)检索到的坐标传递给子路由,以便我可以正确查询Twitter API。但是,我不确定如何做到这一点,或者即使我选择了最佳方法。

这是该指数控制器:

App.IndexController = Ember.ObjectController.extend({ 

    coords: false, 

    getGeoLocation: function() { 
    if (navigator.geolocation) { 
     this.set('retrieving', true); 
     navigator.geolocation.getCurrentPosition(
      this.locationRetrieved.bind(this), 
      this.locationFailed.bind(this), 
      { 
       timeout: 20000 
      } 
     ); 
    } else { 
     this.set('geolocation', false); 
    } 
    }, 

    locationRetrieved: function(position) { 
    this.set('coords', position.coords); 
    this.get('target').transitionTo('what-do-you-want-to-know'); 
    } 
}); 

的WhatDoYouWantToKnow控制器 “需要” 的指标控制器:

App.WhatDoYouWantToKnowController = Ember.ObjectController.extend({ 

    needs: 'index', 

    createMap: function() { 
    var coords = this.get('controllers.index').get('coords'); 
    var pyrmont = new google.maps.LatLng(coords.latitude, coords.longitude); 

    var map = new google.maps.Map(document.getElementById('map'), { 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: pyrmont, 
     zoom: 8 
    }); 

    var marker = new google.maps.Marker({ 
     map: map, 
     position: pyrmont 
    }); 
    } 
}); 

这条路线有哪些链接到“/模板什么-DO-你想知道/鸣叫的路线:

<p>Your location:</p> 
<div id="map"></div> 
<ul> 
    <li>{{#linkTo 'tweets'}}Tweets{{/linkTo}}</li> 
</ul> 

然后,我需要将这些坐标系统传递到孩子ro ute:

App.TweetsRoute = Ember.Route.extend({ 
    model: function() { 
    return App.Tweet.find({coords:}); 
    } 
}); 

我正在使用Ember Data的基本适配器。

回答

5

在路线中很容易做到这一点。在路线中,您可以访问所有控制器。只要使用方法controllerFor这样的:

App.TweetsRoute = Ember.Route.extend({ 
    model: function() { 
     var coords = this.controllerFor('index').get('coords'); 
     return App.Tweet.find({ coords: coords }); 
    } 
}); 
+0

controllerFor已被弃用,取而代之的是你应该使用'需求:“index''在tweetsController – xamenrax 2013-08-12 11:00:03

+0

@Nikita我无法找到你在哪里得到这个信息。代码似乎并不表示'controllerFor'方法已被弃用。 – louiscoquio 2013-11-07 11:50:06

+0

现在更好地在控制器代码中使用新的'需要controllerName'。 – xamenrax 2013-11-07 12:16:16

相关问题