Saturday 15 September 2012

javascript - Backbone collection and models not disposed -



javascript - Backbone collection and models not disposed -

any compassionate geeky human being out there in wilderness, please help me.

i developing simple app using backbone 1.1.2.

one of (dumb) views in charge of displaying collection of items no server-side interaction.

as matter of fact, collection created bootstrapped info so:

the bootstrapped data:

class="lang-js prettyprint-override">var bootsrappedtestitems = function() {return json.parse('[ {"itemid" : 1, "item" : "item 1"}, {"itemid" : 2, "item" : "item 2"}, {"itemid" : 3, "item" : "item 3"}]')}

the model:

class="lang-js prettyprint-override">var testitem = backbone.model.extend ({ idattribute: 'itemid' });

the collection:

class="lang-js prettyprint-override">var testitems = backbone.collection.extend({ model: testitem });

the creation of view (via router):

class="lang-js prettyprint-override">data = new testitems(bootsrappedtestitems()) currentview = new view(data)

now, trying accomplish next behaviors:

when view beingness rendered first time, create collection bootstrapped data; when view beingness closed, dispose collection , models; when view beingness rendered afresh, re-create collection bootstrapped data.

regarding point 1., works expected.

regarding point 2., consider view beingness closed via custom method performs next operations:

class="lang-js prettyprint-override">this.stoplistening() if (this.collection) { this.collection.off(null, null, this) this.collection.reset() } this.remove()

i did verify view correctly beingness closed (no zombie issue).

i must add together tried destroy each model, (with no surprise) backbone spitted out error in console, model definition hasn't go valid url property. tried set models' ids , itemids null in order fool backbone around, must sadly confess backbone seems way smarter me.

regarding point 3., expecting have 1 collection models @ given point, no matter how many times view beingness re-rendered.

contrary expectations, verified closed , re-rendered view several times, no collection beingness disposed ended baffling (not appalling) scenario:

old collections empty persisted in memory; there duplicate models: of them belonged lastly created collection, vast bulk seemed orphans, pointing old (and empty) collections.

now, reason (inexplicable me), old collections , duplicate models persisted stubbornly in memory because entity (obscure me) still referencing them, preventing javascript engine doing nice, proper , cathartic garbage collect.

so - compassionate geeky human being - inquire you:

what doing wrong theoretical perspective? how can track (via chrome or firefox developer tools) obnoxious entity referencing old collections , duplicate models? can enlighten me other piece of info worth considering in nowadays piteous case?

many :-)

i guess you'd tried phone call destroy method on each model. wrong way in case because don't enable/use syncing server. destroy method intended delete model server, not delete model itself. cannot latter in javascript (see here details). can delete reference model, e.g. assigning null.

javascript backbone.js

No comments:

Post a Comment