Created
January 31, 2014 11:10
-
-
Save topherfangio/8730150 to your computer and use it in GitHub Desktop.
frameworks/fire_core/record.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FireCore.attachedRecordTypes = SC.ArrayController.create({ | |
content: [] | |
}); | |
FireCore.DataSource = SC.DataSource.extend({ | |
firebaseApp: null, | |
firebase: function() { | |
var app = this.get('firebaseApp'); | |
return new Firebase('https://%@.firebaseio.com/'.fmt(app)); | |
}.property('firebaseApp'), | |
registeredRecordTypesBinding: SC.Binding.oneWay('FireCore.registeredRecordTypes.content'), | |
attachedRecordTypesBinding: SC.Binding.oneWay('FireCore.attachedRecordTypes.content'), | |
attachCallbacksIfNeeded: function() { | |
var firebase = this.get('firebase'), | |
registeredRecordTypes = this.get('registeredRecordTypes'), | |
attachedRecordTypes = this.get('attachedRecordTypes'); | |
console.log(' *** record types did change: ', registeredRecordTypes.toString()); | |
registeredRecordTypes.forEach(function(recordType) { | |
if (!attachedRecordTypes.contains(recordType)) { | |
console.log(' *** attaching record type: ', recordType); | |
var ref = firebase.child(recordType.firebaseRef()); | |
ref.on('child_added', function(snapshot) { | |
SC.RunLoop.begin(); | |
var id = snapshot.name(), | |
hash = snapshot.val(); | |
if (hash._sc_storeKey) { | |
store.dataSourceDidComplete(hash._sc_storeKey, hash, id); | |
ref.child('_sc_storeKey').remove(); | |
} else { | |
store.loadRecord(recordType, hash, id); | |
} | |
SC.RunLoop.end(); | |
}); | |
ref.on('child_changed', function(snapshot) { | |
SC.RunLoop.begin(); | |
var id = snapshot.name(), | |
hash = snapshot.val(); | |
// If we can't find a record, add it, otherwise update the existing one | |
if (!store.storeKeyExists(recordType, id)) { | |
store.loadRecord(recordType, hash, id); | |
} else { | |
store.pushRetrieve(recordType, id, hash); | |
} | |
SC.RunLoop.end(); | |
}); | |
ref.on('child_removed', function(snapshot) { | |
SC.RunLoop.begin(); | |
var id = snapshot.name(); | |
store.pushDestroy(recordType, id); | |
SC.RunLoop.end(); | |
}); | |
attachedRecordTypes.addObject(recordType); | |
} | |
}); | |
}.observes('registeredRecordTypes'), | |
commitRecords: function(store, createKeys, updateKeys, destroyKeys, params) { | |
var firebase = this.get('firebase'), | |
type, name, hash, ref, id; | |
createKeys.forEach(function(key) { | |
type = store.recordTypeFor(key); | |
name = (type.toString().split('.')[1]).decamelize().pluralize(); | |
hash = SC.merge(store.readDataHash(key), { _sc_storeKey: key }); | |
ref = firebase.child(name); | |
console.log(' *** creating key: ', key, type, name, hash); | |
ref.push(hash); | |
}); | |
updateKeys.forEach(function(key) { | |
type = store.recordTypeFor(key); | |
name = (type.toString().split('.')[1]).decamelize().pluralize(); | |
hash = store.readDataHash(key); | |
ref = firebase.child(name); | |
console.log(' *** updating key: ', key, type, name, hash); | |
id = ref.push(hash); | |
store.dataSourceDidComplete(key, hash, id); | |
}); | |
destroyKeys.forEach(function(key) { | |
type = store.recordTypeFor(key); | |
name = (type.toString().split('.')[1]).decamelize().pluralize(); | |
id = record.get('id'); | |
ref = firebase.child(name).child(id); | |
console.log(' *** destroying key: ', key, type, name); | |
ref.remove(); | |
store.dataSourceDidDestroy(key); | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FireCore.registeredRecordTypes = SC.ArrayController.create({ | |
content: [] | |
}); | |
FireCore.Record = SC.Record.extend(); | |
FireCore.Record.reopen({ | |
extend: function() { | |
FireCore.registeredRecordTypes.addObject(this); | |
return sc_super(); | |
} | |
}); | |
FireCore.Record.mixin({ | |
firebaseRef: function() { | |
return this.toString().split('.')[1].decamelize().pluralize(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment