[英]Emberjs Checkbox Save to Database
I have a checkbox that needs to save to the database as soon as the value is changed. 我有一个复选框,需要在值更改后立即保存到数据库。 It is bound to the data element audit.
它绑定到数据元素审核。 Here is the checkbox in the template:
这是模板中的复选框:
{{view Site.ReleaseChecklistToggleView checkedBinding="audit" contentBinding="this" placeholder="#"}}
Here is what the Site.ReleaseChecklistToggleView
is defined as: 以下是
Site.ReleaseChecklistToggleView
的定义:
Site.ReleaseChecklistToggleView = Em.Checkbox.extend(
change: (e)->
this.content.save()
)
It appears that audit is not being set before change is called. 似乎在调用更改之前未设置审核。 It passes the
audit
variable as null
to the database, but if I do console.log(this.content.audit)
in the change
method, then it tells me the correct value of true
or false
. 它将
audit
变量作为null
传递给数据库,但是如果我在change
方法中执行console.log(this.content.audit)
,那么它将告诉我true
或false
的正确值。 My only conclusion is that it is not updating the ember model until after the change method is being run. 我唯一的结论是,直到运行change方法之后,它才更新ember模型。
Any advice? 有什么建议吗? Am I using the wrong method?
我使用了错误的方法吗?
Try the following: 请尝试以下操作:
Site.ReleaseChecklistToggleView = Em.Checkbox.extend(
valueChanged: ->
@get("content").save()
.observes("checked"))
Checkout the Ember guide for the Todo application ( http://emberjs.com/guides/getting-started/marking-a-model-as-complete-incomplete/ ). 查阅Todo应用程序的Ember指南( http://emberjs.com/guides/getting-started/marking-a-model-as-complete-incomplete/ )。 I was able to get auto-saving of a specific attribute working through a checkbox by following this.
通过执行此操作,我可以通过复选框自动保存特定属性。
In case that link is no longer active, this is how they did it... 万一该链接不再活动,这就是他们的操作方式...
Handlebars 把手
<!--- ... additional lines truncated for brevity ... -->
{{#each itemController="todo"}}
<li {{bind-attr class="isCompleted:completed"}}>
{{input type="checkbox" checked=isCompleted class="toggle"}}
<label>{{title}}</label><button class="destroy"></button>
</li>
{{/each}}
<!--- ... additional lines truncated for brevity ... -->
Controller 控制者
Todos.TodoController = Ember.ObjectController.extend({
isCompleted: function(key, value){
var model = this.get('model');
if (value === undefined) {
// property being used as a getter
return model.get('isCompleted');
} else {
// property being used as a setter
model.set('isCompleted', value);
model.save();
return value;
}
}.property('model.isCompleted')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.