admin管理员组文章数量:1415467
This question is e up with Component Example from the homepage of Ember.js, here's the code:
App.GravatarImageComponent = Ember.Component.extend({
size: 200,
email: '',
gravatarUrl: function() {
var email = this.get('email'),
size = this.get('size');
return '/' + hex_md5(email) + '?s=' + size;
}.property('email', 'size')
});
When I try this example I noticed that everytime I input a character the browser will fire a request to get the avatar. If my email address have 30 characters, there will be 30 times requests got fired until the right one was returned from the server. I think this is too inefficient, isn't it?
After digging out the guides, I didn't find there is a way to solve this problem, so I was wondering: is this possible to delay re-pute a puted property for, say, like 1000ms? or is there a better way to deal with this type of scenario?
This question is e up with Component Example from the homepage of Ember.js, here's the code:
App.GravatarImageComponent = Ember.Component.extend({
size: 200,
email: '',
gravatarUrl: function() {
var email = this.get('email'),
size = this.get('size');
return 'http://www.gravatar./avatar/' + hex_md5(email) + '?s=' + size;
}.property('email', 'size')
});
When I try this example I noticed that everytime I input a character the browser will fire a request to get the avatar. If my email address have 30 characters, there will be 30 times requests got fired until the right one was returned from the server. I think this is too inefficient, isn't it?
After digging out the guides, I didn't find there is a way to solve this problem, so I was wondering: is this possible to delay re-pute a puted property for, say, like 1000ms? or is there a better way to deal with this type of scenario?
Share Improve this question edited Nov 18, 2015 at 18:14 Giao1968 25.9k11 gold badges76 silver badges105 bronze badges asked Jul 27, 2014 at 14:04 nightirenightire 1,3712 gold badges11 silver badges21 bronze badges2 Answers
Reset to default 5You can debounce it, which gives you a similar feeling of it reacting without having to do anything other than typing:
App.GravatarImageComponent = Ember.Component.extend({
size: 200,
email: '',
fetchEmail:'',
watchEmail: function(){
Em.run.debounce(this, this.setEmail, 400)
}.observes('email'),
setEmail: function(){
this.set('fetchEmail', this.get('email'));
},
gravatarUrl: function() {
var email = this.get('fetchEmail'),
size = this.get('size');
return 'http://www.gravatar./avatar/' + md5(email) + '?s=' + size;
}.property('fetchEmail', 'size')
});
Example: http://emberjs.jsbin./lacefi/1/edit
I had to put a debouncer on my observer to limit execution of my actual business logic in one of my project.
In this case, you can change your puted property gravatarUrl
to normal property and move logic to build url on a observer, which observes email
and size
and update gravatarUrl
, and then simply put a debouncer on your observer. I haven’t tested below code but here is the idea:
App.GravatarImageComponent = Ember.Component.extend({
size: 200,
email: '',
gravatarUrl: '',
gravObserver: function() {
Ember.run.debounce(this, this.updateGravUrl, 1000);
}.observes('email', 'size'),
updateGravUrl: function(){
var email = this.get('email'),
size = this.get('size');
this.set('gravatarUrl','http://www.gravatar./avatar/' + hex_md5(email) + '?s=' + size);
}
});
本文标签: javascriptHow to delay computed properties to recomputed themselves in EmberjsStack Overflow
版权声明:本文标题:javascript - How to delay computed properties to recomputed themselves in Ember.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745184106a2646601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论