I'm using the polymer application drawer template from the polymer cli.
I'm having some trouble with:
- When you load a new page, the html element is imported; then it's code executes
- When I move to another page the code for the previous page is still running.
Is there a way to destroy and create the page/element or suspend and enable?
Whats the best practice for dealing with this problem?
Have the pages implement a create and destroy method and invoke it when changing page?
Ie
oldPageElement.destroy();
newPageElement.create();
Polymer({
is: 'my-random-page',
behaviors: [MyBehaviors.CommonPageBehavior],
/**
* @override
*/
create: function() {..}
/**
* @override
*/
destroy: function() {..}
})
You actually don't need to implement anything complicated, but just use a mere
dom-if
.Working prototype: http://jsbin.com/gezihatera/edit?html,console,output
As you can see, the "View One" uses a custom page element, which is always restamped when re-selected. Other pages are ordinary
div
elements, since this is only a minimal prototype. But this also shows that you can selectively choose which pages get restamped and which do not (if you don't always need this).The essence is the following: as per dom-if documentation, if you set the
restamp
attribute totrue
, then thedom-if
will always create and destroy your pages upon selecting/deselecting them. You can see this in the console, where I print outsample-page ready
on everyready
element. I also create a helper function_equals
to help with comparing whether the specified page is really selected.To sum up, let me paste the code for the app:
And the code for the sample page:
Hope this satisfies your question.
Note: you should not put the
name
attribute on thedom-if
itself, but rather onto its content (the same way I did).Thought I would post my solution after implementing @alesc's dom-if to get the element to be deactivated.