meteor - Json page with collection data -


i new nodejs , meteor. need create page content-type application/json , data mongo collection. when collection data change json page must change.

for json page use example: https://stackoverflow.com/a/23666992/1446182

if (meteor.isserver) {   meteor.startup(function () {    try{       var interval =  meteor.setinterval(function() {           var result = meteor.http.call("get", "http://192.168.2.144//ihale/meteorgetpage" );           var resultjson = json.parse(result.content);           var json = ihalecollection.findone();           ihalecollection.update(json, {$set: {json: resultjson}});       }, 1000);   }   catch(err) {       console.log(err);   }   });      router.map(function() {      this.route('jsonexample', {         where: 'server',         path: '/json',         action: function() {             var obj = ihalecollection.findone();             var headers = {'content-type': 'application/json'};             this.response.writehead(200, headers);             this.response.end(json.stringify(obj));         }     }); }); } 

when meteor.startup start update ihalecollection every second. how can update json page when ihalecollection change?

you can't. not way anyway. json has no builtin mechanism detect if source has changed , given router endpoint outputting raw json data no javascript, no automatic updates happen. when client refreshes endpoint there update.

meteor deals reactive data inserting javascript in underlying html page. if want utilize have write template, write helper method return json , use helper method in body.

the helper method should this:

template.jsondoc.helpers{(   json: function(){     return json.stringify(ihalecollection.findone());   } }) 

template can simple that:

<template name="jsondoc">   {{json}} </template> 

and route simple this:

router.route('/jsondoc'); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -