资源清理和内存管理
可选择地,你的viewmodel类可以有一个dispose函数,假如实现,Knockout将调用这个函数,无论什么时候该组件被销毁(例如,因为响应的项被从foreach中除去,或者if绑定变成false)
你必须使用dispose 来除去任何不是被内在的垃圾可收集的资源。例如:
var someExternalObservable = ko.observable(123); function SomeComponentViewModel() { this.myComputed = ko.computed(function() { return someExternalObservable() + 1; }, this); this.myPureComputed = ko.pureComputed(function() { return someExternalObservable() + 2; }, this); this.mySubscription = someExternalObservable.subscribe(function(val) { console.log('The external observable changed to ' + val); }, this); this.myIntervalHandle = window.setInterval(function() { console.log('Another second passed, and the component is still alive.'); }, 1000); } SomeComponentViewModel.prototype.dispose = function() { this.myComputed.dispose(); this.mySubscription.dispose(); window.clearInterval(this.myIntervalHandle); // this.myPureComputed doesn't need to be manually disposed. } ko.components.register('your-component-name', { viewModel: SomeComponentViewModel, template: 'some template' });
仅仅依赖同一视图模型对象的computed和subscription不是严格需要销毁的,因为这仅仅创建了一个Javascript垃圾收集器知道如何释放的循环参考。然而,为了避免必须记得哪些东西需要清理,只要有可能,你最好使用pureComputed,不管是不是在技术尚必要,都显式地销毁所有其他computeds/subscriptions。
原文:
Optionally, your viewmodel class may have a dispose
function. If implemented, Knockout will call this whenever the component is being torn down and removed from the DOM (e.g., because the corresponding item was removed from aforeach
,
or an if
binding has become false
).
You must use dispose
to release any resources that aren’t inherently garbage-collectable. For example:
setInterval
callbacks will continue to fire until explicitly cleared.clearInterval(handle)
to stop them, otherwise your viewmodel might be held in memory.ko.computed
properties continue to receive notifications from their dependencies until explicitly disposed.
.dispose()
on the computed property, otherwise it (and possibly also your viewmodel) will be held in memory. Alternatively, consider using apure
computed to avoid the need for manual disposal..dispose()
on the subscription, otherwise the callback (and possibly also your viewmodel) will be held in memory.createViewModel
function (or even inside a regular component viewmodel, although to fit the MVVM pattern you shouldn’t) must be removed.
For example:
var
someExternalObservable = ko.observable(123); function
SomeComponentViewModel() { this .myComputed = ko.computed( function () { return someExternalObservable() + 1; }, this ); this .myPureComputed = ko.pureComputed( function () { return someExternalObservable() + 2; }, this ); this .mySubscription = someExternalObservable.subscribe( function (val)
{ console.log( ‘The external observable changed to ‘ + val); }, this ); this .myIntervalHandle = window.setInterval( function () { console.log( ‘Another second passed, and the component is still alive.‘ ); }, 1000); } SomeComponentViewModel.prototype.dispose = function () { this .myComputed.dispose(); this .mySubscription.dispose(); window.clearInterval( this .myIntervalHandle); // this.myPureComputed doesn‘t need to be manually disposed. } ko.components.register(‘your-component-name ‘, { viewModel: SomeComponentViewModel, template: ‘ some template‘ }); |
It isn’t strictly necessary to dispose computeds and subscriptions that only depend on properties of the same viewmodel object, since this creates only a circular reference which JavaScript garbage collectors know how to release. However, to avoid having
to remember which things need disposal, you may prefer to use pureComputed
wherever possible, and explicitly dispose all other computeds/subscriptions whether technically necessary or not.
原文地址:http://blog.csdn.net/hongweigg/article/details/43968865