剔除将视图模型重置为原始数据 [英] knockout reset viewmodel to original data

查看:81
本文介绍了剔除将视图模型重置为原始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将敲除视图模型重置回原始数据的最佳方法是什么?

what is the best way to reset knockout viewmodel back to the original data?

如果原始数据json没有更改,对可观察对象进行一些更改后,如何将其重新设置?就像刷新页面一样.

if the original data json is not changed, after I do some changed on the observable, how can I set it back? just like refresh the page.

推荐答案

我认为刷新" viewModel是不好的做法.您可以像这样刷新它:

I think it is bad practice to "refresh" your viewModel. You could refresh it like this:

ko.cleanNode(document.getElementById("element-id"));
ko.applyBindings(yourViewModel, document.getElementById("element-id"));

但是我认为在视图模型上有一个名为重置"的方法会更干净,该方法会将您的观测值设置回初始状态.也许是这样的:

But I think it is cleaner to have a method on your view model called "reset", that sets your observables back to initial states. Maybe like this:

function MyViewModel() {
  this.something = ko.observable("default value");
  this.somethingElse = ko.observable(0):

  this.reset = function() {
    this.something("default value");
    this.somethingElse(0);
  }
}

这篇关于剔除将视图模型重置为原始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆