is possible register firebase listener function without calling when register it?
for example:
this.gamestateurl.on('value', function(snapshot){ self.gamestatechangeevent(snapshot); });
gamestatechangeevent function fires upon setting listener.
thank you.
unfortunately, no. docs state:
this event trigger once initial data stored @ location, , trigger again each time data changes. datasnapshot passed callback location @ on() called. won't trigger until entire contents has been synchronized. if location has no data, triggered empty datasnapshot (val() return null).
you could, this:
var ref = this.gamestateurl // or create ref function dosomethingwithaddedorchangedsnapshot(snapshot) { // function called whenever child added // or changed } // assuming have "timestamp" property on these objects // not called snapshots on initialization // because timestamp of existing snapshots not greater // current time ref.orderbychild('timestamp') .startat(new date().gettime()) .on('child_added', dosomethingwithaddedorchangedsnapshot); ref.on('child_changed', dosomethingwithaddedorchangedsnapshot); ref.once('value', function(snapshot){ // initial state once // snapshot represents items on ref // fire once on initialization initializegamedata(snapshot.val()); });
in english:
- create 1 function handles updated/added child
- start listening
child_added
event children added after current timestamp (unix time since epoch). assumes you're storing timestamp on these children. - start listening
child_changed
event child changed. - grab values of ref once initialize data.
- not sure if use case needs handle 'child_removed' or 'child_moved'
Comments
Post a Comment