目前使用 Ember.js 1.0.0.rc6.4
我有一个新事件的 View ,它呈现一个文本区域(description
)和一个复选框(isQuestion
)。如果在描述中插入 ?
,则该复选框会自动选中。效果很好,直到用户单击复选框,此时绑定(bind)丢失,这很好,但提交表单后我需要重新分配它。这是一些代码,我希望它是干净的,感谢您的兴趣。抱歉,如果我洒了一些咖啡。
App.ActivityFormView = Em.View.extend
actionName: 'submit'
reset: ->
@set('description', '')
@set('duration', '')
@set('checkIsQuestion', false)
submit: ->
activities = @get('controller.model')
activities.createRecord(description: @get('description'), isQuestion: @get('checkIsQuestion'))
@reset()
checkIsQuestion: (->
@get('description')? && @get('description').match(/\?/)?
).property('description')
这是模板
<label>
Add your activity here:
{{textarea value=view.description}}
</label>
<label>
Mark as question:
{{input checked=view.checkIsQuestion type='checkbox'}}
</label>
<button type='submit'>Save</button>
我尝试在 reset
方法中使用绑定(bind),但我认为我需要在单独的函数中提取匹配逻辑,并使用属性或绑定(bind)重新分配它,但我不知道如何.
欢迎任何帮助,请随时对整个解决方案发表评论。谢谢
最佳答案
我想为了使绑定(bind)和计算属性保持完整,您应该区分您的计算属性,如果它是 set
或 get
并且行为不同,请将您的代码修改为这个:
...
checkIsQuestion: function(key, value) {
// getter
if (arguments.length === 1) {
return (this.get('description') != null) && (this.get('description').match(/\?/) != null);
// setter
} else {
return value;
}
}.property('description')
...
执行此操作后,绑定(bind)应保持完整。另请参阅此处的示例 jsbin 。我希望它具有您正在寻找的正确行为。抱歉,“javascript化代码”:)
希望有帮助。
关于binding - Ember : re-bind a function to a view property (checkbox observing text area),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18054793/