Browse Source

ajax listmessages and messages is working

pull/1/head
Senta Mandutz 5 years ago
parent
commit
b52de633b0
3 changed files with 41 additions and 27 deletions
  1. 20
    23
      public/home.js
  2. 1
    1
      public/index.html
  3. 20
    3
      server.js

+ 20
- 23
public/home.js View File

@@ -23,34 +23,31 @@ const HomeRouter = {
tags: "foo"
});
},*/
list_messages: function () {
console.log("egal");
$.get('api/ids/').done(function (jd) {
// _messagelist = jd;
_messagelist.splice(0, _messagelist.length);
_messagelist.push.apply(_messagelist, jd);
console.log("egal2");
//$('#xxx').text(jd[0]);
for (var e in jd) {
if (!_messages[jd[e]]) {
get_insert_message(jd[e]);
}
}
}).fail(function (e, f, g) {
console.log("egal3: " + e + f + g);
});
}
list_messages: function () {
$.ajax({url: "/api/ids", method: "GET"})
.done(jd => {
// NICHT SO wg. Vue: _messagelist = jd;
_messagelist.splice(0, _messagelist.length);
_messagelist.push.apply(_messagelist, jd);
for (var e in jd) {
if (!_messages[jd[e]]) {
get_insert_message(jd[e]);
}
}
}).fail(function (e, f, g) {
console.log("err: " + e + f + g);
});
}
},

mounted: function() {
this.list_messages();
/*this.refresh_messages();*/
mounted: function () {
this.list_messages();
//this.refresh_messages();
}
};

function get_insert_message(msg_id) {
$.get(api/msg/{msg_id}).done(function (msg) {
// _messages[id] = msg;
function get_insert_message(id) {
$.ajax({ url: "/api/msg/"+id, method: "GET" }).done(function (msg) {
Vue.set(_messages, id, msg);
}).fail(function (e, f, g) {
console.log("cannot load " + id + ".json: " + e + f + g);

+ 1
- 1
public/index.html View File

@@ -74,7 +74,7 @@

<nav class="nav nav-tabs nav-justified om-nav" v-if="$route.path !=='/createMessage' ">
<router-link to="/home" class="nav-item nav-link"><i class="material-icons">home</i></router-link>
<router-link to="/files" class="nav-item nav-link"><i class="material-icons">folder</i></router-link>
<router-link to="/files" class="nav-item nav-link"><i class="material-icons">language</i></router-link>
<router-link to="/createMessage" class="nav-item nav-link outlined"><i class="material-icons">add_circle</i></router-link>
<router-link to="/bookmark" class="nav-item nav-link"><i class="material-icons">bookmark</i></router-link>
<router-link to="/profil" class="nav-item nav-link"><i class="material-icons">person</i></router-link>

+ 20
- 3
server.js View File

@@ -103,9 +103,24 @@ mongoose.connect(dbConfig.url)

//require('./app/routes/message.route.js')(app);

app.get ("/api/test/:id", function (req, res) {
console.log ("id: " + req.params.id + " message " + req.query.msg); //bei POST: req.body.msg
Message.find ({_id: req.params.id}) .exec (function (err, results) {

app.get ('/api/ids', function (req, res) {
Message.find({},{_id: true}) .exec () .then(results => {
/* selects id from message: var parsed = [];
for (var i in results) {
parsed.push (results[i]._id);
} */
var parsed = results.map (x => x._id);
res.send(parsed);
} )
.catch(err => {
console.log (err);
res .status(500) .json (err);
});
});

app.get ("/api/msg/:id", function (req, res) {
Message.findOne ({_id: req.params.id}) .exec (function (err, results){
if (err) {
console.log (err);
res .status(404) .json (err);
@@ -210,3 +225,5 @@ process.on ("uncaughtException", function (err) {
console.error ("*** Uncaught Exception:");
console.error (err.stack);
});



Loading…
Cancel
Save