About using Ember.run.schedule

Hi guys, I need to do some operations in my controller after rendering the template.

I am using

Ember.run.schedule(“afterRender”,function(){- - - some code - - - }); .

I don’t know whether this is the correct way to achieve my use case. Can anyone suggest me a good solution to achieve this?

To start with, what are the operations you refer to? Given there are a couple approaches using some of Ember’s features, it helps to have a clear idea of what you need to do.

Generally speaking, you can use Ember’s runloop APIs, but I would suggest avoiding them as the programming model of the Octane edition will make direct interaction obsolete. Regardless of what the future holds, you can often get around runloop usage directly by shifting the logic into a component.

Depending on what that function needs to do you may only need to move the logic into the component. Otherwise you may need to invoke that logic using a component lifecycle hook like didRender.

To clarify using a component, it would look something like the following:

{{! this snippet would be your route's .hbs file, app/templates/profile.hbs }}
{{log "You can log to see when I actually render"}}
1 Like

Thanks efx. :slightly_smiling_face:

But for my use case, I think it is unnecessary to create a component.

Use Case:

I need to initialize a UI component after the element has created(rendered).

Then you should definitely make a component. Touching DOM elements from Controllers is not what they are for, and you will end up with fragile code that’s hard to upgrade (and unlikely to work in fastboot, if that matters to your app).

1 Like

Thanks a lot ef4. :slightly_smiling_face: