5

I have an emberjs app that is structured very much like the sample here: https://github.com/tildeio/bloggr-client

The question I have is that when the user clicks on a 'post' on the left, is there any event I can subscribe to in the post view/route/controller that i can tap into? A javascript library I'm using (gridster) requires some javascript to be run as the user switches from post to post. I've tried using didInsertElement but that doesn't seem to fire (using 1.0.5)

Ben
  • 16,124
  • 22
  • 77
  • 122

1 Answers1

6

You can observe the PostController's content property. This can be done from anywhere that has access to the PostController. For example, from PostView:

App.PostView = Ember.View.extend({
  templateName: 'post',
  contentDidChange: function() {
    console.log('content changed to: ', this.get('controller.content'));
  }.observes('controller.content')
})
Mike Grassotti
  • 19,040
  • 3
  • 59
  • 57
  • Yeah you might have to debounce it. – Mike Grassotti Jun 11 '13 at 21:56
  • what do you mean by debounce it? Also according to the documentation didInsertElement() on the View should trigger... could this be a bug in 1.0.5? – Ben Jun 11 '13 at 22:46
  • 1
    didInsertElement will trigger when PostView is inserted into the dom. So didInsertElement() will be called if you switch from, say /posts to /posts/123 but not if you go from posts/123 to posts/456. Re: debounce I mean taking steps to ignore the duplicates. Can you be more specific about when it fires twice, or post a jsFiddle? – Mike Grassotti Jun 12 '13 at 20:32
  • I can't post a jsFiddle. is there any event for when you go from posts/123 to posts/456 ? – Ben Jun 13 '13 at 09:59
  • 1
    Not AFAIK. When you switch between posts, the route's model changes. That's what the observes('controller.content') is meant to detect. – Mike Grassotti Jun 13 '13 at 17:20
  • I couldn't make this work for `Ember.Component` (which extends `Ember.View`). [See this JS Bin](http://jsbin.com/kozav/1/edit). I would expect at least one of the observing methods to fire. What has to be done to make a component react on a model change? (In the JS Bin, observe the missing output on the *Console* tab) – Abdull Feb 22 '14 at 23:43