dojo/subscribe does not work if the document is not ready - javascript

I have a small requirement. We have a application controller and a custom dijit which work together with each other.
I want to publish a event from the dijit and subscribe the same in controller. But the problem is the controller is not loaded first time when publish event is triggered, so the subscribe is not working first time.
Once the controller is loaded, if we publish again, then subscribe works perfectly fine.
Is there any solution to this problem ?

Have you tried dojo/ready to make sure, all necessary Parts are loaded? Check your loading hierarchy. Is it possible to load the controller in the init-phase at the start?
Regard

Thanks for your answers Dimitri and Ken Franqueiro & MiBrock. Dimitri's solution sound useful to me. The controller and the Widget which publish the event are not on same page also the controlller is not always loaded whereas the Widget is part of my Menu so its present on every page.
What I tried is used Memory to pass the data and first time called the function where the event and data will be used. So my widget sets data in Memory store and in controller, once its loaded we retrieve the value and use it. So this happens only first time, from second time onwards my published calls are working as Controller is loaded on page.

Related

How can I wait for the create/edit entity modal window to finish rendering and execute my custom js code in 2sxc module?

When user presses either create entity button or edit entity button, there's the same modal window in an iframe that is build by js dynamically. So what I'm trying to do is to wait until it's fully rendered and then execute my custom js code. So is there a proper way to do that? Some sort of event like RenderFinished shooting or something? Don't want to use timeout since I understand that it's not a good way to do that.
What I tried so far is that I've added jquery to the page programmatically, since it's not used currently at that particular page for some reason (probably because iframe is built dynamically without jquery and I needed to add it myself).
After that I tried to access iframe via jquery selector and then on iframe.ready access element inside in the same manner (selector and even ready for that element). But iframe is accessed and element inside it is not. Console log inside ready function just outputs no elements found. When I placed breakpoint inside I saw that there's no modal window built yet and my code is executed synchronously before it. So there's nothing to find yet at that moment.
Oh and I tried to put it all inside $(document).ready, of course. But it didn't change the situation neither...
Any ideas about how to do that properly?
The final goal why am I doing all this complicated dancing: I'm trying to add validation that UrlKey for entity is unique. So I want to bind my js function to UrlKey input's onchange event and call backend api to do the validation and return new UrlKey if it wasn't unique and edit the UrlKey input accordingly. The problem that I stumbled upon is to execute my code after modal iframe window is rendered.
Any tips are highly appreciated.
You are in luck :)
2sxc added a Formula feature which will help you with this. There are videos and tutorials and more. See http://r.2sxc.org/formulas

Force a directive to always load prior to a controller

I am working on a UI issue. There is a misfire in load order that happens 1 out of every 50 times or so. The Angular JS app has a shared directive as a navigation button panel and that part is used by most of the other controllers.
Normal firing order
ButtonController.init() <--Set all buttons to hidden
EditGroupController.init() <--Set all buttons based on service call data
When the problem happens firing order
EditGroupController.init() <--Set all buttons based on service call data
ButtonController.init() <--Set all buttons to hidden
In scenario 2 above the EditGroupController tries to invoke an event on the ButtonController via:
$rootScope.$broadcast('setHeadersButtons', buttons);
However, when I trace into the angular internal code there is a logic branch where it looks for a registered listener with that name and it does not find one to fire because the module has not been loaded.
I remember in an Angular 7 app I used the APP_INITIALIZER to force a core piece to load first. Sorry if my wording is not precise front end / angular is not my specialty and this somewhat fell into my lap. Is there a way to ensure a directive in Angular JS is loaded before any other pieces?
There is an app.route.js, can I force the directive to load there?

Is it possible to have a plugin triggered on any page load without having to re initialize it on every view loaded

I do not have any code to display as code would not help towards my question. I am wondering if there is a advanced JavaScript technique that can allow my plugin be called and ran against all items needed but without having to initialize it on every view in mb. Now the layout shared method is not what I mean. So fir example if I placed said plugin in a global scope in one he file and did a regular
$(document).ready(call plugin unit here)
Would that then be bound to every page and view so I do not need it to be added on every view or is thee some kind of event I can bind to when i initialize my plugin within the global.js file so it will be ran on any view or HTML page that the user goes to working on its elements? Kind of how using the older method event binding ".live" would handle all current items and future ones.
I am trying to find this out or find out if possible how to handle it. My plugin I'm testing with is proprietary so cannot post code here but uses the regular concept of a each loop on on elements affected. Hopefully my question has been stated with a clear goal.
Now understanding that it needs to run against any new target items that may not have been loaded prior but may come down the pipe a few page navigations away I'm not sure I can do this. Unless there is some kind of event in the global JavaScript scope I can bind to and during initialization of my plugin within the one section of code will then cause it to run against all items on what ever page is loaded. Please no rude downing with "we need to see code" as that would not affect an answer here and as mentioned the plugin cannot be publically displayed. Please helpful comments only and sorry if over complicating this and overlooking the answer. Thanks in advance.

Nativescript angular 2 call Function On page load every time

I want to execute a piece of code every time a new the page in navigated to. I am using the <page-router-outlet>.
As mention here, while using page-router-outlet the component is cached. So when it is navigated back into view no Init lifecycle events of angular 2 are executed.
I tied using just the , which does not cache the component and as a result call the Init Lifecycle events. But using it exits the app when I press the Hardware back button.
The site you linked to also mentions this: "What you can do is inject Page inside your component and attach to page navigation events (for example navigatedFrom) and do the cleanup there." Those events are listed here, especially the "navigatedTo" event seems relevant for your usecase.

Angular JS fires ng-click before loading the resource at href

I have a link/anchor HTML like:
<a href='/some-form' ng-click='someFunction(item)'>Text</a>
What I have in mind is that user clicks this link, then I want to load an HTML from server, and after the loading of that HTML, I want someFunction to be executed, which fills the loaded form with some data.
However, by debugging my code, it seems that Angular JS first fires someFunction function, and then browser loads the HTML. Not only I want this to be reverse, but also I need them to be executed sequentially (synchronously).
Is my design a good design? Is there any other way to achieve this behavior? If not, what should I do to make it work?
so I think someFunction(item) should be in the other controller dealing with the route /some-form.
You may also consider using $location to manually navigate to /some-form.

Categories

Resources