Smart-Table icon indicating copy to clipboard operation
Smart-Table copied to clipboard

LocalStorage tableState example has a bug

Open JanStevens opened this issue 9 years ago • 1 comments

Hello,

Using a variant of the local storage example I found that there was a subtile hidden bug when using a custom pipe function. When the page loads, the custom pipe function is executed two times, one time on initialise and a second time when the localStorage is loaded. For small website this doesn't really matter but we have some heavy queries we don't want to run two times right after each other.

The following uses a pre and post link functions so we don't call the pipe function two times, I also use the localStorageService plugin but any other plugin or manual storage management is also possible of course.

@directives.directive 'stPersist', (localStorageService) ->
  require: '^stTable'
  link:
      pre: (scope, element, attr, ctrl) ->
        if localStorageService.get(attr.stPersist)
          savedState = localStorageService.get(attr.stPersist)
          tableState = ctrl.tableState()
          tableState.pagination = savedState.pagination
          tableState.sort = savedState.sort
          tableState.search = savedState.search

      post: (scope, element, attr, ctrl) ->
        scope.$watch((-> ctrl.tableState()), (newVal, oldVal) ->
          if newVal != oldVal
            localStorageService.set(attr.stPersist, newVal)
        , true)

Notice that I don't need to call ctrl.pipe() since I use the pre link function.

JanStevens avatar Mar 04 '15 21:03 JanStevens

you are right, there is a discussion about the matter in https://github.com/lorenzofox3/Smart-Table/issues/242 But yeah I might switch the persist example with yours, it seems more robust, Thanks a lot !

lorenzofox3 avatar Mar 05 '15 02:03 lorenzofox3