I'm building a series of checkboxes and associated labels in my DOM by reading a JSON file.
// JavaScript for Sec A
//
// Load the checkboxes and their respective listeners onto the DOM
//
var main = function () {
"use strict";
$.getJSON("../data/checkBoxesA.json", function(checkBoxTxt) {
checkBoxTxt.forEach(function (data) {
var $checkbox = "<input type ='checkbox' value = ' '/>";
$(".enroll_actions").append($checkbox);
$(".enroll_actions").append(' ' + data.label + "<br/>");
$(".enroll_actions").append(' '+ "<br/>");
$(".enroll_actions").append(' ' + data.note + "<br/>");
$(".enroll_actions").append(' '+ "<br/>");
$(".enroll_actions").on("click", function(event) {
console.log("Hello World!!!");
});
});
});
}
$(document).ready(main);
At this point each checkbox has it's own listener. This is Ok so far and displays clickable checkboxes.
The server.js connects to the DB and displays the page, in this case secA.
var express = require("express"),
http = require("http"),
mongoose = require( "mongoose" ),
app = express();
// set up a static file directory to use for
// default routing
app.use(express.static(__dirname + "/client"));
app.use(express.urlencoded());
// connect to r_PE data store in mongo
mongoose.connect('mongodb://localhost/CheckBoxSchema', function(err) {
if(err){
console.log(err);
} else{
console.log('Connected to mongodb!');
}
});
// This is our mongoose data model for check box entries
var CheckBoxSchema = mongoose.Schema({
checked: Boolean,
created: {type: Date, default: Date.now}
});
var CheckBox = mongoose.model("Store", CheckBoxSchema);
// Create our Express-powered HTTP server
http.createServer(app).listen(3000);
console.log("Server listening at http://127.0.0.1:3000/");
// We can update our todo get route to get the to-do items
// out of the database and return them:
app.get("/checkbox.json", function (req, res) {
CheckBox.find( {}, function (err, checkBox) {
res.json(checkBox);
});
});
app.post("/checkbox", function (req, res) {
console.log(req.body);
var newCheckBox = new CheckBox({"checked":req.body.tags});
newCheckBox.save(function (err, result) {
if (err !== null) {
console.log(err);
res.send("ERROR");
} else {
// our client expects *all* of the todo items to be returned,
// so we do an additional request to maintain compatibility
CheckBox.find({}, function (err, result) {
if (err !== null) {
// the element did not get saved!
res.send("ERROR");
}
res.json(result);
});
}
});
})
There is no save to DB. The text I'm using has only a brief account of NoSQL DBs and nothing wit checkboxes.
This is my first exploration on javaScript and I know this is some simple answer but I've been stuck for several days. Any help from the more experienced will be appreciated!
Aucun commentaire:
Enregistrer un commentaire