angularfire-resource
Resource factory built on top of AngularFire
Alpha release, feedback apreciated
Abstract
Resource instances are extended firebaseObject, so you'll find everything you're used to with firebase object, plus
- createdAt / updatedAt timestamps
- hooks (beforeCreate, afterCreate, beforeSave, afterSave)
- a global instances map for each resource to ensure an instance is not retrieved 2 times from firebase
- associations (hasOne, hasMany, hasAndBelongsToMany returning respectively a firebaseObject instance or association collections)
AssociationCollection instances are extended firebaseArray, plus
- they are made of Resource instances
- they can preload their own instances associations
- they deal nicely with the firebase-util librairy for pagination & infinite scrolling work
Usage
Add the 'angularfire-resource' module as a dependency to your application module, and then define your model classes and their relations through the FireResource factory
angular
The above code will maintain a deserialized data model with duplicated foreign keys, to allow security enforcement and easy admin queries.
root
|_ users
| |_ userId1
| | |_ displayedConversationId: conversationId1
| | |_ conversations
| | |_ conversationId1: true
| |_ userId2
| |_ conversations
| |_ conversationId1: true
|
|
|_ conversations
| |_ conversationId1
| |_users
| | |_ userId1: true
| | |_ userId2: true
| |_ messages
| |_ messageId1: aCustomValue
| |_ messageId2: aCustomValue
|
|
|_ messages
|_ messageId1
| |_ userId: userId1
| |_ conversationId: conversationId1
|_ messageId2
|_ userId: userId2
|_ conversationId: conversationId1
And now you can write some clean controllers :-)
angular // Let's asume $currentUser is an instance of User retrieved from a resolve
Demo
To have a more in depth look over a practical case, check out the demo