Wednesday 15 May 2013

javascript - AngularFire 0.82 - How to query denormalised data? -



javascript - AngularFire 0.82 - How to query denormalised data? -

this question similar angularfire - how query denormalised data? want reply uses angularfire (current version 0.82). illustration of info construction use:

{ "users": { "user1": { "name": "alice", "lists": { "list1": "true" } }, "user2": { "name": "bob", "lists": { "list2": "true" } } }, "lists": { "list1": { "title": "example" }, "list2": { "title": "example2" } } }

or, in different notation: of 1 user's lists need of these:

users/$userid/lists/$listid

and lists' content stored under:

lists/$listid

what want accomplish able access content of specific user's lists, without "manually" iterating on each $listid.

an nicer reply can include code wraps access in angularfire extended service, similar code under "creating angularfire services" @ angularfire docs

it help (as does) understand utilize case we're trying resolve rather solution you've picked problem--there simpler ways approach problem set (see what xy problem?).

there nil built angularfire handle nested lists of lists. list of specific elements quite bit easier several lists referenced list.

firebase.util

a tool called firebase.util exists help denormalization (it's getting revved v2, due out in month or two), , it's compatible angularfire:

var fbref = new firebase(url); var indexref = fbref.child('users/' + userid + '/lists'); var dataref = fbref.child('lists'); var joinedref = firebase.util.intersection(indexref, dataref); var lists = $firebase( joinedref ).$asarray(); angular only

an angular-only approach load lists hand , utilize angularfire individual lists:

app.factory('nestedlist', function($firebase, $timeout) { homecoming function(indexref, dataref) { var hashoflists = {}; indexref.on('child_added', function(snap) { var key = snap.key(); var list = $firebase( dataref.child(key).$asarray(); hashoflists[key] = list; }); indexref.on('child_removed', function(snap) { $timeout(function() { delete hashoflists[snap.key()]; }); }); homecoming hashoflists; } });

note illustration uses snap.key(), applies when using version 2.x of sdk, released week; prev versions should utilize snap.name()

for more advanced operations , create own directives or services, check out question , reply here. although may not appear @ first glance, pretty thorough give-and-take of how take firebase info , transform proprietary structure.

javascript angularjs firebase angularfire

No comments:

Post a Comment