PDA

View Full Version : There should be a StoreProxy



iron9light
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?

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

iron9light
9 Apr 2012, 5:16 PM
Consider it done

iron9light
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: [
"Ext.data.Store"
]


isStore: true


config:
store: null


storeId: undefined


data: null


filters: null


sorters: null


grouper: null


groupField: null


groupDir: null


getGroupString: null


pageSize: 25


statics:
proxyMethods: [
"createDataCollection"
"applyStoreId"
"updateStoreId"
"updateGroupField"
"updateGroupDir"
"applyGetGroupString"
"applyGrouper"
"updateGrouper"
"isGrouped"
"updateSorters"
"updateSortTypes"
"updateFilters"
"each"
"getCount"
"getAt"
"getRange"
"getById"
"indexOf"
"indexOfId"
"sort"
"filter"
"filterBy"
"queryBy"
"clearFilter"
"getSorters"
"getFilters"
"getGroups"
"getGroupString"
"find"
"findRecord"
"findExact"
"findBy"
"first"
"last"
"sum"
"min"
"max"
"average"
]


proxyConfig:
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()
@data.setSortRoot('data');
@data.setFilterRoot('data');


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


@initConfig config


storeEventHooks:
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) ->
@data.clear()


@fireEvent "clear", @, eOpts


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


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


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


onStoreRefresh: (store, data, eOpts) ->
@data.clear()
@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
else
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
store
else
Ext.getStore store


updateStore: (store, oldStore) ->
bindEvents = Ext.apply(
{}
@storeEventHooks
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) ->
@data.clear()


@fireEvent "clear", @


if data
@suspendEvents()
@data.addAll data
@resumeEvents()


# @dataLoaded = true


@fireEvent "refresh", @, @data


clearData: ->
@setData null


# addData: (data) ->


isAutoLoading: ->
@getAutoLoad()


isFiltered: ->
@data.filtered


# 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
return