IsLoaded for ExtJS Data Warehouse

This is not a question, but maybe I can help others with this code.

Since the ExtJS repository is not something like isLoaded (), I override it. But I need time to figure this out and make it work. Here is the code (in the coffee script). Feel free to use it.

By doing this, you can do with any store (associations also):

store.isLoaded

If you want to call a method in the repository, but you need a busy store, you can use

store.invokeWhenLoaded(yourFunction)

The code itself explains (I think :-))

###
Because Ext.data.Store is missing something like 'isLoaded'
this adds the possibility to figure it out.
As a bonus ;-) there is a method called invokeWhenLoaded that you can
pass a function to that will be called once the store is loaded.
###
Ext.data.AbstractStore.override(
  constructor:(config) ->
    # Add additional properties to config
    Ext.apply(config, {
      isLoaded: false
      waitlist: []
    })
    # Call overridden constructor and remember return value
    rslt = @callOverridden(arguments)
    # Add onLoad listener
    @on('load', @onLoad)
    rslt

  # Executes each function in the waitlist and finally clears the waitlist
  processWaitlist:()->
    unless @waitlist.isEmpty()
      fn() for fn in @waitlist
      @waitlist = []

  ###
  Executes fn if the store is loaded or adds it to the waitlist if not.
  The fn will be called as soon the store is loaded
  It will try to load the store as well unless param loadLater is true

  Params:
    fn - the function to call once the store is loaded
  ###
  invokeWhenLoaded:(fn)->
    if @isLoaded 
      fn()
    else
      @waitlist.push(fn)

  ###
  @private event onLoad
  "Fires whenever the store [has read] data from a remote data source."
  ###
  onLoad:()->
    unless @isLoaded
      @isLoaded = true
      @processWaitlist()
)
+5
source share

All Articles