I'm passing a data object to a template with iron-router but would like to access the data within the Template.name.rendered = function() { ... } callback.
From Meteor data-context with iron-router I've tried UI.getData() but receive an error There is no current view. Using this.data returns null.
How can I get access to the data object passed to the template from the rendered callback?
You were on the right track with looking for the data context, but this is actually how you get access to it:
var ctx = Template.currentData();
Also, I believe Template.x.rendered is about to be deprecated, so if that doesn't work, try using
Template.x.onRendered(function() {
var ctx = Template.currentData();
console.log(ctx);
});
Related
I have a custom logging function which is assigned to express requests req.log object using middleware.
The purpose of this is for the logging funtion to be able to read the request headers.traceid, before transmitting the log event to a seperate service.
This is working perfectly fine using middlware or with an extra parameter in the function, however to simplify the use of it.
What I'd really like to know if there's a way for the function to be able to read the req object from the scope it was called in, without referencing it either using middlware or as a function paramter?
// log.js
module.exports = () => {
console.log(...arguments)
const req = getVarFromParentScope("req") || undefined
const traceId = req?.headers.traceid || null
// POST { timestamp, traceId, args: { ...arguments } } to logging service
}
No, it isn't.
(And if it was, then the ability for a function to access a variable inside another function just because it was the calling function would make it very easy to write code that was very hard to debug.)
I'm running GM_xmlhttpRequest (in a Greasemonkey script) and storing the responseText into a newly created HTML element:
var responseHTML = document.createElement('HTML');
...
onload: function() { responseHTML.innerHTML = response.responseText; }
And then I am trying to find an element in responseHTML:
console.log(responseHTML.getElementsByTagName('div'));
console.log(responseHTML.getElementById('result_0'));
The first works fine, but not the second. Any ideas?
Use DOMParser() to convert responseText into a searchable DOM tree.
Also, your attempts to search/use anything derived from responseText, must occur inside the onload function.
Use code like this:
GM_xmlhttpRequest ( {
...
onload: parseAJAX_ResponseHTML,
...
} );
function parseAJAX_ResponseHTML (respObject) {
var parser = new DOMParser ();
var responseDoc = parser.parseFromString (respObject.responseText, "text/html");
console.log (responseDoc.getElementsByTagName('div'));
console.log (responseDoc.getElementById('result_0'));
}
Of course, also verify that a node with id result_0 is actually in the returned HTML. (Using Firebug, Wireshark, etc.)
getElementById is not a method of HTML elements. It is a method of the document node. As such you can't do:
div.getElementById('foo'); // invalid code
You can implement your own function to search the DOM by recursively going through children. On newer browsers you can even use the querySelector method. For minimal development you can use libraries like jQuery or sizzle.js (the query engine behind jQuery).
There is no need to store the response in an element neither use DOMParser()
Just set the responseType to 'document' and the response will be parsed automatically and stored in the responseXML
Example:
var ajax = new XMLHttpRequest();
ajax.open('get','http://www.taringa.net');
ajax.responseType = 'document';
ajax.onload = function(){
console.log(ajax.responseXML); //And this is a document which may execute getElementById
};
ajax.send();
Currently, I'm developing an API with sails js + mongoDB for an mobile app. I want to save multiple data that send from mobile app and I catch it via req parameters (as object) in my controller's function.
Some parts of my controller code look like this :
var contactData = req.allParams().Contact;
looping.. {
Contact.create(contactData[index]).exec(function addContact(err,createdContact){ });
}
Could you tell me how the best way to implement looping for save that multiple contact data to the database?
Regards, argi danu.
Okay, So if you are aware of the 'name' attributes for the request parameters, then you can simply use the create method as -
var contactData = req.allParams();
var name = contactData.name // attribute name
var number = contactData.number // attribute name
And after that simply call the create method
Contact.create({name : name, number :number}).exec(functionaddContact(err,createdContact){
// DO Something
});
But if you strictly want to use for loops to get all the data from request parameters, then Consider the following scenario -
Convert the combination of req parameters into a JSON object by appending the contactData into [ ] as
var json = '[' + contactData +']';
and then using the following code get the parameter values from the created JSON object as follows
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if(typeof obj[property] === 'string') {
console.log(property); // provides the property name
console.log(obj[property]); // provides the value of the property
}
}
May be this helps. It worked with SQL databases
The sailsjs model attribute method toJSON is very handy for processing model attributes before sending back to client. However, this method does not take any parameters and I cannot pass additional information to the method that can be used for processing attributes.
In my particular case, I need to check if I should send an attribute by comparing logged in user id (req.user.id). E.g.
toJSON: function () {
var obj = this.toObject();
var result = {};
if (req.user.id === 123) { // How can I access req from inside toJSON?
// do something here ...
}
}
I could not find a way to access the req parameter from inside toJSON. Any suggestions?
Sails does purposely not expose the req attribute as a global so there is no way to access it from within toJSON:
How to access session variables outside controller
Depending on the functionality you are trying to achieve you might be able to use policies instead: How to access request object in sails js lifecycle callbacks
In a similar case, I found myself using a workaround instead. You can add the id of the logged in user in your controller to the model:
YourModel.findOne().exec(function (err, yourModelObject) {
yourModelObject.loggedInUserId = req.user.id;
res.send(yourModelObject);
});
It is then accessible in your toJSON method and you could use it like this:
toJSON: function () {
var obj = this.toObject();
var result = {};
if (obj.loggedInUserId === 123) {
// do something here ...
}
delete obj.loggedInUserId;
return obj;
}
It is a little bit of a hack, but it worked for me. Be careful that a user can't somehow set the same value on the model (for example by using schema: true in your model) in question, to prevent security holes in your code.
If I assigned the res (result) object to a module level property, will it be unique for each request, or could a secondary request that is started before the callback finishes overwrite it?
var moduleData = {};
router.route('/publishers/:id')
.get(function(req, res) {
var id = req.params.id;
// Assigning the res property to a module level property called `moduleData`
moduleData.res = res;
db.findById('publishers', id, function(error, publishers) {
someFurtherWork(publishers); // would rather not pass the res object around
});
});
function someFurtherWork(publishers) {
someWork(publishers, function(error, data) {
// NOW we send the data back to user... is `res` guranteed to be the same one that initiated the request?
moduleData.res.send(data);
});
}
The router is itself event driven, meaning that each .get() request is handled as a callback to the server, when the thread is available. This guarantees each response object is unique to the function. Will a module level property be overwritten by a second GET request here?
If so, what workarounds are there, because I don't want to pass around my response object to multiple chained callback functions, that don't use them until all the data is collected.
There's a good reason I'm doing this, because if I have a bunch of callbacks, and once they're all done, then I need my response object. I figured if I used the response as a module level property, it won't need to be passed around.
The unique scope of each request your app receives is the same as the scope of the first callback you provide to the router, any variable that is defined in the outer scope is global to all requests.
You have to move the declaration of your var ModuleData = {} to the top of the callback. And because you defined someFurtherWork(publishers) function inside the same callback then it will have access via the closure to ModuleData object and you can just use it.
And if what you only need is to have the res object available to your nested callback and function so you can just use it as long as they all have a common root scope which is the initial callback function. Aren't closures awesome!