Ember.js:如何在父路由和#x27;s控制器和子路由?
我正在构建一个应用程序,使用HTML5 geolocation可以找到用户的位置。一旦检索到它们的位置,它们的位置将绘制在地图(父路线)上。然后,他们可以选择查看在该位置(子路径)周围发布的tweet列表 因此,我需要将在父路由(存储在控制器上)检索到的坐标传递给子路由,以便正确查询twitterapi。然而,我不确定如何做到这一点,甚至我是否选择了最好的方法 这是索引控制器:Ember.js:如何在父路由和#x27;s控制器和子路由?,ember.js,ember-data,Ember.js,Ember Data,我正在构建一个应用程序,使用HTML5 geolocation可以找到用户的位置。一旦检索到它们的位置,它们的位置将绘制在地图(父路线)上。然后,他们可以选择查看在该位置(子路径)周围发布的tweet列表 因此,我需要将在父路由(存储在控制器上)检索到的坐标传递给子路由,以便正确查询twitterapi。然而,我不确定如何做到这一点,甚至我是否选择了最好的方法 这是索引控制器: App.IndexController = Ember.ObjectController.extend({ co
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');
}
});
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
});
}
});
WhatDoYouWantToKnow控制器“需要”索引控制器:
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');
}
});
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 know/tweets”路由的模板:
<p>Your location:</p>
<div id="map"></div>
<ul>
<li>{{#linkTo 'tweets'}}Tweets{{/linkTo}}</li>
</ul>
我正在使用Ember Data的基本适配器。在路由中很容易做到这一点。在路由中,您可以访问所有控制器。只需使用方法
控制器,如下所示:
App.TweetsRoute = Ember.Route.extend({
model: function () {
var coords = this.controllerFor('index').get('coords');
return App.Tweet.find({ coords: coords });
}
});
不推荐使用controllerFor,而应在中使用needs:'index'
tweetsController@Nikita我找不到你从哪里得到的信息。代码似乎并不表示控制器的方法已被弃用。最好现在在控制器代码中使用新的需要控制器名。@Nikita终于找到了:控制器的控制器的方法已被弃用,而不是路由的控制器的甚至“需要:”方法已被弃用,你现在就使用注入。e、 g userController:Ember.inject.controller('user'),