我正在爲世界時間的應用程序工作。我有一個餘燼模型:Clock
我想更新每秒鐘的time
屬性。Ember.js:如何從視圖中調用模型中定義的方法?
現在,我已經在時鐘中定義了一個start
方法,它負責更新時鐘模型的時間屬性。
我對此有兩個問題: 1.如何從我的視圖中調用start
模型的方法 2.有沒有更好的方法可以使用?
這裏是我的模型,視圖,控制器,路由器和模板(櫃面你有興趣):
型號:
App.Clock = DS.Model.extend({
city: DS.attr('string'),
country: DS.attr('string'),
latitude: DS.attr('string'),
longitude: DS.attr('string'),
time: DS.attr('date'),
order: DS.attr('number'),
start: function() {
var clock = this;
var updateTime = function() {
var timezoneService = Utilities.TimezoneService;
timezoneService.getTimezone(clock.get('latitude'), clock.get('longitude'), function(timezone) {
clock.set('time', timezoneService.getDateTime(timezone.offset));
});
};
updateTime();
window.setInterval(function() {
updateTime();
}, 1000);
}
});
瀏覽:
App.ClocksView = Ember.View.extend({
templateName : 'world_clock/clocks'
});
App.ClocksNewView = Ember.View.extend({
templateName : 'world_clock/new'
});
控制器:
個App.ClocksController = Ember.ArrayController.extend({
sortProperties: ['order']
});
App.ClocksNewController = Ember.ObjectController.extend({
city: null,
save: function() {
var cityName = this.get('city');
if(!cityName.trim()){
return;
}
var locationService = Utilities.LocationService.getInstance();
locationService.lookupLocation(cityName, function(location) {
var city = location.city;
var country = location.country;
var lat = location.latitude;
var lon = location.longitude;
if(city && country && lat && lon) {
var clks = App.Clock.find({ city: city });
clks.on('didLoad', function() {
if(clks.get('length') === 0) {
var clock = App.Clock.createRecord({
city: location.city,
country: location.country,
latitude: location.latitude,
longitude: location.longitude,
order: 10
});
clock.save();
}
});
}
});
this.set('city', '');
this.get('target').transitionTo('clocks');
}
});
路由器:
App.Router.map(function() {
this.resource('clocks', { path: '/' }, function() {
this.route('new');
});
});
App.ClocksRoute = Ember.Route.extend({
model: function() {
var locationService = Utilities.LocationService.getInstance();
locationService.getLocation(function(location) {
App.Clock.createRecord({
city: location.city,
country: location.country,
latitude: location.latitude,
longitude: location.longitude,
order: -10
});
});
return App.Clock.find();
}
});
clocks.handlebars:
{{outlet}}
{{#linkTo "clocks.new"}}Add Clock{{/linkTo}}
<ul>
{{#each controller}}
<li>{{city}}, {{country}} - {{time}}</li>
{{else}}
You have not defined any clock yet.
{{/each}}
</ul>
new.handlebars:
<form {{action "save" on="submit"}}>
{{view Ember.TextField valueBinding="city" placeholder="City"}}
<button>Save</button>
</form>
非常感謝非常詳細的答覆:)我唯一的問題是,既然'TimeZoneService.getTimezone'是異步回調,也不會偏移值將始終保持爲空? – Moon
是的,這是一個很好的觀點。猜測時鐘應該有'positionDidChange'這樣的觀察者,每當經緯度發生變化時就會觸發。該觀察者會調用時區服務並設置偏移屬性。 –
有道理。但是現在我還有幾個問題。 [1]我在'Rails Mountable Engine'中構建這個應用程序。出於這個原因,'{{world-clock city =「clock.city」...'工作不正常。我怎樣才能定義組件的模板路徑? [2]'{{#每個時鐘在控制器}}'不工作,但'{{#每個控制器}}'。你可以向我解釋這件事嗎? [3]你能爲我提供控制器的示例代碼嗎? – Moon