DefinitelyTyped/angularfire/angularfire-tests.ts
2014-11-06 01:38:27 +09:00

106 lines
3.3 KiB
TypeScript

/// <reference path="angularfire.d.ts"/>
var myapp = angular.module("myapp", ["firebase"]);
interface AngularFireScope extends ng.IScope {
items: AngularFireArray;
remoteItems: RemoteItems;
}
interface RemoteItems {
bar: string;
}
var url = "https://myapp.firebaseio.com";
myapp.controller("MyController", ["$scope", "$firebase",
function ($scope: AngularFireScope, $firebase: AngularFireService) {
var sync = $firebase(new Firebase(url));
sync.$asArray()
.$loaded()
.then(function (list: AngularFireArray) {
console.log("list has " + list.length + " items");
list.$add({ foo: "bar" }).then(function (ref) {
ref.on("value", function (snapshot) {
if (snapshot.val().foo !== "bar") throw "error";
});
});
var item = list.$getRecord("foo");
list.$remove("foo");
list.$remove(0);
list.$save();
});
sync.$asObject()
$scope.items = sync.$asArray();
$scope.object = sync.$asObject();
$scope.items.$add({ foo: "bar" });
$scope.items.$remove("foo");
$scope.items.$remove();
$scope.items.$save();
var child = $scope.items.$child("foo");
child.$remove();
$scope.items.$set({ bar: "baz" });
var keys = $scope.items.$getIndex();
keys.forEach(function (key, i) {
console.log(i, (<any>$scope.items)[key]);
});
$scope.items.$on("loaded", function () {
console.log("Initial data received!");
});
$scope.items.$on("change", function () {
console.log("A remote change was applied locally!");
});
$scope.items.$off('loaded');
function stopSync() {
$scope.items.$off();
}
$scope.items.$bind($scope, "remoteItems");
$scope.remoteItems.bar = "foo";
$scope.items.$bind($scope, "remote").then(function (unbind) {
unbind();
$scope.remoteItems.bar = "foo";
});
}
]);
var foo: AngularFireObject = {
$priority: 0
};
interface AngularFireAuthScope extends ng.IScope {
loginObj: AngularFireAuth;
}
myapp.controller("MyAuthController", ["$scope", "$firebaseSimpleLogin",
function ($scope: AngularFireAuthScope, $firebaseSimpleLogin: AngularFireAuthService) {
var dataRef = new Firebase(url);
$scope.loginObj = $firebaseSimpleLogin(dataRef);
$scope.loginObj.$getCurrentUser().then(_ => {
});
var email = 'my@email.com';
var password = 'mypassword';
$scope.loginObj.$login('password', {
email: email,
password: password
}).then(function (user) {
console.log('Logged in as: ', user.uid);
}, function (error) {
console.error('Login failed: ', error);
});
$scope.loginObj.$logout();
$scope.loginObj.$createUser(email, password).then(_ => {
});
$scope.loginObj.$changePassword(email, password, password).then(_ => {
});
$scope.loginObj.$removeUser(email, password).then(_ => {
});
$scope.loginObj.$sendPasswordResetEmail(email).then(_ => {
});
}
]);