View Full Version : There should be a StoreProxy

9 Apr 2012, 8:26 AM
StoreProxy is just a wrapper of another store. It's read only, it cannot insert or delete records. but can be with filters, sorters and groupers.

StoreProxy can be used by Dataview etc.

StoreProxy will fire event when underlining store changed.

Did someone write this kinda class before?

9 Apr 2012, 8:27 AM
Shouldn't be hard to do. Do you accept the challenge? :D

9 Apr 2012, 5:16 PM
Consider it done

11 Apr 2012, 6:12 AM
Here it is,

It works for me now.

Ext.define "Ext.ux.data.StoreProxy",
alias: "store.storeproxy"
extend: 'Ext.Evented'
requires: [

isStore: true

store: null

storeId: undefined

data: null

filters: null

sorters: null

grouper: null

groupField: null

groupDir: null

getGroupString: null

pageSize: 25

proxyMethods: [

autoLoad: null
autoSync: false
model: undefined
proxy: undefined
fields: null
remoteSort: false
remoteFilter: false
remoteGroup: false
totalCount: null
clearOnPageLoad: true
modelDefaults: {}
autoDestroy: false
syncRemovedRecords: true

currentPage: 1

constructor: (config) ->
config or= {}

@data = @_data = @createDataCollection()

if config.id and not config.storeId
config.storeId = config.id
delete config.id

@initConfig config

addrecords: "onStoreAddrecords"
beforeload: "onStoreBeforeload"
beforesync: "onStoreBeforesync"
clear: "onStoreClear"
load: "onStoreLoad"
metachange: "onStoreMetachange"
refresh: "onStoreRefresh"
removerecords: "onStoreRemoverecords"
updaterecord: "onStoreUpdaterecord"
write: "onStoreWrite"

onStoreAddrecords: (store, records, eOpts) ->
addedRecords = @data.addAll records
if addedRecords
@fireEvent "addrecords", @, addedRecords, eOpts

onStoreBeforeload: (store, operation, eOpts) ->
@fireEvent "beforeload", @, operation, eOpts

onStoreBeforesync: (options, eOpts) ->
@fireEvent "beforesync", options, eOpts

onStoreClear: (store, eOpts) ->

@fireEvent "clear", @, eOpts

onStoreLoad: (store, records, successful, operation) ->
if successful
@data.addAll store.getRange()

@fireEvent "load", @, @data.items, successful, operation

onStoreMetachange: (store, data, eOpts) ->
@fireEvent "metachange", @, data, eOpts

onStoreRefresh: (store, data, eOpts) ->
@data.addAll store.getRange()

@fireEvent "refresh", @, @data, eOpts

onStoreRemoverecords: (store, records, indices, eOpts) ->
removedRecords = []
removedIndices = []
for record in records
index = @data.indexOf record
if index >= 0
removedRecords.push record
removedIndices.push index

@data.removeAll records

if removedRecords.length
@fireEvent "removerecords", @, removedRecords, removedIndices, eOpts

onStoreUpdaterecord: (store, record, newIndex, oldIndex, modifiedFieldNames, modifiedValues, eOpts) ->
idProperty = record.getIdProperty()
if Ext.Array.contains modifiedFieldNames, idProperty
oldId = modifiedValues[idProperty]
oldIndex = @data.indexOfKey oldId
@data.replace oldId, record
oldIndex = @data.indexOfKey record.getId()
@data.replace record

newIndex = @data.indexOf record
if oldIndex >= 0 and newIndex >= 0
@fireEvent "updaterecord", @, record, newIndex, oldIndex, modifiedFieldNames, modifiedValues, eOpts
else if oldIndex >= 0
@fireEvent "removerecords", @, [record], [oldIndex], eOpts
else if newIndex >= 0
@fireEvent "addrecords", @, [record], eOpts

onStoreWrite: (store, operation, eOpts) ->
@fireEvent "write", @, operation, eOpts

applyStore: (store) ->
if store.isStore
Ext.getStore store

updateStore: (store, oldStore) ->
bindEvents = Ext.apply(
scope: @

if oldStore and Ext.isObject(oldStore) and oldStore.isStore
oldStore.un bindEvents

if store and Ext.isObject(store) and store.isStore
store.on bindEvents
@setData store.getRange()

applyData: (data) ->

@fireEvent "clear", @

if data
@data.addAll data

# @dataLoaded = true

@fireEvent "refresh", @, @data

clearData: ->
@setData null

# addData: (data) ->

isAutoLoading: ->

isFiltered: ->

# load: (options, scope) -> Ext.Logger.warn "Unsupported method: load"

isLoading: -> @getStore().isLoading()

isLoaded: -> @getStore().isLoaded()

# sync: -> Ext.Logger.warn "Unsupported method: sync"

# doDataRefresh: (store, data, operation) ->

# getNewRecords: ->

# getUpdatedRecords: -> Ext.Logger.warn "Unsupported method: getUpdatedRecords"

# getRemovedRecords: -> Ext.Logger.warn "Unsupported method: getRemovedRecords"

# loadPage: (page, options, scope) ->

# nextPage: (options) ->

# previousPage: (options) ->
, ->
for method in @proxyMethods
@prototype[method] = Ext.data.Store.prototype[method]
generateSetter = (setter) ->
-> Ext.Logger.warn "#{setter} will do nothing"
generateGetter = (getter, defaultValue) ->
store = @getStore()
if store then store[getter]() else defaultValue
for name, defaultValue of @proxyConfig
capitalizedName = name.charAt(0).toUpperCase() + name.substr(1)
setter = "set#{capitalizedName}"
@prototype[setter] = generateSetter setter
getter = "get#{capitalizedName}"
@prototype[getter] = generateGetter getter