Register an Action with Javascript

edited June 25 in Questions
Is there a way to register an action to a model from javascript? 

Comments

  • Pat VachonPat Vachon Member ✭✭
    edited June 25
    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.

    image

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

  • edited April 2017
    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.actions = model.actions || []; var action = {     actions: '<actions><action type="publish" event="' + eventName + '"/></actions>',     events: ["models.loaded"] };  model.actions.push(action);  model.initialize({});  
  • Pat VachonPat Vachon Member ✭✭
    edited April 2017
    Well that was nifty!
  • Barry SchnellBarry Schnell Member ✭✭
    edited April 2017
    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', function(data) {              
           // do something
    });
    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.


Sign In or Register to comment.