mirror of
https://github.com/amark/gun.git
synced 2026-03-13 11:55:07 +00:00
use gun for todo list
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
html, body { font-size: 14pt; padding: 10px 2.5%;}
|
||||
.hide { display: none; }
|
||||
form .who { width: 10%; }
|
||||
form .what { width: 80%; }
|
||||
ul { list-style: none; padding: 0; }
|
||||
ul .when {color: #555; font-size: 12pt; float: right; display: none; }
|
||||
li:hover .when {display: inline;}
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
<h1>
|
||||
{{title}}
|
||||
</h1>
|
||||
<div>
|
||||
|
||||
<form (ngSubmit)="add()">
|
||||
<input type="text" [(ngModel)]="newTodo" name="newTodo" placeholder="New TODO">
|
||||
<button type="submit" *ngIf="newTodo" [disabled]="newTodo?.trim().length === 0">Add</button>
|
||||
</form>
|
||||
<br />
|
||||
<ul>
|
||||
<li *ngFor="let todo of todos$ | async">{{todo.val}}</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@@ -1,10 +1,33 @@
|
||||
import { Component } from '@angular/core';
|
||||
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/app.module';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.component.html',
|
||||
styleUrls: ['./app.component.css']
|
||||
})
|
||||
export class AppComponent {
|
||||
title = 'app works!';
|
||||
export class AppComponent implements OnInit {
|
||||
newTodo = '';
|
||||
|
||||
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) })));
|
||||
|
||||
constructor(private db: GunDb) { }
|
||||
|
||||
ngOnInit() {
|
||||
$rx(this.todos).subscribe(x => console.log(x));
|
||||
}
|
||||
|
||||
add() {
|
||||
if (this.newTodo) {
|
||||
this.todos.path(Gun.text.random()).put(this.newTodo);
|
||||
this.newTodo = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
import { NgModule } from '@angular/core';
|
||||
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 { AppComponent } from './app.component';
|
||||
|
||||
@@ -14,7 +17,13 @@ import { AppComponent } from './app.component';
|
||||
FormsModule,
|
||||
HttpModule
|
||||
],
|
||||
providers: [],
|
||||
providers: [GunDb],
|
||||
bootstrap: [AppComponent]
|
||||
})
|
||||
export class AppModule { }
|
||||
|
||||
@Injectable()
|
||||
export class GunDb {
|
||||
|
||||
readonly gun = Gun(location.origin + '/gun');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user