What is the use of the script mentioned? - meanjs

working on the sample meanjs application. I am not able to understand the below code snippet.
Kindly explain the use of this script in layout.server.view.html view.
<!--Embedding The User Object-->
<script type="text/javascript">
var user = {{ user | json | safe }};
</script>

Basically user is being sent via express/swig everytime a request is made to the route /* (see here) so that user data can be used by AngularJS.
If you check the file core.server.controller.js (here), you will see that MEAN.JS passes a user object in the response in this code block:
res.render('modules/core/server/views/index', {
user: safeUserObject
});
Then in the code block you mentioned, you're assigning user object sent by express/swig to the variable user which can then be accessed later using $window.user similarly to what is done here.
json and safe are two swig filters. The first one returns a string representation of a JavaScript object and the second one forces the input to not be auto-escaped.

Related

How to create alert from view model

I want to validate file. While file is invalid, i want to refresh my page and inform user that he did not upload proper file. So i have this in my
views/campaign.py
try:
wb = load_workbook(mp_file)
except BadZipfile:
return redirect('campaign_add', client_id)
The only way i know how to do it is add another attribute to client class which will be
is_error(models.BooleanField())
And then change views/campaign to
try:
client.is_error = False
wb = load_workbook(mp_file)
client.save()
except BadZipfile:
client.is_error = True
client.save()
return redirect('campaign_add', client)
And with another attribute i can add in my campaign.html file some kind of if is.error is true i'm adding some kind of windows with information about bad file after reloading page. But is there any way to do it without adding another attribute?
Ok, let's imagine that the answer is a little bit complicated than you've expected.
Modern UI's are not reloading pages just to inform about some errors with user input or upload.
So what is the best user experience here?
User is uploading some file(s) from the page.
You are sending a file via JavaScript to the dedicated API endpoint for this uploading. Let's say /workbook/uploads/. You need to create a handler for this endpoint (view)
Endpoint returns 200 OK with the empty body on success or an error, let's say 400 Bad Request with detailed JSON in the body to show to the user what's wrong.
You're parsing responses in JavaScript and show the user what's wrong
No refreshes are needed. 🙌
But the particular answer will need more code from your implementation. (view, urls, template)

Grab value from class in deployed web app - Google App Script

I have a simple html form on a webapp, in which the user can input information
<form>
Field 1: <input class="pod" type="text" name="Field1" value="Test1"><br>
Field 2: <input class="Start-Date" type="text" name="start-date"
value="2019-03-29"><br>
</form>
Once the user inputs this information I simply need to grab it, which I can do on the HTML file through some jquery:
<script>
$(document).ready(function() {
var test = $(".pod").val();
});
</script>
So this works on the HTML file. I can't however get this to work on the .gs file and I am lost on how to accomplish that. I need the user input to run SQL and return some data afterward. All documentation I found was rather confusing and didn't me help much, so I hope that somebody can help me here.
Alternatively: How can you use a variable defined on the HTML file on the GS file? For instance, what if I grabbed the information through the example code on the html file, is there a way to call this variable (var test) on the GS file?
Thank you a ton,
Sascha
Grab your user input from frontend
Pass that to backend using run
Execute your SQL in backend using the user inputs received.
Return the result from backend
Receive response from backend in frontend using withsuccesshandlerfunction
Reference : How to send data from HTML form to Google Spreadsheet using JavaScript?

PUG with javascript

If I have the following piece of code
script.
function getProductParams(params) {
return JSON.parse(localStorage.getItem(params));
}
each product in products
-var getVariable = getProductParams("ids");
This piece of code doesn't work, I'm guessing that - is on server side, while script. is on clients?
How can I access a variable from localStorage in pug and use it for comparison with variables received from server.
I want to make something like this
script.
function getProductParams(params) {
return JSON.parse(localStorage.getItem(params));
}
each product in products
-var getVariable = getProductParams("ids");
-if (getVariable.includes(#{product._id}) {
// create the element in html
-} else {
- // call next product and compare if we have it
-}
The simple answer is that you can't do this. LocalStorage is exactly that - local. Your server can't directly access it unless you explicitly pass it back using some other format/method.
What your server can access easily is the cookie. If you store the data in the cookie and use cookie-parser then you can handle this list quickly. This use case is exactly why cookies exist.
Another option would be to pass the data to the server in the body of a POST request, but your question doesn't provide a lot of information as to what exactly you're doing and if this is an option or not.
It would also be much easier for you to accomplish all of the sorting and filtering in your route instead of the pug template. That way you have full request to the cookie, request body, etc. directly and you don't have to pass that down to the template too.
Then, when your template gets the list it's a really simple matter to render it:
each product in products
//create the element in html

AngularJs - passing parameters from grid result rows to external webpage

I am working on a webapp built upon angularjs.So,i have a search result UI built which upon search displays result in grid.Now,when user clicks on each row of the result ,an external webpage should be called with values of columns passed from that clicked grid row to the external webpage.
How can i do this,can i have a sample controller to refer upon.
Please help with this
Thanks,
Alex
If I understand you correctly, you want to send an AJAX call to some JSON api when a user clicks on some element. Here's a basic thing of how to do that in an angularjs controller:
angular.module("your.app")
.controller("ajaxingCtrl",["$http","$scope", function($http, $scope){
$scope.onSomeUserAction = function(argument){
//substitute the type of request your api requires
$http.get("http://your.remote.api/some/resource",
{
//params will get parsed into HTTP query parameters
params: {
x:arguments.x
}
}
).then(function(result){
console.log(result);// and then do what you will with the result
})
}
}])
You can find more information about doing ajax calls in angular's $http service docs.

Meteor: reading simple JSON file

I am trying to read a JSON file with Meteor. I've seen various answers on stackoverflow but cannot seem to get them to work. I have tried this one which basically says:
Create a file called private/test.json with the following contents:
[{"id":1,"text":"foo"},{"id":2,"text":"bar"}]
Read the file contents when the server starts (server/start.js):
Meteor.startup(function() {
console.log(JSON.parse(Assets.getText('test.json')));
});
However this seemingly very simple example does not log anything to the console. If I trye to store it in a variable instead on console.logging it and then displaying it client side I get
Uncaught ReferenceError: myjson is not defined
where myjson was the variable I stored it in. I have tried reading the JSON client side
Template.hello.events({
'click input': function () {
myjson = JSON.parse(Assets.getText("myfile.json"));
console.log("myjson")
});
}
Which results in:
Uncaught ReferenceError: Assets is not defined
If have tried all of the options described here: Importing a JSON file in Meteor with more or less the same outcome.
Hope someone can help me out
As per the docs, Assets.getText is only available on the server as it's designed to read data in the private directory, to which clients should not have access (thus the name).
If you want to deliver this information to the client, you have two options:
Use Assets.getText exactly as you have done, but inside a method on the server, and call this method from the client to return the results. This seems like the best option to me as you're rationing access to your data via the method, rather than making it completely public.
Put it in the public folder instead and use something like jQuery.getJSON() to read it. This isn't something I've ever done, so I can't provide any further advice, but it looks pretty straightforward.
The server method is OK, just remove the extra semi-colon(;). You need a little more in the client call. The JSON data comes from the callback.
Use this in your click event:
if (typeof console !== 'undefined'){
console.log("You're calling readit");
Meteor.call('readit',function(err,response){
console.log(response);
});
}
Meteor!