Hi guys,
I’m following this tutorial http://www.joshmorony.com/building-a-crud-ionic-2-application-with-firebase-angularfire/ (Hi Josh ) and I’m trying to enhance a bit the information that I send to firebase.
Basically I ask the user for an event title and a category, and then in the backend I want to add information, like the date and the participant limit for example.
It looks like my object gets created properly when I look a the console output (my newEvent has a date set as expected), but my firebase object gets only created with a title and a category. What the users inputs is there, but not the additional info I add at the object creation. Am i miss-using the ionic feature ?
The code of my event.ts
import { Component } from '@angular/core';
import { NavController, AlertController} from 'ionic-angular';
import {AngularFire, FirebaseListObservable} from 'angularfire2';
import { EventModel } from '../../app/models/eventmodel'
/*
Generated class for the Event page.
See http://ionicframework.com/docs/v2/components/#navigation for more info on
Ionic pages and navigation.
*/
@Component({
selector: 'page-event',
templateUrl: 'event.html'
})
export class EventPage {
events:FirebaseListObservable<any>;
constructor(public navCtrl: NavController, public alertCtrl:AlertController, af:AngularFire) {
this.events = af.database.list('/events');
}
addEvent(alertCtrl:AlertController) {
let prompt = this.alertCtrl.create({
title: 'Event name',
message: 'Enter the title of this event',
inputs: [
{
name: 'title',
placeholder: 'Title'
},
{
name: 'category',
placeholder: 'Category'
}
],
buttons:[
{
text: 'Cancel',
handler: data => {
console.log('Cancel button clicked');
}
},
{
text: 'Save',
handler: data => {
var newEvent = this.createEvent(data);
console.log("location:" + newEvent.location + " - category:" + newEvent.category + " - date:"
+ newEvent.date);
this.events.push(newEvent) ;
//this.events.push({title:data.title});
}
}
]
});
prompt.present();
}
createEvent(data: any){
var date = new Date();
var genEvent = new EventModel(data.title, date , data.category, null, 20, []);
return genEvent;
}
ionViewDidLoad() {
console.log('Hello EventPage Page');
}
}
Code for my eventModel.ts
import { Component } from '@angular/core';
export class EventModel {
title:String;
date:Date;
location:any;
category:String;
maxSize: Number;
participants:any[];
constructor(title: String='Default', date: Date = null, category:String='',
location: any=null, maxSize: Number=10, participants:any[]){
this.title = title;
this.date = date;
this.location = null;
this.category = category;
this.maxSize = null;
this.participants = participants;
}
}
Thanks a lot!