如何使用剔除映射克隆和重新绑定我的数据?
我需要将 ViewModel 中主源中的一些数据克隆到对话框中。原因是用户可以取消对话框,并且我不希望主机反映那些取消的更改。
我在对话框中创建主数据的克隆副本,并将数据绑定设置为获取“localEdited.*”属性。如果用户单击“确定”,我会尝试将数据保存回主数据库(如果已编辑),否则推送数据(如果是新数据)。
editItem: function(data) {
// clone a temporary copy for the dialog
this.localEdited = ko.mapping.fromJS(ko.toJS(data));
$("#dlgAdd").dialog("open");
},
上述内容当前有效,但是如果我单击母版中的另一个项目,对话框不会显示更新的值。就好像 ko.mapping.fromJS 只工作一次,然后就不再工作了。它总是选取第一个值。我该如何解决这个问题?我觉得我需要重新绑定价值观,但 KO 的重点是不必这样做。
我如何将数据保留回父级。我想我可能也有和上面一样的问题。
顺便说一句,我正在使用 KnockoutJS 1.2.1。
I need to clone some data from a master source in the ViewModel into a dialog. The reason being is that the user could cancel the dialog, and I don't want the master to reflect those cancelled changes.
I create a cloned copy of the master data into the dialog, and the data-bindings are set to pick up 'localEdited.*' properites. If the user clicks ok, I then try and persist the data back to the master if its edited, otherwise push the data if its new data.
editItem: function(data) {
// clone a temporary copy for the dialog
this.localEdited = ko.mapping.fromJS(ko.toJS(data));
$("#dlgAdd").dialog("open");
},
The above currently works, however if I click another item in the master, the dialog isn't showing the updated values. Its as if the ko.mapping.fromJS only works once and then never again. It always picks up the first values. How do I get around this problem? I feel like I need to re-bind the values, but the whole point of KO is to not have to do this.
How do I also persist the data back to the parent. I guess I might have the same problem as above.
BTW, I'm using KnockoutJS 1.2.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
localEdited 需要是一个可观察的,您应该在
editItem
函数中执行this.localEdited(ko.mapping.fromJS(ko.toJS(data)))
以使淘汰赛重新绑定您的每次调用 editItem 时都会出现对话框。对话框中的数据绑定需要更改为 localEdited()。*您可以在
localEdited
不被观察的情况下离开,但在这种情况下,您需要手动更新localEdited 的每个可观察属性
在editItem
函数中。所以你必须有类似的东西,要点是你需要使用函数调用语法来更新可观察量。这样,淘汰赛将捕获更新并通过绑定传播它。在最初的代码中,您只需对
localEdited
进行简单的赋值,因此淘汰赛没有钩子来拦截并发挥其魔力。更新:
事实证明 ko.mapping 插件可以自动更新模型中的各个可观察量,因此您不必手动执行此操作:
localEdited needs to be an observable and you should do
this.localEdited(ko.mapping.fromJS(ko.toJS(data)))
ineditItem
function to make knockout rebind your dialog every time editItem is called. Data bindings in the dialog will need to be changed to localEdited().*You can get away without
localEdited
being observable, but in this case you will need to manually update each observable property oflocalEdited
ineditItem
function. So you would have to have something likeThe point is you need to update observables using function call syntax. This way knockout will catch the update and propagate it through the bindings. In you initial code you just do simple assignment to
localEdited
, so knockout doesn't have a hook to intercept and do its magic.UPDATE:
It turns out ko.mapping plugin can update individual observables in your model automatically, so you don't have to do it manually: