我如何将我的测验的所有选项设置为活动:false点击
How do I set all options of my quiz to active: false on click?
我正在尝试构建一个带有多项选择选项的问题(最终是一个测验)。当用户单击某个选项时,活动类被设置为true,并为该选项插入红色背景。我想帮助的是,如果用户决定点击另一个选项。之前的活动选项应该更改为active: false,新选项应该更改为active: true。这是我的第一个Ember.js应用程序,所以如果有更好的方法,我洗耳恭听。
var IndexRoute = Ember.Route.extend({
model: function() {
return Ember.A([
{
question: "What's up?",
options: [
{
text: "option a",
active: false
},
{
text: "option b",
active: false
}
]
},
{
question: "How many?",
options: [
"one",
"two"
]
}
]);
}
});
index.hbs
{{#each model}}
{{quiz-question section=this}}
{{/each}}
quiz-question.hbs
{{#with section}}
{{question}}
{{#each options}}
{{quiz-option active=active text=text}}
{{/each}}
{{/with}}
{{yield}}
quiz-option.hbs
<div {{bind-attr class="active"}}>{{text}}</div>
{{yield}}
quiz-option.js
export default Ember.Component.extend({
click: function () {
console.log('asdf', this.get('text'));
this.set('active', true);
}
});
app.css
html, body {
margin: 20px;
}
.active {
background-color: red;
}
您可以使用sendAction
将操作传递给父级,在父级中它可以迭代集合并将所有其他项设置为false。
App.QuizOptionComponent = Ember.Component.extend({
click: function () {
this.set('active', true);
this.sendAction('killFriendsExcept', this.get('text'));
}
});
{quiz-option active=active text=text killFriendsExcept='resetChildrenExcept'}}
App.QuizQuestionComponent = Ember.Component.extend({
actions: {
resetChildrenExcept: function(except){
this.get('section.options').forEach(function(item){
if(Em.get(item, 'text')!=except){
Em.set(item, 'active', false);
}
});
}
}
});
http://emberjs.jsbin.com/jitezago/1/edit 相关文章:
- 设置选项对象关键点:值对以实现带第二个y轴的动态打印
- Grunt为任务设置选项,并使用新值运行该任务
- 无法在 IE 中设置选项元素的属性
- 使用设置选项时未引用高图表错误
- 使用多维数组使用 ng-option 设置选项的值和文本
- 如何设置选项选择的值
- 在 React-Leaflet 库中为
- 如何使用 AngularJS 从控制器
的 中设置选项值 - 基于数据属性的选择下拉列表中的设置选项
- 如何在 JSHint 中设置选项列表
- SenchaTouch2 - 侦听商店加载事件,设置选项卡面板项“徽章文本”
- 根据 if / else 语句修改设置“选项”值
- 如何设置选项以选择框
- 在角度模态打开后设置选项
- 使用 jQuery 设置选项值以外的默认值
- 在 jade 筛选器中使用区域设置选项失败
- xml2js:无法在 parseString() 函数中设置选项
- 设置选项接受一个数字变量,拒绝另一个数字变量
- jQuery UI选项卡,设置选项卡被动和主动
- 谷歌地图多边形在设置选项后不更新颜色