mkdir nativescript-firebase
cd nativescript-firebase
npm init
tns init
mkdir -p platforms/ios
touch platforms/ios/Podfile
- In platforms/ios/Podfile:
pod 'Firebase'
mkdir -p platforms/android/libs
- Copy the Firebase SDK JAR to platforms/android/libs
- (Optional)
touch platforms/android/AndroidManifest.xml
- (Optional) In platforms/android/AndroidManifest.xml:
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
- In your NativeScript project:
tns plugin add <location of nativescript-firebase>
var appModule = require("application");
// ...
var Firebase = com.firebase.client.Firebase;
var ChildEventListener = com.firebase.client.ChildEventListener;
Firebase.setAndroidContext(appModule.android.context);
ref = new Firebase(<Firebase URL>);
ref.addChildEventListener(new ChildEventListener({
onChildRemoved: function(snapshot) {
// do your thing
},
onChildAdded: function(snapshot, previousChildKey) {
// do your thing
},
onChildChanged: function(snapshot, string) {
// do your thing
},
onChildMoved: function(snapshot, string) {
// do your thing
}
}));
To add new items:
var data = new java.util.HashMap();
data.put(...);
ref.push().setValue(data);
To delete items:
ref.child(id).setValue(null);
And so on.
var ref = new Firebase(<Firebase URL>);
ref.observeEventTypeWithBlock(FEventType.FEventTypeChildAdded, function (snapshot) {
// do your thing
});
ref.observeEventTypeWithBlock(FEventType.FEventTypeChildRemoved, function (snapshot) {
// do your thing
});
ref.observeEventTypeWithBlock(FEventType.FEventTypeChildChanged, function (snapshot) {
// do your thing
});
To add new items:
ref.childByAutoId().setValue(...);
To delete items:
ref.childByAppendingPath(id).setValue(null);
And so on.
I'd like to contribute to this thread with two functions, one to transform a javascript object into a HashMap for insertion into the Firebase database and another to to the opposite, it takes a HashMap and convert it back to javascript.
Javascript object to HashMap
You can use like this:
HashMap to Javascript object
And you can use like this:
Therefore one can work on the application level with javascript objects only.