Polymer 1.0 数据绑定不起作用
Polymer 1.0 data binding not working
我有以下聚合物元素:
navigator.currentStep 的值在调用 someMethod 后未更新。
<dom-module id="m">
<template>
Navigator step = <span>{{navigator.currentStep}}</span>
</template>
</dom-module>
Polymer({
is: 'm',
ready: function() {
this.navigator = new Navigator(1);
console.log(this.navigator.currentStep); // 1
},
someMethod: function() {
this.navigator.next();
console.log(this.navigator.currentStep); // 2
}
});
输出总是
Navigator step = 1
但下面的作品
<dom-module id="m">
<template>
Navigator step = <span>{{currentStep}}</span>
</template>
</dom-module>
Polymer({
is: 'm',
ready: function() {
this.navigator = new Navigator(1);
this.currentStep = this.navigator.currentStep; // 1
},
someMethod: function() {
this.navigator.next();
this.currentStep = this.navigator.currentStep; // 2
}
});
呼叫this.notifyPath('navigator.currentStep', this.navigator.currentStep)
.
参见 https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#set-path。
Sometimes imperative code needs to change an object’s sub- properties directly. As we avoid more sophisticated observation mechanisms such as Object.observe or dirty-checking in order to achieve the best startup and runtime performance cross-platform for the most common use cases, changing an object’s sub-properties directly requires cooperation from the user.
Specifically, Polymer provides two API’s that allow such changes to be notified to the system: notifyPath(path, value)
and set(path, value)
, where path is a string identifying the path (relative to the host element).
我有以下聚合物元素:
navigator.currentStep 的值在调用 someMethod 后未更新。
<dom-module id="m">
<template>
Navigator step = <span>{{navigator.currentStep}}</span>
</template>
</dom-module>
Polymer({
is: 'm',
ready: function() {
this.navigator = new Navigator(1);
console.log(this.navigator.currentStep); // 1
},
someMethod: function() {
this.navigator.next();
console.log(this.navigator.currentStep); // 2
}
});
输出总是
Navigator step = 1
但下面的作品
<dom-module id="m">
<template>
Navigator step = <span>{{currentStep}}</span>
</template>
</dom-module>
Polymer({
is: 'm',
ready: function() {
this.navigator = new Navigator(1);
this.currentStep = this.navigator.currentStep; // 1
},
someMethod: function() {
this.navigator.next();
this.currentStep = this.navigator.currentStep; // 2
}
});
呼叫this.notifyPath('navigator.currentStep', this.navigator.currentStep)
.
参见 https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#set-path。
Sometimes imperative code needs to change an object’s sub- properties directly. As we avoid more sophisticated observation mechanisms such as Object.observe or dirty-checking in order to achieve the best startup and runtime performance cross-platform for the most common use cases, changing an object’s sub-properties directly requires cooperation from the user.
Specifically, Polymer provides two API’s that allow such changes to be notified to the system:
notifyPath(path, value)
andset(path, value)
, where path is a string identifying the path (relative to the host element).