isLoaded用于ExtJS数据存储

时间:2012-07-19 09:52:57

标签: extjs extjs4

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

因为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()
)

0 个答案:

没有答案
相关问题