选定的对象图案与淘汰赛 pt 2
这个问题涉及到 带剔除功能的选定对象模式 感谢答案,我可以通过绑定设置所选项目。
但是,我该如何在代码中设置它呢?
例如,我想创建一个新对象并将其设置为模型中的选定对象。
通常情况下这样就可以了。 1)创建对象。 2) 设置对象默认值。 3)Model.selected_object = objectJustCreated
//model
item_selected = ko.observable();
//code
var new_item = app.createItem(defaults);
model.item_selected = new_item;
我正在尝试切换模型中的可观察对象,但我的绑定没有反映更改。我是否必须循环遍历这些值并以这种方式更新所选对象?
非常感谢任何帮助。
This question relates to
selected object pattern with knockout
Thanks to the answer, I am able to set the selected item via bindings.
However, how would I go about setting it in code?
For example, I want to create a new object and set that as the selected object in the model.
Normally this would go.
1) Create Object.
2) set object defaults.
3) Model.selected_object = objectJustCreated
//model
item_selected = ko.observable();
//code
var new_item = app.createItem(defaults);
model.item_selected = new_item;
I'm trying this to switch out the observable in the model but my binding are not reflecting the changes. Will I have to loop through the values and update the selected object that way?
Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您设置可观察量的值时,您需要将其作为第一个参数传递给可观察量(这是一个函数)。
所以,你会想要这样做:
这是一个基于上一个问题的示例: http://jsfiddle.net/ rniemeyer/ST8gt/
When you set the value of an observable you need to pass it as the first argument to the observable (which is a function).
So, you would want to do:
Here is a sample based on the previous question: http://jsfiddle.net/rniemeyer/ST8gt/