我在一个网页上建立功能,用户可以执行多次。通过用户的操作,创建了一个对象/模型,并使用 ko.applicyBindings ()将其应用到 HTML 中。
数据绑定的 HTML 是通过 jQuery 模板创建的。
目前为止还不错。
当我通过创建第二个对象/模型并调用 ko.applicyBindings ()来重复这个步骤时,我遇到了两个问题:
为了解决这个问题,在第一次通过之后,我调用 jQuery。Void ()删除包含所有数据绑定属性的模板化 HTML,这样它就不再出现在 DOM 中。当用户启动第二次传递的过程时,数据绑定的 HTML 被重新添加到 DOM 中。
But like I said, when the HTML is re-added to the DOM and re-bound to the new object/model, it still includes data from the the first object/model, and I still get the JS error which doesn't occur during the first pass.
结论似乎是 Knokout 保留了这些绑定属性,即使从 DOM 中删除了标记。
所以我要寻找的是一种方法,去除敲除这些绑定属性,告诉敲除,不再有一个可观察的模型。有办法吗?
剪辑
The basic process is that the user uploads a file; the server then responds with a JSON object, the data-bound HTML is added to the DOM, then the JSON object model is bound to this HTML using
mn.AccountCreationModel = new AccountViewModel(jsonData.Account);
ko.applyBindings(mn.AccountCreationModel);
Once the user has made some selections on the model, the same object is posted back to the server, the data-bound HTML is removed from then DOM, and I then have the following JS
mn.AccountCreationModel = null;
当用户希望再次执行此操作时,将重复所有这些步骤。
恐怕代码太复杂了,不能做 jsFiddle 演示。