Register an Action with Javascript

Is there a way to register an action to a model from javascript? 

Hmmm … don’t understand why you’d need to, but I don’t think any action framework can be registered from javasript. What’s the use case? You can setup an action on model using model actions.

Alternatively, you could use our Custom Compenent called “Template w/ Action Frameworkand Context” to store Action Framework and call it using Javascript.

Well I needed it because I was working on a component that integrated with a model already existing on the page, and I needed to set a action on the “model.loaded” event.
Eventually found a hack for it injecting the action into the model and re-initializing it:

model&#46;actions = model&#46;actions || []; var action = { actions: '<actions><action type="publish" event="' + eventName + '"/></actions>', events: ["models&#46;loaded"] }; model&#46;actions&#46;push(action); model&#46;initialize({});  

Well that was nifty!

Nifty indeed, however I’m not sure taking this approach would be best.  At the very least, it would likely be an unsupported scenario.

Velvel -

If I’m understanding what your goal is, you want something to happen when a model is loaded.  You can accomplish this by subscribing to the models.loaded event from within your component using the skuid.events.subscribe api.  In this manner, you’re component is in control and doesn’t rely directly modifying the underlying model data structure.  Unless absolutely necessary, skuids data structures shouldn’t be directly manipulated.

skuid.events.subscribe('models.loaded',&nbsp;function(data)&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;// do something<br>});


If you have the above code run from within your component, your component will get notified every time a model is loaded.  The params passed give you which model, etc. so you can have conditional logic on if/when you should do certain things.

The one thing to note with the above is that if your component is conditionally rendered, your “render” method will get called multiple times within a page lifetime so you’ll want to make sure you unsubscribe when unrendered to avoid duplicate notifications.