Access widget config.json from a user-created js

1663
5
Jump to solution
06-24-2016 11:26 AM
Alexwang
Occasional Contributor II

I can use this.config.<> to get any configurations in config.json from the Widget.js. But how can i access it from  a usercreated.js?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Alex,

  If the usercreated.js is called from the widgets Widget.js then all you need to do is pass a reference to the widget into the js file in the js files constructor or use a public var in your js file and set it to "this".

View solution in original post

5 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Alex,

  If the usercreated.js is called from the widgets Widget.js then all you need to do is pass a reference to the widget into the js file in the js files constructor or use a public var in your js file and set it to "this".

View solution in original post

Alexwang
Occasional Contributor II

Thanks again Robert. Worked.

the only thing is that if i have 100 parameters to pass, then I need to pass all of them as parameters in either the constructor or methods.

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Alex,

NO absolutely not. Just pass the widget object (i.e. yourJS.parentWidget = this inside your Widget.js) and then you can use parentWidget.config.xyz in your js file.

0 Kudos
Alexwang
Occasional Contributor II

ok, i think i was confused.

currently I can make it work like below. can you please share your codes so i can access all parameters in the config.json within User.js?

in config.json

"xyz": "USA",

"abc": "Canada"

...

in Widget.js

constructor: function (options) {

            ......

            this.UserJS = UserJS.getInstance(options.config.xyz);

},

in User.js

clazz.getInstance = function (xyz) {

        if (instance === null) {

            instance = new clazz();

        }

        instance.xyz = xyz;

        return instance;

    };

so within User.js, console.log(this.xyz) will return "USA"

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Alex,

  In Widget.js you would create a new instance of your User.js i.e

var user = new User();

user.parentWidget = this;

and in the User.js you would have a public var like:

return declare([_WidgetBase, Evented], {

     parentWidget: null;

The you use this.parentWidget.config.xyz as this.parentWidget a reference to the Widget.js.

0 Kudos