Observers: should I use values passed to the function, or can I use this.value safely instead?

17 views
Skip to first unread message

Tony Mobily

unread,
Feb 9, 2017, 11:20:06 PM2/9/17
to Polymer
I have code like this in my app:

      observers: [
        '_dataGenericObserver(userData.generic)',
      ],

      // If this.userData.generic.id is set, then the register tab is
      // NOT visible. In this case, the selected tab mustn't be "register"...
      _dataGenericObserver: function(){
        if (this.userData.generic.id){
          this.selected = 'login';
        } else {
          if (this.defaultRegister) {
            this.selected = 'register';
          } else {
            this.selected = 'login';
          }
        }
      },

Is this safe? Or should I *always* do this instead:

      observers: [
        '_dataGenericObserver(userData.generic)',
      ],

      // If this.userData.generic.id is set, then the register tab is
      // NOT visible. In this case, the selected tab mustn't be "register"...
      _dataGenericObserver: function(generic){
        if (generic.id){
          this.selected = 'login';
        } else {
          if (this.defaultRegister) {
            this.selected = 'register';
          } else {
            this.selected = 'login';
          }
        }
      },

...?
I noticed in some cases in other parts of the app that if I have an observer on `_someFunc(value.subvalue)`, and then have `_someFunc: function( subValue)`, in the function `subValue` is set, whereas `this.value.subValue` isn't. However, I am not able to replicate it - sorry.

Questions:

* Is it always recommended to use the values passed to the functions, rather than `this.*`?
* WHEN is it likely to happen, that a `subValue` is set as a function parameter, but NOT in `this.value.subValue`?
Reply all
Reply to author
Forward
0 new messages