當前日期(只是一天,而不是時間)發生變化時,是否有簡單的方法來觸發Backbone.js事件?當前日期發生變化時觸發Backbone.js事件
2
A
回答
2
我根本就沒有定期檢查一個更好的主意:
var lastCheckedDate = new Date();
setInterval(function() {
var d = new Date();
//TODO: check whether the day in d and lastCheckedDate are the same or not
//TODO: if not, trigger event
lastCheckedDate = d;
}, 10000); //this will check in every 10 seconds
2
您可以創建一個擴展骨幹事件這樣的構造:
var Clocks = function(){};
_.extend(Clocks.prototype, Backbone.Events, {
start: function() {
this.timer = setInterval(_.bind(this.tick, this), 1000);
return this;
},
tick: function() {
this.trigger('tick', new Date)
},
stop: function() {
this.timer && clearInterval(this.timer);
return this;
}
});
,然後用它在骨幹模型更新它屬性:
var ClocksModel = Backbone.Model.extend({
defaults: function() {
var date = new Date
return {
date: date.getDate(),
seconds: date.getSeconds()
}
},
initialize: function() {
this.clocks = new Clocks;
this.clocks.on('tick', this.updateDate, this)
},
updateDate: function (date) {
this.set('date', date.getDate())
this.set('seconds', date.getSeconds())
},
start: function() {
this.clocks.start();
return this;
},
stop: function() {
this.clock.stop();
return this;
}
});
然後你就可以:
clocks = new ClocksModel;
clocks
.on('change:seconds', function(model, newSeconds) {
console.log('seconds changed to ', newSeconds)
})
.on('change:date', function(model, newDate) {
console.log('date changed to', newDate)
})
.start()
4
所以,這個問題的答案 - 有沒有滾動自己的解決方案沒有簡單方式。
這就是說,實現並不算太壞。
這裏的想法是,你有一個'天'屬性的模型。在初始化模型時,我們將調用一個初始化器來確定持續到「明天午夜」的持續時間。在午夜之後,setTimeout將被調用,它將更新模型的'day'屬性。在視圖本身中,我們傾聽「天」屬性的更改。一旦該屬性更新,我們將重新初始化更改日期的方法。這樣就沒有輪詢,並且一旦日期發生變化,你的事件就會被觸發。
當然,有許多方法,你可以廣播的更新 - 即明確觸發,而不是set
荷蘭國際集團的財產的事件 - 但這裏是爲了什麼,我上述的代碼:
模式
var Model = Backbone.Model.extend({
defaults: function(){
var d = new Date();
return {
day: d.getDay()
}
},
initialize: function(){
_.bindAll(this, '_init_date_trigger');
this._init_date_trigger();
},
_init_date_trigger: function(){
var now = new Date();
var midnightTomorrow = new Date();
midnightTomorrow.setDate(now.getDate() + 1);
midnightTomorrow.setHours(0,0,0,0);
var msTillTomorrow = midnightTomorrow - now + 1;
var me = this;
console.log("date will be changed in " + msTillTomorrow);
setTimeout(function(){
me.set("day", new Date().getDay());
me._init_date_trigger();
}, msTillTomorrow);
}
})
查看
var View = Backbone.View.extend({
template:_.template("The day is: <span class='day'> <%= day %> </span>"),
initialize: function(){
this.model.on("change:day", this.render);
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
實施
var myModel = new Model();
var myView = new View({model: myModel});
$("#example").html(myView.render().el);
這裏的小提琴:http://jsfiddle.net/6KGzt/4/
+0
在我的答案中,使用'setTimeout'肯定比'setInterval'好。獲取我的+1。 – 2013-05-10 16:11:28
相關問題
- 1. 當關系發生變化時觸發休眠(事前/事後)更新事件
- 2. 當css值發生變化時觸發事件
- 3. 當頁面主題發生變化時觸發什麼事件
- 4. 當Ember.Select選項發生變化時觸發onchange事件
- 5. 當ListView的ItemsSource發生變化時觸發事件
- 6. 當MKMAPVIEW範圍發生變化時會觸發什麼事件
- 7. 當UITabBarController的'selectedViewController'發生變化時觸發一個事件?
- 8. 當ListBox.Item的大小發生變化時觸發事件
- 9. 當div中的內部html發生變化時觸發事件
- 10. 當某個類發生變化時觸發事件
- 11. 當底層發生變化時,OpenLayers 2觸發事件
- 12. 任何變量發生變化時觸發一個事件
- 13. 當字符串變量發生變化時事件未被觸發
- 14. 觸發器觸發基於與當前日期的時間差
- 15. 變量值變化時觸發事件
- 16. 當值不變時觸發backbone.js更改
- 17. 當事件源發生變化時,viewRender不會更新日曆
- 18. 如何在模型屬性發生變化時觸發事件
- 19. <input type =「number」/>什麼事件發生變化時觸發?
- 20. 表單發生變化時觸發的事件
- 21. AS3 ENTER_FRAME事件仍然在幀發生變化時觸發
- 22. Backbone.js事件觸發器
- 23. 觸發事件Backbone.js的
- 24. 如何在表格數據發生變化時觸發前端的NOTIFICATION事件
- 25. 日期在解析時發生變化
- 26. Java - 當對象的任何屬性發生變化時觸發事件
- 27. 當數據集發生變化時,SqlDependency不會觸發OnChange事件
- 28. 當asp文本框的文本發生變化時觸發事件
- 29. 當表格內容發生變化時,在SWT中觸發了哪個事件
- 30. 當元素的````屬性發生變化時,是否會觸發事件?
好奇 - 什麼是在投票點,如果每10秒它的評估當天的事?最好先確定持續時間,用setTimeout調用延遲事件觸發,然後用另一個setTimeout與86400重新初始化該函數? – 2013-05-10 03:17:39
這是個好主意。考慮添加一個答案。 – kapa 2013-05-10 07:11:20
謝謝大家!我希望避免使用setTimeout來支持更多的事件驅動方法,但考慮到我真的只需要知道日期何時發生變化,我不想只爲一個簡單的功能創建整個模型。我可能會實施涅ana的方法。再次感謝大家! – CullenJ 2013-05-10 16:09:52