forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 2
/
angular-meteor-tests.ts
255 lines (201 loc) · 7.69 KB
/
angular-meteor-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/// <reference path="angular-meteor.d.ts" />
interface ITodo {
_id?: string;
name: string;
public?: boolean;
sticky?: boolean;
}
interface TodoAngularMeteorObject extends ITodo, angular.meteor.AngularMeteorObject<ITodo> {}
interface CustomScope extends angular.meteor.IScope {
sticky: boolean;
todos: angular.meteor.AngularMeteorCollection<ITodo>;
stickyTodos: angular.meteor.AngularMeteorCollection<ITodo>;
notAutoTodos: angular.meteor.AngularMeteorCollection<ITodo>;
todo: ITodo;
todoNotAuto: TodoAngularMeteorObject;
todoSubscribed: TodoAngularMeteorObject;
save: (todo: ITodo) => void;
saveAll: () =>void;
autoSave: (todo: ITodo) => void;
remove: (todoId: string) => void;
removeAll: () => void;
removeAuto: (todo: ITodo) => void;
toSticky: (todo: ITodo) => void;
}
var Todos = new Mongo.Collection<ITodo>('todos');
var app = angular.module('angularMeteorTestApp');
app.controller("mainCtrl", ['$scope', '$meteor', ($scope: CustomScope, $meteor: angular.meteor.IMeteorService) => {
// Bind all the todos to $scope.todos
$scope.todos = $meteor.collection(Todos);
$scope.sticky = true;
// Bind all sticky todos to $scope.stickyTodos
// Binds the query to $scope.sticky so that if it changes, Meteor will re-run the query and bind it
// to $scope.stickyTodos
$scope.stickyTodos = $meteor.collection<ITodo>(function(){
return Todos.find({sticky: $scope.getReactively('sticky')});
});
// Bind without auto-save all todos to $scope.notAutoTodos
$scope.notAutoTodos = $meteor.collection(Todos, false).subscribe("publicTodos");
$scope.todoNotAuto = <TodoAngularMeteorObject>$meteor.object(Todos, 'TodoID', false);
$scope.todoSubscribed = <TodoAngularMeteorObject>$meteor.object(Todos, 'TodoID').subscribe('todos');
$scope.todo = $scope.todoNotAuto.getRawObject();
$scope.todoNotAuto.reset();
$scope.todoNotAuto.save($scope.todo).then((data) => { return data == 1; });;
// todo might be an object like this {text: "Learn Angular", sticky: false}
// or an array like this:
// [{text: "Learn Angular", sticky: false}, {text: "Hello World", sticky: true}]
$scope.save = function(todo) {
$scope.notAutoTodos.save(todo);
};
$scope.saveAll = function() {
$scope.notAutoTodos.save();
};
$scope.autoSave = function(todo) {
$scope.todos.push(todo);
};
// todoId might be an string like this "WhrnEez5yBRgo4yEm"
// or an array like this ["WhrnEez5yBRgo4yEm","gH6Fa4DXA3XxQjXNS"]
$scope.remove = function(todoId) {
$scope.notAutoTodos.remove(todoId);
};
$scope.removeAll = function() {
$scope.notAutoTodos.remove();
};
$scope.removeAuto = function(todo) {
$scope.todos.splice( $scope.todos.indexOf(todo), 1 );
}
$scope.toSticky = function(todo) {
if (angular.isArray(todo)){
angular.forEach(todo, function(object) {
object.sticky = true;
});
} else {
todo.sticky = true;
}
$scope.stickyTodos.save(todo);
};
var todoObject = {name:'first todo'};
var todosArray = [{name:'second todo'}, {name:'third todo'}];
var todoSecondObject = {name:'forth todo'};
$scope.todos.save(todoObject); // todos equals [{name:'first todo'}]
$scope.todos.save(todosArray); // todos equals [{name:'first todo'}, {name:'second todo'}, {name:'third todo'}]
$scope.todos.push(todoSecondObject); // The scope variable equals to [{name:'first todo'}, {name:'second todo'}, {name:'third todo'}, {name:'forth todo'}]
// but the collection still equals to [{name:'first todo'}, {name:'second todo'}, {name:'third todo'}]
$scope.todos.save(); // Now the collection also equals to [{name:'first todo'}, {name:'second todo'}, {name:'third todo'}, {name:'forth todo'}]
$scope.todos.remove('firstTodoId'); // scope and collection equals to [{name:'second todo'}, {name:'third todo'}, {name:'forth todo'}]
var todoIdsArray = ['secondTodoId', 'thirdTodoId'];
$scope.todos.remove(todoIdsArray); // removes everything matches the array of IDs both in scope and in collection
$scope.todos.pop(); // removes only in scope
$scope.todos.remove(); // syncs also in Meteor collection
// Subscribe ->
$meteor.subscribe('todos').then((subscriptionHandle) => {
// Bind all the todos to $scope.todos
$scope.todos = $meteor.collection(Todos);
console.log($scope.todos + ' is ready');
// You can use the subscription handle to stop the subscription if you want
subscriptionHandle.stop();
});
$scope.subscribe('todos').then((subscriptionHandle) => {
// Bind all the todos to $scope.books
$scope.todos = $meteor.collection(Todos);
console.log($scope.todos + ' is ready');
// No need to stop the subscription, it will automatically close on scope destroy
});
$meteor.call<ITodo>('subscribe', $scope.todo._id, $scope.currentUser._id).then((data) => {
// Handle success
console.log('success subscribing', data.name);
}, (err) => {
// Handle error
console.log('failed', err);
});
if (!$scope.loggingIn) {
$meteor.waitForUser();
$meteor.requireUser();
$meteor.requireValidUser(user => {
return user.username == 'admin';
});
$meteor.loginWithPassword('user', 'password').then(() => {
console.log('Login success');
}, err => {
console.log('Login error - ', err);
});
$meteor.createUser({
username:'moma',
email:'[email protected]',
password: 'Bksd@asdf',
profile: {expertize: 'Developer'}
}).then(() => {
console.log('Login success');
}, err => {
console.log('Login error - ', err);
});
$meteor.changePassword('old', 'new232f3').then(() => {
console.log('Change password success');
}, err => {
console.log('Error changing password - ', err);
});
$meteor.forgotPassword({email: '[email protected]'}).then(() => {
console.log('Success sending forgot password email');
}, err => {
console.log('Error sending forgot password email - ', err);
});
$meteor.resetPassword('tokenID', 'new232f3').then(() => {
console.log('Reset password success');
}, err => {
console.log('Error resetting password - ', err);
});
$meteor.verifyEmail('tokenID').then(() => {
console.log('Success verifying password ');
}, err => {
console.log('Error verifying password - ', err);
});
$meteor.logout().then(() => {
console.log('Logout success');
}, err => {
console.log('logout error - ', err);
});
$meteor.logoutOtherClients().then(() => {
console.log('Logout success');
}, err => {
console.log('logout error - ', err);
});
var loginWithOptions = {requestPermissions: ['email']};
$meteor.loginWithFacebook({requestPermissions: ['email']}).then(() => {
console.log('Login success');
}, err => {
console.log('Login error - ', err);
});
$meteor.loginWithGithub({requestPermissions: ['email']}).then(() => {
console.log('Login success');
}, err => {
console.log('Login error - ', err);
});
$meteor.loginWithGoogle({requestPermissions: ['email']}).then(() => {
console.log('Login success');
}, err => {
console.log('Login error - ', err);
});
$meteor.loginWithMeetup({requestPermissions: ['email']}).then(() => {
console.log('Login success');
}, err => {
console.log('Login error - ', err);
});
$meteor.loginWithTwitter({requestPermissions: ['email']}).then(() => {
console.log('Login success');
}, err => {
console.log('Login error - ', err);
});
$meteor.loginWithWeibo({requestPermissions: ['email']}).then(() => {
console.log('Login success');
}, err => {
console.log('Login error - ', err);
});
}
$meteor.autorun($scope, () => { console.log("Aurorun triggered."); });
$meteor.getCollectionByName('collectionName');
// requires meteor add mdg:camera
$meteor.getPicture().then(function(data){
$scope['picture'] = data;
});
$meteor.session('counter').bind($scope, 'counter');
}]);