[英]Polymer 1.0 Observe Array
I just tried to update my Polymer Website to version 1.0, but my array-observer-function does not work anymore. 我只是尝试将Polymer网站更新为1.0版,但是我的array-observer-function不再起作用。 So I had a look in the docs and noticed, that there is a new way to observe push & pop changes. 因此,我在文档中进行了查看,并注意到,有一种观察推送和弹出更改的新方法。
To test these changes, I copied the code from the Polymer docs , but even that example doesn't work... Here is my try to test the example script: 为了测试这些更改,我从Polymer docs复制了代码,但是即使该示例也不起作用...这是我尝试测试示例脚本的方法:
<!doctype html>
<html>
<head>
<title>Test</title>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<script src="/bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="/bower_components/polymer/polymer.html">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<my-index></my-index>
</body>
</html>
<dom-module id="my-index">
<script>
Polymer({
is: "my-index",
properties: {
users: {
type: Array,
value: function() {
return [];
}
}
},
observers: [
'usersAddedOrRemoved(users.splices)'
],
ready: function(){
this.addUser();
},
usersAddedOrRemoved: function(changeRecord) {
console.log(changeRecord);
changeRecord.indexSplices.forEach(function(s) {
s.removed.forEach(function(user) {
console.log(user.name + ' was removed');
});
console.log(s.addedCount + ' users were added');
}, this);
},
addUser: function() {
this.push('users', {name: "Jack Aubrey"});
}
});
</script>
</dom-module>
The javascript-console just says Uncaught TypeError: Cannot read property 'indexSplices' of undefined
. javascript控制台仅显示Uncaught TypeError: Cannot read property 'indexSplices' of undefined
。 Any ideas what is wrong? 任何想法有什么问题吗?
The usersAddedOrRemoved function is firing at the start when the users array is created (with an undefined changeRecord). 创建users数组时(带有未定义的changeRecord),在开始时会触发userwegoOrRemoved函数。 If you handle the undefined you'll see it works as expected and fires both at the start and on the user added. 如果您处理未定义的内容,您会看到它可以按预期工作,并且会在开始时和添加的用户上触发。
usersAddedOrRemoved: function(changeRecord) {
console.log(changeRecord);
if (changeRecord) {
changeRecord.indexSplices.forEach(function(s) {
s.removed.forEach(function(user) {
console.log(user.name + ' was removed');
});
console.log(s.addedCount + ' users were added');
}, this);
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.