聚合物1.0组件是否有办法注册页面上发生的所有按键操作,无论重点在哪里?
在预组件世界中,我会加入文档正文中的关键事件。什么是干净的,组件驱动的方法?
答案 0 :(得分:0)
我能够通过实施Polymer.IronA11yKeysBehavior
behavior来实现这一目标。
我基本上只是阅读为该演示定义的组件的源代码,is here
我在对原始问题的评论中提到的错误只是让我keyEventTarget
properties
properties
对象放在<dom-module id="x-component">
<template>
Type shift+a to see feedback.
<div id="feedback"></div>
</template>
</dom-module>
<script>
Polymer({
is: 'x-component',
behaviors: [Polymer.IronA11yKeysBehavior],
keyBindings: {
'shift+a': '_handleKey'
},
_handleKey: function(e) {
this.$['feedback'].innerHTML = this.$['feedback'].innerHTML + "<p>again</p>";
},
properties: {
keyEventTarget: {
type: Object,
value: function() {
return document.body;
}
}
}
});
</script>
对象上方的一两个屏幕上; d已经放入页面,优先。
这是一个非常简单的组件,可以显示它的实际效果:
def play_game(initial_funds, price_to_play, iterations):
prob_tree = {}
for iteration in range(1, iterations + 1):
prob_tree[iteration] = []
# first number always represents money in hand, second represents money in pot.
prob_tree[1].append([initial_funds - price_to_play, 1])
for iteration in range(2, iterations + 1):
# for each possible outcome listed in the line above in the probability tree
for possibility in prob_tree[iteration - 1]:
# if there isn't any money in the pot AND not enough current funds, just repeat it twice in the next line for counting purposes.
if possibility[0] < price_to_play and possibility[1] == 0:
prob_tree[iteration].append(possibility)
prob_tree[iteration].append(possibility)
# add the current possibility combo twice to the current iteration.
# elif there is money in pot, append a heads and tails possibility to the current row
elif possibility[0] < price_to_play and possibility[1] > 0:
case2_old_pot = possibility[1]
case2_win_new_pot = possibility[1] * 2
case2_lose_new_pot = 0
case2_player_funds = possibility[0]
# if successful, the money in the pot will double, and the player's money will remain constant
prob_tree[iteration].append([case2_player_funds, case2_win_new_pot])
# if not successful, the money in the pot will be added into the player's money, and the pot will return to 0
prob_tree[iteration].append([case2_player_funds + case2_old_pot, case2_lose_new_pot])
# elif there is no money in the pot but there is sufficient money in the player's pocket to play again1
elif possibility[0] >= price_to_play and possibility[1] == 0:
# first, the money in the player's pocket goes down by the amount price_to_play, and the pot amount goes up to one.
case3_funds_after_buyin = possibility[0] - price_to_play
case3_pot_after_buyin = 1
case3_pot_if_successful = 2
case3_pot_if_unsuccessful = 0
case3_funds_if_successful = case3_funds_after_buyin
case3_funds_if_unsuccessful = case3_funds_after_buyin + case3_pot_after_buyin
# then, either the player gets back the pot and it goes to 0
prob_tree[iteration].append([case3_funds_if_unsuccessful, case3_pot_if_unsuccessful])
prob_tree[iteration].append([case3_funds_if_successful, case3_pot_if_successful])
counter = 0
for outcome in prob_tree[iterations]:
if outcome[0] < price_to_play and outcome[1] == 0:
counter += 1
print float(counter)/(2**iterations)
play_game(2, 2, 25)