简体   繁体   中英

Ractive.js Pattern Indirect Observer Reports Same Value

I use Ractive.js to bind a javascript object and an HTML form. My object looks like this:

entity: {
  id: 'some id'
  type: 'a type'
  names: [
     {
       locale: 'en',
       name: 'some name'
     },
     {
       locale: 'tr',
       name: 'some translation'
     }
  ]
}

I'd like to subscribe for name updates. I register for updates in the following way:

ractive.observe({
  // indirect observer
  'entity': function(newValue, oldValue, key) {
    console.log(key + ' is changed from ' + oldValue + ' to ' + newValue);
  },
  // direct observer
  'entity.names.*': function(newValue, oldValue, key, index) { 
     console.log(key + ' is changed from ' + oldValue + ' to ' + newValue + ' and index is ' + index);
  }
}

However, oldValue and newValue is always the same (equal to the most recent value of observed pattern) for both observers above.

This behavior is expected for direct observer according to their documentation . However, indirect observer should provide different values.

For v0.7.3 on lines 2648 and 2743 the values are copied into pattern observer. However, that copy is a shallow copy. So if the actual value is updated, then the values in the observer are updated, even though we expect pattern obverser to keep value before the update.

More discussion on this could be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM