Ste*_*ung 5 ember.js ember-data
我正在构建一个应用程序,其中使用HTML5地理位置找到用户的位置.一旦检索到它们的位置,它们的位置就会绘制在地图上(父路线).然后,他们可以选择查看在该位置(子路线)周围发布的推文列表.
所以,我需要将在父路由检索到的坐标(存储在控制器上)传递给子路由,这样我就可以正确地查询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');
}
});
Run Code Online (Sandbox Code Playgroud)
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
});
}
});
Run Code Online (Sandbox Code Playgroud)
这条路线有一个模板,链接到'/ what-you-to-to-know/tweets'路线:
<p>Your location:</p>
<div id="map"></div>
<ul>
<li>{{#linkTo 'tweets'}}Tweets{{/linkTo}}</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
然后我需要将这些坐标传递到子路线上:
App.TweetsRoute = Ember.Route.extend({
model: function() {
return App.Tweet.find({coords:});
}
});
Run Code Online (Sandbox Code Playgroud)
我正在使用Ember Data的基本适配器.
小智 5
在路线上很容易做到这一点.在路线中,您可以访问所有控制器.只需使用这样的方法controllerFor:
App.TweetsRoute = Ember.Route.extend({
model: function () {
var coords = this.controllerFor('index').get('coords');
return App.Tweet.find({ coords: coords });
}
});
Run Code Online (Sandbox Code Playgroud)