Ractive.js 模式间接观察者报告相同的值
Ractive.js Pattern Indirect Observer Reports Same Value
我使用 Ractive.js 来绑定一个 javascript 对象和一个 HTML 表单。我的对象看起来像这样:
entity: {
id: 'some id'
type: 'a type'
names: [
{
locale: 'en',
name: 'some name'
},
{
locale: 'tr',
name: 'some translation'
}
]
}
我想订阅名称更新。我通过以下方式注册更新:
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);
}
}
然而,oldValue
和 newValue
对于上面的两个观察者来说总是相同的(等于观察模式的最新值)。
根据他们的 documentation,这种行为对于直接观察者来说是预期的。但是,间接观察者应该提供不同的值。
- 0.8.0-edge 示例:http://jsfiddle.net/0mdjdq4u/5/
- 0.7.3 示例:http://jsfiddle.net/0mdjdq4u/6/
我使用 Ractive.js 来绑定一个 javascript 对象和一个 HTML 表单。我的对象看起来像这样:
entity: {
id: 'some id'
type: 'a type'
names: [
{
locale: 'en',
name: 'some name'
},
{
locale: 'tr',
name: 'some translation'
}
]
}
我想订阅名称更新。我通过以下方式注册更新:
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);
}
}
然而,oldValue
和 newValue
对于上面的两个观察者来说总是相同的(等于观察模式的最新值)。
根据他们的 documentation,这种行为对于直接观察者来说是预期的。但是,间接观察者应该提供不同的值。
- 0.8.0-edge 示例:http://jsfiddle.net/0mdjdq4u/5/
- 0.7.3 示例:http://jsfiddle.net/0mdjdq4u/6/