kyassuru
kyassuru

Reputation: 163

How to trigger code on view transitions

I have a view something like this.

App.TestView = Em.View.extend 
  templateName: 'test'
  modelDidChange: (() ->
    # fires twice, need to check state to make sure it doesn't
    if @state is 'preRender'
      # ...do stuff
  ).observes('controller.content')

I want to be able to trigger whenever a view changes to a new model, for instance if we were to go from route /test/123 to /test/456 in my app. The code above works for this but seems strange. I don't think I should be looking at the state of the view. Is there a "correct" way of going about this? I cant seem to find any other options. I've tried using an on('init') hook, but the controller appears to be undefined if I use that.

Upvotes: 0

Views: 33

Answers (1)

Bavo Van Geit
Bavo Van Geit

Reputation: 852

I would do this in the route, using the setupController hook. It will be called when the model changes.

App.TestRoute = Ember.Route.extend({
  setupController: function (controller, model) {
    // Call _super for default behavior
    this._super(controller, model);
    console.log('model change:'+model.get('id'));
    # ...do stuff
  }  
});

jsbin example: http://emberjs.jsbin.com/potehi/1/edit

Upvotes: 1

Related Questions