Splunk Dev

SplunkJS: What's the difference between get() and getInstance()?

marco_sulla
Path Finder

What is the difference between:

mvc.Components.get(id)

and

mvc.Components.getInstance(id)

in SplunkJS?

Is there a reference for Splunk mvc library?

Tags (2)
1 Solution

marco_sulla
Path Finder

With getInstance() you can also create the component if it does not exists. From $SPLUNK_HOME/share/splunk/search_mrsparkle/exposed/js/splunkjs/mvc/registry.js:

/**
 * @param id                The id of the instance to return.
 * @param options           (Optional).
 * @param options.create    Whether to create a new token model with
 *                          the given name if the specified instance
 *                          does not exist.
 * @return                  The specified instance, or a falsy value
 *                          if the instance does not exist and
 *                          options.create is omitted or false.
 */
getInstance: function(id, options) {
    options = options || {};

    // If an instance with this id doesn't exist,
    // figure out whether we want to create one or not.
    if (!this.has(id)) {
        if (options.create) {
            this.registerInstance(id, this._createTokenModel(id));
        }
        else {
            console.error(this.createError("No instance with id: " + id));
        }
    }

    return this.get(id);
},

View solution in original post

marco_sulla
Path Finder

With getInstance() you can also create the component if it does not exists. From $SPLUNK_HOME/share/splunk/search_mrsparkle/exposed/js/splunkjs/mvc/registry.js:

/**
 * @param id                The id of the instance to return.
 * @param options           (Optional).
 * @param options.create    Whether to create a new token model with
 *                          the given name if the specified instance
 *                          does not exist.
 * @return                  The specified instance, or a falsy value
 *                          if the instance does not exist and
 *                          options.create is omitted or false.
 */
getInstance: function(id, options) {
    options = options || {};

    // If an instance with this id doesn't exist,
    // figure out whether we want to create one or not.
    if (!this.has(id)) {
        if (options.create) {
            this.registerInstance(id, this._createTokenModel(id));
        }
        else {
            console.error(this.createError("No instance with id: " + id));
        }
    }

    return this.get(id);
},
Get Updates on the Splunk Community!

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

April 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...