I’m working on a custom dialog like alert.js, smoke.js, …etc. and i was wondering what is the best practice do something like this.
Currently it works like this…
When app starts, I create an initializer called alert, which is an Ember.Object.
To create a dialog, I would call this.alert.confirm('message');
When confirm function is called it would update message attribute, and setup interval function of 250ms, so it would poll answer property.
When message gets updated, x-alert component would show dialog. On confirmation or cancel, it would set shared property answer (alias for alert.answer)
confirm function will return a promise. Promise is resolve or rejected based on answer (true => resolve, false => reject), and it would clear interval if answer was found. Other than that, interval will keep checking.
Is there a simpler approach or a way around using setInterval for checking for answer?
Update
I’ve created a gist for the alert service that gets injected in Route, Controller, Component scope.
I am pretty new to emberjs myself, so I don’t think I can provide any reliable input on your questions, but I felt probably these links might be useful to you:
I have implemented the same functionality in a different way, using only actions, with no need for promises:
Implement ‘confirm’ action on application route with associated hidden view in application template.
Other actions that need to be confirmed have a confirmed boolean parameter. If not confirmed,
these actions trigger ‘confirm’ action and do nothing else.
For confirm action handling: application route sets message on
confirmation dialog and dialog is shown (it is always in the
application template). If confirmed the original action is re-sent
to the triggering action with the flag confirmed=true and all other
arguments. You may need to send the target for the action to the
confirm action if it is not a route.