最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ember js component observer does not work - Stack Overflow

programmeradmin3浏览0评论

I have a scenario in emberjs ponent where the observe does not get hit. I figured out the reason as "the ponent is not yet inserted when the ponent property being observed is set."

My questions is, couldn this be handled in a better way in ember js?

Better explanation can be found in the below jsbin`s.

Not working Scenario

Working scenario

I have a scenario in emberjs ponent where the observe does not get hit. I figured out the reason as "the ponent is not yet inserted when the ponent property being observed is set."

My questions is, couldn this be handled in a better way in ember js?

Better explanation can be found in the below jsbin`s.

Not working Scenario

Working scenario

Share Improve this question asked Jan 6, 2015 at 16:08 wallopwallop 2,5811 gold badge26 silver badges39 bronze badges 5
  • What exactly are you trying to acplish here? In this scenario, both observer functions would be better served as a puted property (via Ember.Computed). – awgreenarrow08 Commented Jan 6, 2015 at 18:45
  • Agreed, observers are overkill in both situations – Kingpin2k Commented Jan 6, 2015 at 22:19
  • Like i have mentioned in the ments, this was an example and not the actual use case. The actual use case being a huge one i thought of simplyfying it. Basically there is a lot of calculation in the observer and its not just assigning it to another property Also when you say " both observer functions would be better served as a puted property " if you use puted property in ponent it wont work owing to the same reason why the observer is not working – wallop Commented Jan 7, 2015 at 1:35
  • The question you need to ask yourself is, why am I puting something, that isn't going to be used. If it's going to be used, why am I not using a puted property? – Kingpin2k Commented Jan 7, 2015 at 2:23
  • @Kingpin2k may be i didn get your point the right way but i feel i didn explain well either. The example stated is just an example and not the actual use case i am ending up with! I am puting something that is being used but i am puting more than one. Since i am puting more than one i felt its better to use observers than puted properties! – wallop Commented Jan 7, 2015 at 5:59
Add a ment  | 

1 Answer 1

Reset to default 16

You can specify .on('init') to force the observers to run right after initialization; otherwise like @Kingpin2k mentioned - they don't run

App.TextboxDisplayComponent = Ember.Component.extend({
  displayText: '',        

  boundProperty: '',

  observeBoundProperty: function () {
    this.set('displayText', this.get('boundProperty'));        
  }.observes('boundProperty').on('init')
});

Your (non-)working example here

发布评论

评论列表(0)

  1. 暂无评论