sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

overridden dispose() method triggered on page edit

Open jaredbuttars opened this issue 10 years ago • 11 comments

This is a bug I'm getting with recent SPFx updates

Category

  • [ ] Question
  • [ ] Typo
  • [x] Bug
  • [ ] Enhancement / Suggestion
  • [ ] Additional article idea

Expected or Desired Behavior

Override the dispose() method to run code when a web part is deleted. For example:

  protected dispose(): void {
    // cleanup code when web part is deleted
  } 

Observed Behavior

A recent update to SPFx calls the overridden dispose() method when the edit page button is clicked on modern pages.

jaredbuttars avatar Nov 27 '16 18:11 jaredbuttars

Thanx for the info. Will review and get back.

manishgarg1 avatar Nov 27 '16 21:11 manishgarg1

@manishgarg1 I can see in the source comments that the dispose() API will be deprecated in favor of an API named onDispose(). Also in the comments it is noted that the onDispose API "is expected to be called in scenarios like page navigation i.e. the host is transitioning from one page to another and disposes the page that is being transitioned out." That wasn't the behavior in Drop 5. In drop 5 dispose() was called when the web part was DELETED from a page. Perhaps there is need for an onDelete() or onRemove() API that is called when a web part is removed?

jaredbuttars avatar Nov 29 '16 23:11 jaredbuttars

@manishgarg1 There is still no way to intercept a web part delete event. We need this to cleanup file system resources when a web part is deleted.

jaredbuttars avatar Feb 11 '17 17:02 jaredbuttars

@jaredbuttars could you tell more what file system resources you would like to clean up on web part deletion?

waldekmastykarz avatar Feb 18 '17 18:02 waldekmastykarz

@waldekmastykarz several of my web parts allow users to upload files to the site. The files themselves are for specific use by a single instance of a web part on a page. It is logical in this use case that if the web part instance was deleted from a page all of the files uploaded through it would also be deleted. At present I can't intercept that delete from page event. If I could through some sort of onDelete override then I could easily write the bits to cleanup files associated with that web part instance.

jaredbuttars avatar Feb 24 '17 14:02 jaredbuttars

The challenging part is that cleaning up resources might take a while and it's definitely not something you should want to do in browser. Having something like a webhook would make more sense, but as you've noticed there is currently no onDelete event. Have you tried looking into the web part/web part manager API to see if there are no events that you could subscribe to?

waldekmastykarz avatar Feb 25 '17 13:02 waldekmastykarz

@jaredbuttars, there are two types of delete events:

  1. Page navigation: when the page tells the web part manager to cleanup all the current web part related context. This is the case we have addressed with the onDispose() API. In this case you may have DOM handles and other client side objects that you may want to clean up. This is primarily to avoid client side memory leaks.

  2. Deletion of a web part from a page: we discussed a lot for an API for this scenario. Multiple potentially complicated scenarios came up. Modern pages are stored as list items. What would you do when the page is deleted directly from the list? The web part data would be left hanging. What if you delete the web part from the page data through an API? And so on... The only scenario we can cover at this point is the delete button on the web part toolbar. Would you want an API just for that one scenario and is it ok if all other scenarios are left out?

manishgarg1 avatar Feb 27 '17 04:02 manishgarg1

@manishgarg1

I can see how it would become complicated to cover all delete scenarios. Even still, an API just for the one scenario when a web part is deleted from a page would be extremely useful.

jaredbuttars avatar Feb 27 '17 10:02 jaredbuttars

Added this to User Voice.

https://sharepoint.uservoice.com/forums/329220-sharepoint-dev-platform/suggestions/19261525-ondelete-method

jaredbuttars avatar May 12 '17 18:05 jaredbuttars

Covered by user voice. Closing this entry.

patmill avatar Oct 24 '17 17:10 patmill

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues

msft-github-bot avatar Jan 30 '20 00:01 msft-github-bot