Accessing Vue-data while in $ons.notification



  • How can I retrieve from data while in this.$ons.notification.confirm callback?

    <script>
    export default {
      name: 'groups',
      data () {
        return {
          myGroups: JSON.parse(localStorage.getItem("myGroups")),
        }
      },
      methods: {
        deleteGroup: function(groupId) {
    
          console.log(this.myGroups); // ****** OKE here ******
    
          this.$ons.notification.confirm('Are you sure?',
              { modifier: 'material',
                cancelable: true,
                title: 'Delete group?',
                buttonLabels: ['Yes', 'No'],
                callback: function(answer) {
                    if (answer == 0) { //YES
                      console.log(this.myGroups) // ***** Undefined here ?? *****
                    }
                }
              })
        }
    }
    </script>
    


  • The above is probably not OnsenUI related and has something to do with scope/async.
    But doing it like this:

    $ons.notification.confirm('Are you sure?',
              { modifier: 'material',
                cancelable: true,
                title: 'Delete group?',
                buttonLabels: ['Yes', 'No']}
     ).then((response) => {
        console.log(this.myGroups) // ***** Works FINE !!! *****
      });
    

    as stated here: https://onsen.io/v2/api/vue/$ons.notification.html

    it works fine…


  • Onsen UI

    @Bassie JavaScript issue: Use an arrow function instead. Normal functions create new contexts but arrow functions don’t.


Log in to reply