All Apps and Add-ons

Setting new keys in context with custom behavior

cmak
Contributor

I am trying to add keys in a custom behavior with context.set(), but am not exactly sure how to return the context afterwards. For example, this does not update the context.

In the XML, I have a custom Behavior Module with the name modifyAnyDeliverableSelection

In application.js, I have something like

Sideview.utils.declareCustomBehavior("modifyAnyDeliverableSelection", function(module) {

    module.onContextChange = function() {

        var context = this.getContext();


        context.set('variable',"foo");

        return context;
    }

});

The context does not get updated. Am I missing something here? Is it not getting returned properly?

1 Solution

sideview
SplunkTrust
SplunkTrust

onContextChange is the method to implement if you want the module to take action when it receives a new context from upstream. However it has nothing to do with effecting changes into the context for the modules that are downstream. For that you will want to implement getModifiedContext

Sideview.utils.declareCustomBehavior("modifyAnyDeliverableSelection", function(customBehaviorModule) {
    customBehaviorModule.getModifiedContext = function() {
        var context = this.getContext();
        context.set("foo", "someValue");
        return context;
    }
});

In this simple example all of the modules downstream from the CustomBehavior module can use $foo$ and it will have the value "someValue"

Incidentally, what are you using the CustomBehavior for? Sometimes I can just tell people about the lesser-known modules in Sideview utils and the same thing can be done without custom code.

View solution in original post

sideview
SplunkTrust
SplunkTrust

onContextChange is the method to implement if you want the module to take action when it receives a new context from upstream. However it has nothing to do with effecting changes into the context for the modules that are downstream. For that you will want to implement getModifiedContext

Sideview.utils.declareCustomBehavior("modifyAnyDeliverableSelection", function(customBehaviorModule) {
    customBehaviorModule.getModifiedContext = function() {
        var context = this.getContext();
        context.set("foo", "someValue");
        return context;
    }
});

In this simple example all of the modules downstream from the CustomBehavior module can use $foo$ and it will have the value "someValue"

Incidentally, what are you using the CustomBehavior for? Sometimes I can just tell people about the lesser-known modules in Sideview utils and the same thing can be done without custom code.

sideview
SplunkTrust
SplunkTrust

We talked further by email and narrowed the problem down to an exception that the custom code was throwing, and after fixing that it seems to be more or less squared away.

0 Karma

sideview
SplunkTrust
SplunkTrust

then that is caused by another problem in what you're doing. getModifiedContext is the correct method to use, trust me. it's not very meaningful which one is called before the other - they are simply called at different times in different situations. My advice is to post all your XML and all your JS.

0 Karma

cmak
Contributor

Yes it is a CustomBehavior module. When I use getModifiedContext instead of onContextChange, my JavaScript throws an error, as Deliverable_Select_v4 is not set in the context, and is null. getModifiedContext seems to be running much sooner than onContextChange, which runs after all the search queries are loaded in the upstream pulldowns

0 Karma

sideview
SplunkTrust
SplunkTrust

What kind of module are you attaching the customBehavior to? I assumed you were attaching it to a plain CustomBehavior module and you wanted the key to be available downstream. If that's the case, you absolutely implement getModifiedContext. getModifiedContext does not run only at the start of the page. It runs every time the module gives new data downstream.

0 Karma

cmak
Contributor

getModifiedContext does not seem to work, as I want my function to run only if the context has changed. Also getModifiedContext seems to run at the very start of the page, before the search result to populate the pulldown has returned.

0 Karma

cmak
Contributor

I am using the new ArrayValueSetter, which works great. However, I need the default option to also have two different values. One of my default values is very complicated, and I am taking all the options in my pulldown and constructing the value. I am using JavaScript to do this. I have the customBehavior module downstream from the pulldown module. This is why i am using onContextChange. If the pulldown has changed and the default label is selected, I dynamically construct the default value using all the other options in my pulldown. I am able to do this, but am unable to return the context.

0 Karma
Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...