-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added update method #120
base: master
Are you sure you want to change the base?
added update method #120
Conversation
Hello @RickHolmes, thanks for your proposal :-) Can you explain a bit the internals of I saw you renamed some internal properties (which are public, so maybe people rely on them): is there any specific reason to do so? Thank you :-) |
In this particular use case for the Imager -- a single-page-application and adding the page content
dynamically. So I needed either an add() method or an update() method, since there's nothing there for the Imager to do when the page loads. From that page (a "gallery" page), however, a couple things may happen: So I added an update() method that deletes the element list and rebuilds In order to remove an event with either the W3C or old IE method, you I'm actually using another version of the Imager based on your Oct. 26, s with background-images using the same Imager instance. I'll upload it if you're interested. On 1/13/2015 4:50 AM, Thomas Parisot wrote:
|
OK gotcha. Detached DOM Nodes can definitely be a pain. I have not looked yet but do you know if there is a way to detect if a given I would rather prefer a It would make the |
You can find detached nodes the hard way by comparing this.divs against a list of the divs in the current DOM with this.className. Then you can remove those that currently don't exist from this.divs. I'm not sure there's an easier way; other methods I've found with a quick search check the node's parents or its html or something. And I'm not sure that would be any easier/less expensive than a direct comparison of the lists. Edit: I take that back. In order to compare the lists, you'd need to compare the data-src attributes, which might be harder, since you'd need to build an array of the data-src's of this.divs, then compare the current. |
Well thinking about it, the thing is if you replace the actual |
True, that's why I did that. But the way I implemented things, I had to reinitialize the Imager, which attached additional (duplicate) events on "window". If things can be done without the init() step, all would be good. Adding additional options probably isn't necessary, and deleting events would only make sense if you set scroll and resize events on DOM elements, not the window itself. |
Can you share a snippet of code which demonstrates the initial setup of Imager, and how you reinitialise it? I wonder why would such a thing is needed (if we can design a cleaner path let's do it). |
All the js is being loaded by require.js and Backbone.Marionette is being used to create the page. In my "data-main" file, I create a Marionette.Application and set the Imager as a property:
This gives me a single instance of the Imager that I call from various places as needed. For instance, in the main page view (the "gallery") I set a listener for "dom:refresh" and simply call
Then, should a gallery item be clicked, I will rerun App.Imager.update() in a dom-refresh listener on the view for either the modal or the single-page (which is only shown for small screens). Changing the modal (or going "back" for small screens) runs App.Imager.update() again. It turns out the this.init() in the update() method is only needed if lazy loading is desired. |
OK I see. So I would suggest to not care about the possible detached elements in the Calling Imager.prototype.scrollCheck = function (force) {
// ...
if (Boolean(force) || this.scrolled) {
// ...
}
};
Imager.prototype.update = function(elementsOrSelector) {
this.divs = [];
this.add(elementsOrSelector);
this.lazyload && this.scrollCheck(true);
} Something like that. It is not generally a good practice to call a constructor/init function more than once – it induces side effects we are not generally looking forward to solve :-) |
Do you mean not care about the detached elements or not care about the added events? There could be dozens of detached elements, since each modal would have at least two to as many as seven elements handled by the Imager. This would add up quickly. I totally agree about not calling init() more than once. I'll see if I can "reset" the lazy load another way. Edit: I tried your suggestion and it seems to work fine. |
Well, Tell me if I am wrong – otherwise let me know why and how detached DOM nodes could still exist if we empty out the |
No, you're right, I missed that line and was thinking that add() just added the new. Sorry. |
@RickHolmes it's alright :-) if you could reflect the changes with the suggested direction, and confirm it works well within your single page application, I'd be happy to merge the change and to release a new version of Imager. |
Will do, probably a bit later today. |
Well, you are changing the codestyle of the project – I would not recommend to do that. I'd prefer you to rever to the previous Thanks :-) |
This branch adds an update() method that resets the entire elements list and detaches any previously attached events, hopefully limiting memory leaks. One use case is with the creation/destruction of a modal popup or slider when the entire thing is created dynamically, not just displayed with existing elements.
I also added the ability to pass additional options at the same time.