Skip to content

Commit

Permalink
Simplify angular demo
Browse files Browse the repository at this point in the history
- Use ngx-pipes to transform object to array of pairs
- Extract GunDb in its own file
- Introduce deletion
  • Loading branch information
victornoel committed Apr 19, 2017
1 parent 1fd2bfa commit c6fc02d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
2 changes: 2 additions & 0 deletions examples/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"express-http-proxy": "^0.11.0",
"gun": "^0.7.2",
"gun-edge": "^0.8.8",
"ngx-pipes": "^1.5.10",
"rxjs": "^5.3.0",
"underscore": "^1.8.3",
"zone.js": "^0.8.4"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/angular/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
</form>
<br />
<ul>
<li *ngFor="let todo of todos$ | async">{{todo.val}}</li>
<li *ngFor="let todo of todos$ | async | pairs" (click)="delete(todo[0])">{{todo[1]}}</li>
</ul>
</div>
15 changes: 9 additions & 6 deletions examples/angular/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Component, OnInit } from '@angular/core';
import { $rx } from 'gun-edge/edge/observable/rx';
import Gun from 'gun/gun';
import { Observable } from 'rxjs/Observable';
import { GunDb } from 'app/gun.service';
import { omit } from 'underscore';

import { GunDb } from 'app/app.module';
import { Subject } from 'rxjs/Subject';

@Component({
selector: 'app-root',
Expand All @@ -15,19 +17,20 @@ export class AppComponent implements OnInit {

todos = this.db.gun.get('todos');
todos$: Observable<string[]> = $rx(this.todos)
.startWith([])
.map(o => Object.keys(o).filter(k => typeof o[k] === 'string').map(k => ({ key: k, val: (o[k] as string) })));
.map(o => omit(o, '_'));

constructor(private db: GunDb) { }

ngOnInit() {
$rx(this.todos).subscribe(x => console.log(x));
}
ngOnInit() { }

add() {
if (this.newTodo) {
this.todos.path(Gun.text.random()).put(this.newTodo);
this.newTodo = '';
}
}

delete(key: string) {
this.todos.path(key).put(null);
}
}
13 changes: 4 additions & 9 deletions examples/angular/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { NgModule, Injectable } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import Gun from 'gun/gun';
import { NgPipesModule } from 'ngx-pipes';

import { AppComponent } from './app.component';
import { GunDb } from 'app/gun.service';

@NgModule({
declarations: [
Expand All @@ -15,15 +15,10 @@ import { AppComponent } from './app.component';
imports: [
BrowserModule,
FormsModule,
HttpModule
HttpModule,
NgPipesModule
],
providers: [GunDb],
bootstrap: [AppComponent]
})
export class AppModule { }

@Injectable()
export class GunDb {

readonly gun = Gun(location.origin + '/gun');
}
1 change: 0 additions & 1 deletion examples/angular/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

import 'rxjs/add/observable/of';
import 'rxjs/add/operator/startWith';

if (environment.production) {
Expand Down

0 comments on commit c6fc02d

Please sign in to comment.