By not assigned I mean that when I do console.log(JSON.stringify(Happening))
after Happening = SelectedHappeningId.flatMap(function(x) {return Happenings.where({id: x});})
it’s an empty Observable array, it therefore couldn’t have assigned any values to Happening
.
Ok maybe I’ve gotten lost in translation here.
Happenings list is created
Happening is created
Happenings are retrieved from database as JSON and assigned to the Happenings observable
(UX) The user clicks on a list item (a happening), that selects it and user is routed to the edit page
An if statement checks whether the user is editing or adding a new happening
If the user is editing a happening, it gets the Happening in question from Happenings observable and assigns that entry to the Happening observable
(UX) UX uses the Happening observable to fill input fields in the form (Happening.title, Happening.text, etc...)
(UX) The user does changes to the fields and clicks the 'save' button
function save() {
grabs Happening items, packs them into a JS array and sends to server for SQL (update/insert) through PHP $_POST[]
}
And here’s the whole code:
var Observable = require('FuseJS/Observable');
var HappeningContext = require('Modules/HappeningContext');
var UserContext = require('Modules/UserContext');
var EventId = null;
Happenings = Observable();
NoHappenings = Observable(true);
Loading = Observable(true);
IsHost = Observable(false);
FormTitle = Observable();
FormMode = Observable();
SelectedHappeningId = Observable();
SelectedEventId = Observable();
NewHappening = Observable({event_id: SelectedEventId.value, title: "handsome title", text: "", from: {date: "", time: ""}, to: {date: "", time: ""}});
this.Parameter.onValueChanged(module, function(param) {
IsHost.value = param.isHost;
EventId = param.eventId;
loadHappenings();
});
function loadHappenings() {
Happenings.clear();
UserContext.getStorageUser().then(function(user_result) {
HappeningContext.getHappenings(EventId, user_result.id).then(function(happenings_result) {
if (happenings_result !== false) {
Happenings.replaceAll(happenings_result);
NoHappenings.value = false;
}
Loading.value = false;
});
});
}
function editHappening(){
if (IsHost.value) {
happeningsRouter.push("editHappening");
}
FormMode.value = "edit";
FormTitle.value = "Edit Happening";
Happening.value = Happenings.where({id: SelectedHappeningId.value});
}
function addHappening(){
if (IsHost.value) {
Happening.clear();
happeningsRouter.push("addHappening");
}
FormMode.value = "add";
FormTitle.value = "Add Happening";
}
function save() {
happeningsRouter.goto("savingHappening");
if (FormMode.value == "add") {
// HappeningContext.add(happeningObject).then(function(result){
// console.log("result: " + result);
// });
} else if (FormMode.value == "edit") {
var happeningObject = {
id: Happening.value.id,
method: "update",
event_id: Happening.value.event_id,
title: Happening.value.title,
text: Happening.value.text,
from_time: Happening.value.from.time,
from_date: Happening.value.from.date,
to_time: Happening.value.to.time,
to_date: Happening.value.to.date
};
HappeningContext.set(happeningObject).then(function(result){
if (result) {
happeningsRouter.goto("happenings");
} else {
happeningsRouter.goto("errorPage");
}
});
}
loadHappenings();
}
function retry() {
if (FormMode.value == "add") {
happeningsRouter.goto("addHappening");
} else if (FormMode.value == "edit") {
happeningsRouter.goto("editHappening");
}
}
module.exports = {
Happenings: Happenings,
NewHappening: NewHappening,
NoHappenings: NoHappenings,
IsHost: IsHost,
editHappening: editHappening,
addHappening: addHappening,
FormTitle: FormTitle,
SelectedHappeningId: SelectedHappeningId,
Happening: Happening,
save: save,
Loading: Loading,
FormMode: FormMode,
retry: retry
};