How to do data binding in polymer with meteor - data-binding

I'm very new to data binding and the two frameworks. Right now I'm pretty stuck at how to bind the data within a polymer element.
For example, I have a book list with books' name. If I only use blaze to do the rendering, I would do it in the follow way:
//app.js
Template.bookList.helpers({
books: function () {
return Books.find({});
}
});
//app.html
<template name="bookList">
<h1>List</h1>
<ul>
{{#each books}}
{{> book}}
{{/each}}
</ul>
</template>
<template name="book">
<li>{{name}}</li>
</template>
Now I'm using it with polymer, I do:
//my-book-list.html
<polymer-element name="my-book-list">
<template>
<h1>List</h1>
<content></content>
</template>
</polymer-element>
//app.html
<template name="bookList">
<my-book-list>
<ul>
{{#each books}}
{{> book}}
{{/each}}
</ul>
</my-book-list>
</template>
<template name="book">
<li>{{name}}</li>
</template>
So I place the dynamic data inside of the polymer item through the content block. Although it still does the job, I don't want it that way. I want to do the data-binding inside the polymer element, something like(I hope it makes sense to you):
//my-book-list.html
<polymer-element name="my-book-list">
<template bind="{{books}}">
<h1>List</h1>
<ul>
<template repeat>
<li>{{name}}</li>
</template>
</ul>
</template>
</polymer-element>
//app.html
<template name="bookList">
<my-book-list></my-book-list>
</template>
Is there a way to do it? Thanks in advance.
Progress:
I now can put books purely inside the polymer element, the problem now is that it doesn't seem to react when the data change because polymer doesn't observe change of a object, and I am struggling in finding a way to observe all the nested values inside a object:
<polymer-element name="my-book-list">
<template bind="{{books | mapBooks}}">
<h1>List</h1>
<ul>
<template repeat>
<li>{{name}}</li>
</template>
</ul>
</template>
<script>
Polymer("my-book-list", {
books: Books.find(),
mapBooks : function(booksCursor) {
return booksCursor.map(function(p) { return {id: p.id, name: p.name}})
}
});
</script>
</polymer-element>

Finally got a hacky solution, but I don't know if this is the best practice or not:
<polymer-element name="my-book-list">
//Force polymer to update DOM when books.lastUpdate change
<template bind="{{books.lastUpdate | getBooks}}">
<h1>List</h1>
<ul>
<template repeat>
<li>{{name}}</li>
</template>
</ul>
</template>
<script>
Polymer("my-book-list", {
ready: function() {
var books = this.books;
this.books.observeChanges( //Observe the change of cursor and update a field
{
added: function(id, fields) {
console.log("Item added");
books.lastUpdate = new Date();
},
changed: function(id, fields) {
console.log("Item changed");
books.lastUpdate = new Date();
},
removed: function(id, fields) {
console.log("Item deleted");
books.lastUpdate = new Date();
}
}
),
books: Books.find(),
getBooks : function() {
return Books.find().map(function(p) { return {id: p.id, name: p.name}})
}
});
</script>
</polymer-element>

Related

Flow Router Footer Flashes to Top Before Data Loads

I'm loading a grid of data with flow router, but when I view the page the footer always flashes to the top before the data loads. What is the best way to fix this?
Here is the route:
AdminRoutes.route('/dashboard', {
name: 'adminDashboard',
action() {
BlazeLayout.render('AppLayout', {page: 'AdminDashboard'});
}
});
Here is the js file:
import { Template } from 'meteor/templating'
import Stores from '../../../../api/stores/stores.js'
import './AdminDashboard.html'
Template.AdminDashboard.onCreated(function() {
var self = this;
self.autorun(function() {
self.subscribe('stores.names.links');
});
});
Template.AdminDashboard.helpers({
stores: function () {
return Stores.find();
}
});
Here is the html layout file:
<template name='AppLayout'>
{{#if Template.subscriptionsReady}}
{{> Header }}
{{> Template.dynamic template=page}}
{{> Footer }}
{{/if}}
</template>
Here is the dashboard html file:
<template name='AdminDashboard'>
<div class='admin-dashboard-page'>
<section class='stores-grid'>
{{#each stores}}
<div class='store'>
<h2 class='store-name'>{{name}}</h2>
<a href='/admin/dashboard/{{link}}' class='store-button'>Edit</a>
</div>
{{/each}}
</section>
</div>
</template>
I'd try displaying the footer AFTER .stores-grid loads. Create a reactiveVar with a handler that is set to true when your data is loaded and return it's value on a helper, then wrap the footer in an if block on your template.
It would be something like the following...
First create a reactiveVar with a value of false:
Template.AdminDashboard.onCreated(function() {
this.isDataLoaded = new ReactiveVar(false);
var self = this;
self.autorun(function() {
self.subscribe('stores.names.links');
});
});
Set the value to true when the collection is loaded:
Template.AdminDashboard.helpers({
stores: function () {
let data = Stores.find()
if(data) {
Template.Instance().isDataLoaded.set(true)
}
return data ;
},
dataLoaded: function () {
return Template.Instance().isDataLoaded.get();
}
});
Finally, wrap your footer so it's only displayed after data is loaded:
<template name='AppLayout'>
{{#if Template.subscriptionsReady}}
{{> Header }}
{{> Template.dynamic template=page}}
{{#if dataLoaded}}
{{> Footer }}
{{/if}}
{{/if}}
</template>
A simple fix would be to set a minimum height on the div your content will be loaded into, pushing the footer down while the content loads. This may or may not work for you, depending on what the expected height of your content will be.
You could also install a loading screen/animation to hide the footer while the data loads.

Meteor blaze, Use helper / variable in Content Blocks (contentFor)

I have a template (navBarContent) that I is passed a "title" when I insert it.
I am then able to access {{title}} within that template, but it is not possible to access it within a {{#contentFor}} block embedded in the navBarContent template:
<template name="map">
{{>navBarContent title="MAP"}}
... other content ...
<template>
<template name="navBarContent ">
{{title}}
{{#contentFor "headerTitle"}}
<h1 class="title">{{title}}</h1>
{{/contentFor}}
</template>
I already tried to "forward" the title:
<template name="navBarContent ">
{{title}}
{{#contentFor "headerTitle" title="MAP"}}
<h1 class="title">{{title}}</h1>
{{/contentFor}}
</template>
which produces the following error:
First argument must be a function, to be called on the rest of the arguments;
EDIT:
Ok. I think the data scopes are a the following:
<template name="layout">
{{> yield "headerTitleRenderedInLayout"}}
{{> yield}}
</template>
<template name='map'>
{{> yield "headerTitleRenderedInTemplate"}}
{{>navBarContent title="PARAMETER_TITLE"}}
</template>
<template name="navBarContent">
{{title}} <!-- output: PARAMETER_TITLE -->
{{#contentFor "headerTitleRenderedInLayout"}}
<h1 class="title">{{title}}</h1> <!-- output: LAYOUT_DATA_TITLE -->
{{/contentFor}}
{{#contentFor "headerTitleRenderedInTemplate"}}
<h1 class="title">{{title}}</h1> <!-- output: TEMPLATE_DATA_TITLE -->
{{/contentFor}}
</template>
Above outputs are produced when I use the following router options:
Router.route('/map', function () {
this.layout("layout", {
data: function() {
return { title: "LAYOUT_DATA_TITLE" }
}
});
this.render('map', {
data: function() {
return { title: "TEMPLATE_DATA_TITLE" }
}
});
});
My app has a navbar that is defined in my main layout and I therefore need to set the datacontext for the layout in my route. So far so good, but I want to set that data context based on the value that I pass via:
{{>navBarContent title="PARAMETER_TITLE"}}
This is just a cosmetic thing, because I prefer to define my navbar content in the different templates rather than the routes.
It works when you pass the data context to the render function because Iron Router handles the contentFor block:
Router.route('/', function() {
this.render('map', {
data: function() {
return { title: 'MAP' }
}
});
});
Then the following template shows MAP twice:
<template name="map">
{{> navBarContent }}
{{> yield 'anotherBlock'}}
</template>
<template name="navBarContent">
<div>
In "map": {{title}}
</div>
{{#contentFor 'anotherBlock'}}
<div>
In contentFor: {{title}}
</div>
{{/contentFor}}
</template>

Meteor using Template.registerHelpers,

I am trying to combine 2 beginner classes together (https://www.meteor.com/tutorials/blaze/templates & http://meteortips.com/second-meteor-tutorial/iron-router-part-1/). I have been able to complete both classes individually with no problem.
I am using Iron:Router to route to various templates. I run in to issues when I try to access a template within a template. The proper results do not get displayed on the " aostasks"page.
.js:
Router.route('aostask', {
template: 'aostask'
});
Tasks = new Mongo.Collection("tasks");
if (Meteor.isClient) {
// This code only runs on the client
Template.registerHelpers('task', function () {
return tasks.find({});
})
};
.html:
<template name= "aostask">
<head>
<title>Todo List</title>
</head>
<body>
<div class="container">
<header>
<h1>Todo List</h1>
</header>
<ul>
{{#each tasks}}
{{> task}}
{{/each}}
</ul>
</div>
</body>
</template>
<template name="task">
<li>{{text}}</li>
</template>
Result:
A page with the correct headers.However, it does not list the tasks i have in my mongo collection. Is the issue that i am using the registerHelper function incorrectly? Should i be using some other function?
Thanks in advance.
There is typo in your helper name
Template.registerHelper('tasks', function () {
return tasks.find({});
})
};
tasks not task
Your helper is registered with the same name as your template. It should be just 'task'. Template.registerHelpers should be Template.registerHelper
if (Meteor.isClient) {
// This code only runs on the client
Template.registerHelper('task', function () {
return Tasks.find({});
})
};

Meteor: Blaze.Each example

How can one use Blaze.Each? For example, I'd like to emulate this:
{{#each Items}}
{{this}}
{{/each}}
in JS. I need to keep reactivity.
Something like:
Blaze.Each(Items.find(), function(item) {
console.log(item);
});
I don't get previous answer
This
html
<template name="yourTemplate">
<div id="each-area">
</div>
</template>
js
Template.yourTemplate.onRendered(function () {
var renderableContent = Blaze.Each(Collection.find({}), function(){
return Template.templateToRender;
});
Blaze.render(renderableContent, this.find("#each-area"));
});
Is complete equivalent of this:
html
<template name="yourTemplate">
<div id="each-area">
{{#each data}}
{{>templateToRender}}
{{/each}}
</div>
</template>
js
Template.yourTemplate.helpers({
"data": function(){
return Collection.find({})
}
});
this.autorun(function(){
var items = Items.find();
_.each(items.fetch(), function(item) {
console.log(item);
});
});
This works. It console.logs everytime an Item is added.
w/o underscore and autorun:
Blaze.Each(Data.find().fetch(), function(item){
console.log(item)
})

Handlebars.js - how to pick a "logout" value and append to "div" element

In the Handlebars.js - template i am getting a senario to check the value of the array, and if the value is not equal to do a task and equal to do a taks.. i do like this, But it thrown a error, how to handle this scenario.. any one help me?
Or any one give the way to handle this properly.
myJson would be :
{
"links":[{"label":"x","link":"x"},
{"label":"y","link":"y"},
{"label":"Logout","link":"Logout"}]
}
{{#each links}}
{{#if !lable.Logout}}
<li>
{{label}}
{{#if subLinks}}
<ul>
{{#each subLinks}}
<li>{{label}}</li>
{{/each}}
</ul>
{{else}}
<div>{{label}}</div>
{{/if}}
{{/each}}
Handlebars alone cannot handle conditional if statements, you need to use a helper function.
<div id="myDiv"></div>
<script type="text/x-handlebars-template" id="handlebar">
{{#each links}}
{{#ifCond label "Logout" }}
<li>
{{label}}
{{#if subLinks}}
<ul>
{{#each subLinks}}
<li>{{label}}</li>
{{/each}}
</ul>
{{/if}}
{{else}}
<div>
{{label}}
</div>
{{/ifCond}}
{{/each}}
</script>
<script>
$( document ).ready(function() {
var source = $("#handlebar").html();
var template = Handlebars.compile(source);
var context = {
"links":[
{"label":"x","link":"x"},
{"label":"y","link":"y"},
{"label":"Logout","link":"Logout"}
]
};
var html = template(context);
$("#myDiv").html(html);
});
Handlebars.registerHelper('ifCond', function(v1, v2, options) {
if(v1 === v2) {
return options.fn(this);
}
return options.inverse(this);
});
</script>

Resources