Comments (8)
@jankapunkt @zodern thoughts?
from blaze.
I can take a look at it tonight
from blaze.
@michaelcbrook I tested this with 2.6.1-rc and if I change the code to your suggestions the views will not be removed anymore from the DOM and remain drawn on the screen.
Can you please give more context, why you need the view to remain attached to the DOM during onDestroyed
callback?
from blaze.
@jankapunkt There are many times I use this, but one recent example I can give is when I initialized a jQuery plugin on a text input to let it autogrow when the text within it changes. The way the plugin works is it creates a hidden element at the end of the body mocking the current input, measures the width, and then updates the width of the original input to fit the content. To prevent memory leaks (from these hidden elements being added to the DOM), it requires that a destroy function is called when we don't care about it any longer. We call that in the onDestroyed
callback.
Here's some actual code:
import inputAutogrow from 'input-autogrow';
Template.example.onRendered(function exampleOnRendered() {
// initialize autogrow
this.$('input').inputAutogrow({ maxWidth: 800, minWidth: 220 });
});
Template.example.onDestroyed(function exampleOnDestroyed() {
// destroy autogrow
this.$('input').inputAutogrow("destroy");
});
This caused the following error to be thrown and the hidden element was not removed from the DOM.
Error: Can't select in removed DomRange
at Blaze._DOMRange.DOMRange.$ (http://localhost:3000/packages/blaze.js?hash=981d8eb98840b94fa27e85bfc8334660676125f3:654:26)
at Blaze.TemplateInstance.$ (http://localhost:3000/packages/blaze.js?hash=981d8eb98840b94fa27e85bfc8334660676125f3:3450:25)
at Blaze.TemplateInstance.exampleOnDestroyed (/imports/ui/layouts/example/components/example.js:197:10)
at http://localhost:3000/packages/blaze.js?hash=981d8eb98840b94fa27e85bfc8334660676125f3:3279:20
at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?hash=981d8eb98840b94fa27e85bfc8334660676125f3:3662:14)
at fireCallbacks (http://localhost:3000/packages/blaze.js?hash=981d8eb98840b94fa27e85bfc8334660676125f3:3275:12)
at Blaze.View.<anonymous> (http://localhost:3000/packages/blaze.js?hash=981d8eb98840b94fa27e85bfc8334660676125f3:3382:5)
at fireCallbacks (http://localhost:3000/packages/blaze.js?hash=981d8eb98840b94fa27e85bfc8334660676125f3:1874:75)
at Object.Tracker.nonreactive (http://localhost:3000/packages/tracker.js?hash=5ef67b97eaf2ca907dc38459283f2349bada6814:625:12)
at http://localhost:3000/packages/blaze.js?hash=981d8eb98840b94fa27e85bfc8334660676125f3:1871:13 undefined
With some monkey patching though, I was able to get it to work by overriding Blaze.remove
and just switching the destroy()
and detach()
calls myself:
Blaze.remove = function (view) {
if (! (view && (view._domrange instanceof Blaze._DOMRange)))
throw new Error("Expected template rendered with Blaze.render");
while (view) {
if (! view.isDestroyed) {
var range = view._domrange;
range.destroy(); // THIS WAS ORIGINALLY AFTER THE IF STATEMENT BELOW (BUT NOT INSIDE IT)
if (range.attached && ! range.parentRange)
range.detach();
}
view = view._hasGeneratedParent && view.parentView;
}
};
I think keeping the DOM attached during onDestroyed
is warranted because that seems to be the intended functionality and the way that it works everywhere else.
Having said that, I tried several other solutions, like the one used inside flow-router-extra, but this created the same problem you mentioned of the DOM remaining drawn on the screen.
const _BlazeRemove = function (view) {
try {
Blaze.remove(view);
} catch (_e) {
try {
Blaze._destroyView(view);
view._domrange.destroy();
} catch (__e) {
view._domrange.destroy();
}
}
};
But this did not work for me, and I wonder if this was written in response to the Blaze.remove
function not working correctly in the first place.
from blaze.
Well, that's a problem now. I just changed the order in #366 because this caused lots of DOMRange not attached
errors. Now I think this issue is connected to all this. Do you already use the 2.6.1-rc.1
release? If not, please try it out if it fixes your issue.
Otherwise I think we have to make another round of debugging to find the source of the issue.
from blaze.
@michaelcbrook please see #374
Actually your propsed solution works, what was problematic was that I also use flow-router-extra which made my drawing faulty but with a clean project this works. In turn I will have to make a PR to the router to fix this.
from blaze.
I just tried running 2.6.1-rc.1
just to check, and without the fix I posted, the problem still exists. I also tested the fix on 2.6.1-rc.1
and it worked correctly. So I'm in support of PR #374 still being pushed through.
from blaze.
I'm closing this as it's covered by release 2.6.1. Feel free to reopen when the issue persists in 2.6.1
from blaze.
Related Issues (20)
- blaze is missing `jquery` in its dependency list. HOT 6
- Handlebars syntax is not bad at all, we can make it faster than React, Vue, or Svelte HOT 5
- small issue with parsing html comments inside Blaze templates HOT 3
- ReferenceError: SpacebarsCompiler is not defined HOT 1
- Handle async code HOT 20
- Help developers by indicating undefined data in debug mode
- Move codebase to ES6 HOT 15
- spacebars-tests packages still uses removed code
- Error handling, callbacks and DOMRange HOT 9
- Errors in onCreated callback cause a complete stop for rendering further Templates
- Bootstrap select picker is not correctly removed / disposed during Template descruction HOT 6
- Add benchmarks to tests
- SSR is broken in 2.6.0 HOT 4
- Add ts types HOT 3
- Blaze compile errors completely silent if imported HOT 12
- Be able to have more contentBlock in template HOT 1
- Non-primitives not fully reactive in 2.7
- Complete GitHub community standards HOT 7
- CI/CD for documentaiton
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blaze.