Ember.js:如何在父路由的控制器和子路由之间传送数据? [英] Ember.js: How to communicate data between a parent route's controller and a child route?

查看:94
本文介绍了Ember.js:如何在父路由的控制器和子路由之间传送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,其中使用HTML5地理位置找到用户的位置。一旦他们的位置被检索,他们的位置被绘制在地图上(父路由)。然后,他们可以选择查看在该位置(子路径)上张贴的推文列表。

I'm building an app where the user's location is found using HTML5 geolocation. Once their location has been retrieved their location is plotted on a map (the parent route). They can then choose to view a list of tweets that were posted around that location (the child route).

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

So, I need to pass the coordinates I retrieved at the parent route (which are stored on the controller) to the child route so I can query the Twitter API properly. However, I am unsure how to do this or even if I've chosen the best approach.

这是索引控制器:

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控制器需要索引控制器:

The WhatDoYouWantToKnow controller "needs" the index controller:

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

此路由有一个模板,链接到/ what-do-you-want-to-知道/推送路线:

This route has a template which links to the '/what-do-you-want-to-know/tweets' route:

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

然后我需要将这些协调传递到子路由上:

I then need to pass these coords onto the child route:

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

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

I'm using Ember Data's Basic Adapter.

推荐答案

在路由中很容易做到。在路由中,您可以访问所有控制器。只需使用方法 controllerFor ,如下所示:

It is easy to do that in a route. In route you have access to all controllers. Just use method controllerFor like this:

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

这篇关于Ember.js:如何在父路由的控制器和子路由之间传送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆