How to fetch and display the data from the database? - mongodb

Am trying to fetch and display the items from my collection.
I created the template and gave it the design for each item. Here is the code:
<template name="list_products">
{{#each applications}}
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img src="http://placehold.it/320x150" alt="">
<div class="caption">
<h4 class="pull-right">{{price}}</h4>
<h4>{{title}}
</h4>
<p>{{description}}</p>
</div>
</div>
</div>
{{/each}}
</template>
on the .js file, i created the applications that will return all the items in the collection
Template.list_products.applications = function(){
Products.find();
}
then i called the template in the .html file
{{> list_products}}
I got this error once i run "npm run start"
Refused to execute script from 'http://localhost:3000/js/jquery.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled
what am i doing wrong? any steps am missing here?

Do it by using a helper.
Template.list_products.helpers({
applications:function(){
return Products.find({});
}
});
Again you could iterate through the applications because it will return a cursor. It is possible also to return Products.find({}).fetch();
But first try with the cursor in order to see if it works.

Related

Functionality of data-sly-test for given example in image

Please find the below snippet. Can any one explain above functionality for the data-sly-test. How it will work condition here for image??
<div class="spon-image-container col-12 col-md-4">
<sly data-sly-test="${properties.fileReference}">
<img class="spon-_image" src="${properties.fileReference}"/>
</sly>
</div>
There are a few things to mention here. The gist of the code snippet is, that the <img> tag will only be rendered if {$properties.fileReference} is not empty.
Be aware, there is no sanity check involved here. data-sly-test won't check if the referenced file exists etc.
So assume that ${properties.fileReference} equals /content/dam/myImage.png. Then the resulting HTML would like this:
<div class="spon-image-container col-12 col-md-4">
<img class="spon-_image" src="/content/dam/myImage.png"/>
</div>
On the other hand, if the ${properties.fileReference} is empty (or null) you get the following HTML:
<div class="spon-image-container col-12 col-md-4">
</div>
Depending on your HTML/CSS/JS you might not want that to happen. So you could improve your code to include the data-sly-test statement in the <div> tag:
<div class="spon-image-container col-12 col-md-4" data-sly-test="${properties.fileReference}">
<img class="spon-_image" src="${properties.fileReference}"/>
</div>
This way, the <div> is only rendered, if a fileReference is set. But even if you still want the <div> to appear, your code can be improved by removing the <sly> element and adding the data-sly-test to the <img> tag:
<div class="spon-image-container col-12 col-md-4">
<img class="spon-_image"
src="${properties.fileReference}"
data-sly-test="${properties.fileReference}"/>
</div>
As mentioned in the specification, data-sly-test:
Keeps or removes the element depending on the attribute value.
For your case if fileReferece property evaluates to true (not null, not empty), it will render:
<div class="spon-image-container col-12 col-md-4">
<img class="spon-_image" src="....."/>
</div>
Note that the sly tag unwraps/removes itself, it's actually unnecessary here as the data-sly-test attribute could be moved to the img.
If fileReference evaluates to false, it will render:
<div class="spon-image-container col-12 col-md-4">
</div>
It basically checks if the current resource properties(i.e component properties) contain fileRefernce then it will add an image tag.

how to check handlebar-template completely rendered

i have a handlebar-template in B.phtml file like this:
<div class="conversation-header" id="conversation-header" >
<div class="avatar">
<a href="{{profileUrl}}">
{{> avatar showHardcore=false}}
</a>
</div>
</div>
This template is going to append A.html file which have been add eventlistener 'load' then will do something.
But it can't check exactly when the handlebars-template completely rendered.
May i have a callback for B.phtml or sth like that and can i write this on A.phtml ?

ng-flow define maximum number of files

I am using ng-flow in my app, is there any way to limit the number of files for upload?
A code sample:
<div class="thumbnail" ng-show="$flow.files.length">
<img flow-img="$flow.files[0]" />
</div>
<div>
Select image
Change
<a href="#" class="btn btn-danger" ng-show="$flow.files.length"
ng-click="$flow.cancel()">
Remove
</a>
</div>
The attribute flow-file-added is expecting a boolean value.
You can put all your conditions in there.
$flow.files.length returns the number a files you have already added
For example:
<div flow-init flow-files-submitted="$flow.upload()" flow-file-added="$flow.files.length<3"></div>

Is it possible to implement dynamic form input rows (adding new row by clicking on Add) with Handlebars and Meteor?

I'm getting my hands dirty with Meteor, and I wanted to port this AngularJS app (http://simplydo.com/projector/) over as an exercise.
I'm having difficulty implementing adding dynamic input form rows to sections using Handlebars, and I've found no documentation anywhere that documents if this possible. It's a snap in Angular using ng-repeat, but I want to confirm if this is something that is possible in Handlebars or whether I need to use Jquery in order to achieve this.
Thanks in advance.
It's just as easy in Meteor
all you have to do is repeat the rows with {{#each rowData}} and have a button that adds a document to the collection. Here is an example:
In this template the rows for a page are shown. In order to add a row, the user has to click on the add image. The template is :
<template name="page">
{{#with page}}
<h3>{{title}}</h3>
{{#each rows}}
<div class="row-fluid page-row {{#if isSelected this}}selected-row{{/if}}">
... page content here
</div>
{{/each}}
{{/with}}
<div class="btn-toolbar">
<div class="pull-right">
<a id="add-row" href="#" data-toggle="tooltip" title="{{lbl 'add-page'}}">
<img src="/images/add.png" class="asset-icon"/>
</a>
<img src="/images/separator.png" class="asset-icon"/>
<a id="delete-row" href="#" data-toggle="tooltip" title="{{lbl 'delete-page'}}">
<img src="/images/delete.png" class="asset-icon"/>
</a>
</div>
</div>
</template>
the helpers for this template are:
Template.page.page = function () {
return Session.get("selected-page");
}
in order to add a page the click event for the add button is implemented:
"click #add-row": function (evt, template) {
var page = Session.get("selected-page");
Pages.update({_id: page._id}, ... add a new row here ...)
}
because the whole thing is reactive, the list of rows will redraw after the update on the Pages collection.

Liftweb eager_eval and inserting html from db

I need to execute snippets like:
<div class="lift:firstSnippet.content?eager_eval=true">
<p>Some text</p>
<div class='lift:secondSnippet.showAddNewForm>'></div>
</div>
So in my template I have
<div class="lift:firstSnippet.content?eager_eval=true"></div>
FirstSnippet insert some html from db:
def content = "*" #> Unparsed(page.open_!.content.is)
That html looks like:
<p>Some text</p><div class='lift:secondSnippet.showAddNewForm>'></div>
But SecondSnippet doesnt execute. I also tried to use S.eagerEval(Unparsed(page.open_!.content.is))
but result is same. I can't figure out why.
I don't know if you copied your template code over or re-typed it, but there's a syntax error:
<div class="lift:firstSnippet.content?eager_eval=true">
<p>Some text</p>
<div class='lift:secondSnippet.showAddNewForm>'></div>
</div>
Note the > at the end of secondSnippet.showAddNewForm>
I think it should read:
<div class="lift:firstSnippet.content?eager_eval=true">
<p>Some text</p>
<div class='lift:secondSnippet.showAddNewForm'></div>
</div>
Please try that and see if it makes a difference.