在Backbone中,當我將Model的屬性設置爲與之前相同的值時,「更改」事件不會觸發。Backbone.js:始終檢測模型何時設置屬性,而不是/只是/更改
筆者認爲:
initialize: function() {
this.$lastRollResult = this.$el.find('#lastRollResult');
this.listenTo(this.model, 'change:lastRoll', this.render);
},
render: function() {
this.$lastRollResult
.html(this.model.get('lastRoll'))
.addClass('shaking');
var self = this;
setTimeout(function() {
self.$lastRollResult.removeClass('shaking');
}, 1000);
},
events: {
'click #button': function() {
this.model.set('lastRoll', getRandomInteger(1, 6));
}
}
有時候,當我打電話getRandomInteger(),INT引腳將是相同的前一個,導致我的.SET( 'lastRoll',int)以未註冊爲「改變「事件。但這意味着我的「搖動」課不會被應用,那很糟糕。
即使我設置的值是相同的,我怎樣才能使骨幹始終識別何時設置了屬性?是否有某種「設置」事件? (沒看到一個在文檔。)
編輯:我知道,有可能是一個{無聲:真正}/.trigger()黑客做到這一點,但沒有任何「乾淨」或「語義」的方式來始終識別.set()調用?
可能的重複[我可以強制更新模型的屬性註冊爲更改,即使它不是?](http://stackoverflow.com/questions/15931233/can-i-force-an-更新到模型的屬性註冊爲一改變,即使它) – Jackson
找到我的答案在一個重複的主題: http://stackoverflow.com/questions/15931233/can-i-force-an-update-to-a-models-attribute-to-register-as-a-change-even-if-it – Jackson