5

这不是一个问题,但也许我可以用该代码帮助其他人。

因为 ExtJS 商店没有类似 isLoaded() 的东西,所以我覆盖了它。但是我需要一些时间来弄清楚并让它发挥作用。这是代码(在咖啡脚本中)。随意使用它。

通过这样做,您可以对任何商店(以及关联)进行操作:

store.isLoaded

如果您想在商店上调用方法但需要加载商店,您可以使用

store.invokeWhenLoaded(yourFunction)

代码是自我解释的(我认为:-))

###
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()
)
4

0 回答 0