Accessing an nls string from an event

1285
3
Jump to solution
04-06-2017 02:41 PM
KenBuja
MVP Esteemed Contributor

I've created a Toggle Button in the postcreate function

this.signin = new ToggleButton({
  showlabel: true,
  checked: false,
  label: this.nls.signin,
  onChange: this._onBtnSignInChanged
}, this.btnSignIn);‍‍‍‍‍‍

In the onChange event, I'd like to change the label to another string.

_onBtnSignInChanged: function (val) {
  if (val) {
    this.set('label', nls.signin);
  } else {
    this.set('label', nls.signout);
  }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍

However, this gives the error "Uncaught TypeError: Cannot read property 'signout' of undefined". What's the right way of accessing the nls to get that string? I suspect I have to use lang.hitch somewhere, but everywhere I've tried gives me another undefined error.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Ken,

   If this.nls.signin (like Sapna recommends) in your function does not do the trick, then your lang.hitch would be here:

this.signin = new ToggleButton({
  showlabel: true,
  checked: false,
  label: this.nls.signin,
  onChange: lang.hitch(this, this._onBtnSignInChanged)
}, this.btnSignIn);‍‍‍‍‍‍

View solution in original post

0 Kudos
3 Replies
sapnas
by
Occasional Contributor III

It looks like you  are not referencing "this" in onChange event. shouldnt that be this.nls.signin and this.nls.signout. assuming that signout is defined in string.js?

RobertScheitlin__GISP
MVP Emeritus

Ken,

   If this.nls.signin (like Sapna recommends) in your function does not do the trick, then your lang.hitch would be here:

this.signin = new ToggleButton({
  showlabel: true,
  checked: false,
  label: this.nls.signin,
  onChange: lang.hitch(this, this._onBtnSignInChanged)
}, this.btnSignIn);‍‍‍‍‍‍
0 Kudos
KenBuja
MVP Esteemed Contributor

In my original code, I had this.nls.signin but had stripped out the "this" at one point as I tried various options. This is what finally worked (although I could have sworn I tried this yesterday)

this.signin = new ToggleButton({
 showlabel: true,
 checked: false,
 label: this.nls.signin,
 onChange: lang.hitch(this, this._onBtnSignInChanged)
}, this.btnSignIn);

_onBtnSignInChanged: function (val) {
  console.log(this.nls.signout);
  if (val) {
    this.signin.set('label', this.nls.signin);
  } else {
    this.signin.set('label', this.nls.signout);
  }
}