ngn-data
Version:
Data modeling, stores, proxies, and utilities for NGN
190 lines (147 loc) • 3.94 kB
JavaScript
/**
* v0.1.11 generated on: Sat Jun 24 2017 01:20:07 GMT+0000 (UTC)
* Copyright (c) 2014-2017, Ecor Ventures LLC. All Rights Reserved. See LICENSE (BSD3).
*/
'use strict'
class NgnDataProxy extends NGN.EventEmitter {
constructor (config) {
config = config || {}
super(config)
Object.defineProperties(this, {
store: NGN.private(null),
initialized: NGN.private(false),
liveSyncEnabled: NGN.private(false),
_enabled: NGN.private(true),
id: NGN.privateconst(NGN.DATA.util.GUID())
})
}
get state () {
return this._enabled ? 'enabled' : 'disabled'
}
get enabled () {
return this._enabled
}
get disabled () {
return !this._enabled
}
get type () {
return this.store instanceof NGN.DATA.Store ? 'store' : 'model'
}
get connectionpool () {
return NGN.DATA.ConnectionPool
}
enable () {
if (!this._enabled) {
this._enabled = true
this.emit('enabled')
this.emit('statechange', this.state)
}
}
disable () {
if (this._enabled) {
this._enabled = false
this.emit('disabled')
this.emit('statechange', this.state)
this.disableLiveSync()
}
}
init (store) {
if (this.initialized) {
return
} else {
this.initialized = true
}
this.store = store
if (store instanceof NGN.DATA.Store) {
Object.defineProperties(store, {
changelog: NGN.get(() => {
return this.changelog
})
})
}
}
get changelog () {
const me = this
if (this.store === null && !(this instanceof NGN.DATA.Store)) {
return []
}
return {
create: this.store._created,
update: this.store.records.filter(function (record) {
if (me.store._created.indexOf(record) < 0 && me.store._deleted.indexOf(record) < 0) {
return false
}
return record.modified
}).map(function (record) {
return record
}),
delete: this.store._deleted
}
}
save () {
}
fetch () {
console.warn('Fetch should be overridden by a proxy implementation class.')
}
enableLiveSync () {
if (this.liveSyncEnabled) {
return
}
this.liveSyncEnabled = true
this.switchSync('on')
this.emit('livesync.enabled')
}
disableLiveSync () {
if (!this.liveSyncEnabled) {
return
}
this.liveSyncEnabled = false
this.switchSync('off')
this.emit('livesync.disabled')
}
switchSync (fn) {
if (this.type === 'model') {
this.store[fn]('field.create', this.createModelRecord)
this.store[fn]('field.update', this.updateModelRecord)
this.store[fn]('field.remove', this.deleteModelRecord)
this.store[fn]('relationship.remove', this.deleteModelRecord)
} else {
this.store[fn]('record.create', this.createStoreRecord)
this.store[fn]('record.restored', this.createStoreRecord)
this.store[fn]('record.update', this.updateStoreRecord)
this.store[fn]('record.delete', this.deleteStoreRecord)
this.store[fn]('clear', this.clearStoreRecords)
}
}
createModelRecord () {
this.shouldOverride('createModelRecord')
}
updateModelRecord () {
this.shouldOverride('updateModelRecord')
}
deleteModelRecord () {
this.shouldOverride('deleteModelRecord')
}
createStoreRecord () {
this.shouldOverride('createStoreRecord')
}
updateStoreRecord () {
this.shouldOverride('updateStoreRecord')
}
deleteStoreRecord () {
this.shouldOverride('deleteStoreRecord')
}
clearStoreRecords () {
this.shouldOverride('clearStoreRecords')
}
proxyRecordFilter (model) {
if (model.hasOwnProperty('proxyignore')) {
return !model.proxyignore
}
return true
}
static shouldOverride (methodName) {
console.warn(methodName + ' should be overridden by a proxy implementation class.')
}
}
NGN.DATA.Proxy = NgnDataProxy