From 46ac9a42e9527d1b4bb0e1014fd8c92457d505ac Mon Sep 17 00:00:00 2001 From: Fabio Giaffaglione Date: Sun, 27 Nov 2016 23:07:39 +0100 Subject: [PATCH] Add, update and remove method have been implemented --- src/app/app.component.html | 10 ++--- src/app/app.component.ts | 42 +++++++++++++++---- .../item-dialog/item-dialog.component.html | 7 ++-- src/app/item-dialog/item-dialog.component.ts | 12 +++--- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index ecbe6e1..e5d3f68 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,11 +1,9 @@ - - - \ No newline at end of file + diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 709f86c..200c7a1 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -5,8 +5,8 @@ import { ItemDialogComponent } from './item-dialog/item-dialog.component'; @Component({ selector: 'app-root', - templateUrl: './app.component.html', - styleUrls: ['./app.component.css'] + template: require('./app.component.html'), + styles: [require('./app.component.css')] }) export class AppComponent { dialogRef: MdDialogRef; @@ -16,17 +16,18 @@ export class AppComponent { constructor(private af: AngularFire, public dialog: MdDialog) { this.items = af.database.list('/items'); console.log(this.items); - this.selectedItem = 'prova'; + this.selectedItem = { name: 'Prova' }; } - click(item) { + selectItem(item) { console.log(item); this.selectedItem = item; + this.openDialog(); } add() { console.log('add new item'); - const newItem = {item: { name: 'Test'}}; + // const newItem = {item: { name: 'Test'}}; // this.items.push(newItem); } @@ -35,15 +36,38 @@ export class AppComponent { disableClose: false, }); + this.dialogRef.componentInstance.newItem = this.selectedItem; + this.dialogRef.afterClosed().subscribe(result => { console.log('result: ' + result); this.dialogRef = null; - if(result) { - const newItem = {item: { name: result}}; - console.log(newItem); - this.items.push(newItem) + if (result) { + if (result.delete) { + if (result.item && result.item.$key) { + this.items.remove(result.item.$key); + } + } else { + const key = result.$key; + if (key) { + delete result['$key']; + delete result['$exists']; + this.items.update(key, result); + } else { + console.log(result); + this.items.push(result); + } + } } }); } } + + + +// WEBPACK FOOTER // +// /home/fgiaffaglione/angular2/demo/~/angular2-template-loader!/home/fgiaffaglione/angular2/demo/src/app/app.component.ts + + +// WEBPACK FOOTER // +// /home/fgiaffaglione/angular2/demo/~/angular2-template-loader!/home/fgiaffaglione/angular2/demo/src/app/app.component.ts diff --git a/src/app/item-dialog/item-dialog.component.html b/src/app/item-dialog/item-dialog.component.html index 2b4530d..d3bc496 100644 --- a/src/app/item-dialog/item-dialog.component.html +++ b/src/app/item-dialog/item-dialog.component.html @@ -1,10 +1,11 @@ - + - \ No newline at end of file + (click)="dialogRef.close(newItem)" md-tooltip="This is a tooltip!">Salva + + \ No newline at end of file diff --git a/src/app/item-dialog/item-dialog.component.ts b/src/app/item-dialog/item-dialog.component.ts index 81c44eb..6be87b4 100644 --- a/src/app/item-dialog/item-dialog.component.ts +++ b/src/app/item-dialog/item-dialog.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { MdDialogRef } from '@angular/material'; @Component({ @@ -6,11 +6,13 @@ import { MdDialogRef } from '@angular/material'; templateUrl: './item-dialog.component.html', styleUrls: ['./item-dialog.component.css'] }) -export class ItemDialogComponent { - firstname: string; +export class ItemDialogComponent implements OnInit { + newItem: any; - constructor(public dialogRef: MdDialogRef) { - this.firstname = ''; + constructor(public dialogRef: MdDialogRef) { + } + + ngOnInit() { }