问题
I'm currently working on an ionic app and I'm using the Ionic Native Calendar Plugin to create calendar events in my project. I want to be able to change the dates and times dynamically for each event so I'm using Firebase as the backend and would like to integrate the calendar parameters into my JSON file to achieve this.
Everything is working well following the standard method, which is, having a button with a function:
**home.html**
<button ion-button (click)="createEvent()">Add to Calendar</button>
and then declaring the function:
**home.ts**
createEvent() {
this.calendar.createEvent('myEventName', 'myEventLocation', 'myEventNote', new Date(2017, 9, 20, 13, 0, 0, 0),
new Date(2017, 9, 20, 14, 0, 0, 0)).then(() => {
console.log('Event Created!');
}).catch((err) => {
console.log('Oops, something went wrong:', err);
});
}
If I run this method above in my project the event gets created successfully. However, using this method will then not allow me to be able to create calendar events dynamically, which is what I'm trying to achieve. So somehow, I need to link up the code inside my createEvent()
in home.ts
file into my JSON
file, right? So this is what I've tried so far and nothing seems to be working, and I don't know where I'm going wrong - I'm suspecting I'm not converting the new Date() parameters properly into a readable JSON string? Here is what I've done so far in trying to create my events dynamically via JSON:
home.html:
<button ion-button (click)="createEvent()">Add to Calendar</button>
home.ts:
import firebase from 'firebase';
export class HomePage {
events = [];
constructor(..) {
firebase.database().ref('events').on('value', snapshot => {
this.events = snapshot.val();
});
}
createEvent() {
this.calendar.createEvent(
this.item.item[0].title,
this.item.item[0].location,
this.item.item[0].notes,
this.item.item[0].startDate,
this.item.item[0].endDate,
).
then(() => {
console.log('Event Created!');
})
.catch((err) => {
console.log('Oops, something went wrong:', err);
});
}
JSON file (in Firebase database):
"events" : [ {
"title" : "myEventName",
"location" : "myEventLocation",
"notes" : "myEventNote",
"startDate" : "new Date(2017, 9, 20, 13, 0, 0, 0)",
"endDate" : "new Date(2017, 9, 20, 14, 0, 0, 0)"
}, {
So, when I run the above nothing happens, no calendar event is getting created using this method. Please assist. Thanks.
回答1:
Just addressing the question, how to convert a date to JSON, you can use Date.prototype.toJSON()
(MDN for more info). But I'm not sure if that will actually address your issue of creating an event dynamically.
回答2:
After a bit of snooping around, I got it to work using the below method...
home.html:
<button ion-button (click)="createEvent()">Add to Calendar</button>
home.ts:
import firebase from 'firebase';
export class HomePage {
public events;
public item;
constructor(...) {
firebase.database().ref('events').on('value', snapshot => {
this.events = snapshot.val();
});
this.item.startDate = new Date(this.item.startDate);
this.item.endDate = new Date(this.item.endDate);
}
createEvent() {
this.calendar.createEventInteractively(
this.item.title,
this.item.eventLocation,
this.item.notes,
this.item.startDate,
this.item.endDate,
).
then(() => {
console.log('Event Created!');
})
.catch((err) => {
console.log('Oops, something went wrong:', err);
});
}
JSON file (in Firebase database):
"events" : [ {
"title" : "myEventName",
"eventLocation" : "myEventLocation",
"notes" : "myEventNote",
"startDate" : "2010-05-10T10:20:00+02:00",
"endDate" : "2010-05-10T18:30:00+02:00"
}, {
来源:https://stackoverflow.com/questions/46838993/how-to-convert-new-date-into-json-string