Can't display javascript object properties in mandrill template using handlebars - javascript

I am trying to display dynamic content by using the mandrill template api in a node project.
I have followed the docs and looked at plenty of examples, and for the most part can get things working.
However, when I try to access properties of an object that I pass through the api in the mandrill template, it does not display anything.
Here is my mandrill template (using handlebars):
<p>{{greeting}} {{person.firstName}},</p>
<p>{{greeting}} {{person.0.firstName}},</p>
<p>Your location is {{person.location}}.</p>
Now, the greeting does display the value passed in the global_merge_vars part. But the properties for the person object do not get displayed, as if they are undefined.
Here is part of the json being sent:
var greeting = "Hello ";
var person = {firstName:"testfname",location:"testlocation"};
var globalMergeVars = [
{"name": "greeting","content":greeting},
{"name": "person","content": person},
];
Am I not passing the object correctly or naming the 'name' property correctly in the api call? I have tried a bunch of different things. I know that I could create multiple vars inside the globalMergeVars object instead of passing the entire person object, however I have a lot more properties attached to the person object.
I have also successfully used an each loop for an array of items and that all gets displayed correctly.
Thanks.

Related

Multi element JSON object from JavaScript

I have a Javascript JSON object
const student = { name: "bob", age: 7, grade: 6 }
and I can pass it to my Web API using axios POST command by
JSON.stringify(student)
and I can build my student object by looping an array and passing in a value such as
let studentArr= []
const student = { name: studentArr[i]["name"], age: studentArr[i]["age"], grade: studentArr[i]["grade"}
I'm using i in the example as the index because it could have 100 students. As long as I pass in only one value for i, everything works fine. My question is how can I make it into a multi-element JSON object from my array. I've been spoiled by Newtonsoft.Json where I can pull data from a SQL database and create a JSON object. If I just JSON.stringify(studentARR) is shows empty. I want to pass to the Web API all of the students on one post so the Web API can make a document and download it back.
I seen many different ways of trying to accomplish this and the methods seem to change over time. Thanks for the help
Why do you have to JSON.stringify if you are using axios. The declaration of axios.post accepts URL as first parameter and the data which is formdata or the JSON object as second parameter
I'm pretty sure that you might not need to use JSON.stringify if you are using axios to post to a Web API
Example of using Axios post method https://axios-http.com/docs/post_example

Alfresco javascript get custom DataList's properties

I have written one rule(execute script) on datalist, so that whenever any new item is added, it should send an email to the respective user.
In email I want to add custom properties value e.g. employee_first_name
I've tried to get datalist using
var dataLists = siteName.getContainer("dataLists");
But it gives an error as follows:
"getContainer" method is not available.
The script given in Alfresco javascript get custom DataList works perfectly in Javascript console.
Your siteName variable is probably a string, which does not have a method called "getContainer". That's why you are seeing that message.
Here's a code snippet that fetches the data list container object given a site ID (site short name):
var siteId = "jtp-test-site-1";
var siteInfo = siteService.getSite(siteId);
var dataLists = siteInfo.getContainer("dataLists");
print(dataLists.name);
Notice the use of the built-in root-scoped object, siteService, that fetches the site info for a given site.
You can run that in the JavaScript Console and it will output the name of that folder, which is "datalists".

How to access a json object in javascript files from a model passed to the view

I have a JSON object in my database that contains html inside it. I pass the object as part of a model to my view. I need to access the object and read it in the javascript files for the page. However when i try and assign the object to a global variable in my html file i cannot access it in my javascript file.
I tried reading the object as a string it returns decoded html (
"page-1":) which i cant do anything with. If i call #Html.Raw(#Model.CourseContent.ExpectedResult) it created the JSON object as expected. However in my javascript file it is listed as undefined. I have no idea how to solve this.
#model DataTransferObjects.Models.UserCourseAndContent
<script>
var totalPages = '#Model.CourseContent.TotalPages';
var expectedResults = #HTML.Raw(#Model.CourseContent.ExpectedResult)
</script>
The json object that comes out when i use the above code looks like
var expectedResults = {
"page-1": "<head></head><body></body>",
"page-3": "<head></head><body><h1>My Cool News Article</h1><p>Welcome
to my news article, you’ll find some cool news here.</p>
<Our most recent
news</<p>Below you’ll find my most recent news!</p></body>"
};
I expected it to be an actual json string but instead ive got an object (?) i am confused as to how to decode the html out of it then turn the resulting json obejct into a json string to be read in the javascript file.
Any help would be great!
var expectedResults = {
"page-1": "<head></head><body></body>",
"page-3": "<head></head><body><h1>My Cool News Article</h1><p>Welcome
to my news article, you’ll find some cool news here.</p>
<Our most recent
news</<p>Below you’ll find my most recent news!</p></body>"
};
// Parse JSON
const parsedJSON = JSON.parse(JSON.stringify(expectedResults));
// Access to properties
const page-1 = parsedJSON['page-1'];
const page-3 = parsedJSON['page-3'];

Restangular: getList(), then get related information

I am trying to wrap my head around how this would be done.
Get a list of "Creations".
"Creations" list returns an ID of the "Author" of that creation.
Automatically do another Restangular call to grab that Authors information and relate it to the object returned by the first getList of creations.
Restangular.all('creations').getList({
limit: 5
}).then(function(creations) {
$scope.creations = creations;
// returns a list of Objects for each creation with one parameter being author:id (author:1,2,3, etc)
// THEN GET INFORMATION ABOUT EACH CREATION'S AUTHOR
// Restangular.one('users', creations.author).get().......?
});
Is there a better way of doing this so that in my view I can access something like $scope.creations.authorObject...

Databinding to Windows 8 ListView

I'm having massive problems with databinding to a ListView in a Windows 8 app using Javascript.
Inside the "activated" event on default.js I have written some code to get some data from a web service and push it into an array. This bit works OK and the array is populated.
The problem I have is that the app won't recognise the data. I have this code in a page called inspections.html:
data-win-options="{itemTemplate: select('#imageTextListCollectionTemplate'),
itemDataSource: dataList.dataSource,
layout: {type: WinJS.UI.ListLayout}}
and then in the "activated" event I declare:
var dataList = new Array();
and push the data from the web service into this array. But at runtime I get an error that says something along the lines of "can't find dataSource on undefined dataList".
I've done some of the examples on the MS website and in one of them it creates a dummy dataset and references it from a namespace. I kinda think that what I'm missing here is a namespace too but I don't know what the namespace for default.js is. Or maybe I'm wrong and it's something totally different.
Please help - this is so fundamental (and should be easy) but I can't get my head around it.
Do you want to create datalist in HTML or javascript?
It seems you want to create it from JavaScript. Assuming that you have already pushed your data into array from your webservice, you only need to call:
var dataList = new WinJS.Binding.List(array);
now accessing dataList.dataSource is perfectly valid.
Also, to create the datalist you don't always need an array. You could probably start with an empty list and then keep inserting data directly into the data list from web services, like:
var dataList = new WinJS.Binding.List([]);
dataList.push(value1);
dataList.push(value2);
...
Hope it helps. Let me know if you have any more questions.
If you are getting troubled by assigning datasource in HTML side
Prefer js side like
var list = new WinJS.Binding.List(array here);
listView.itemDataSource = list.dataSource;
by using this you can easily go through the data which you are binding to ListView.

Categories

Resources