ReactJS and friends
Meteor
The origin
meteor create search-box
meteor
http://localhost:3000
Inspecting the generated code
Changing the folder structure
- client (templates/stylesheets)
-lib/collection
-public
-server
Messing around with the
original files...
Changing the folder structure
-Create Layout file.
<template name="layout">
{{>yield}}
<hr/>
<div class="container">
<footer>
<p>© The Amazing Search Box. ·</p>
</footer>
</div>
</template>
Changing the folder structure
-Rename search-box.html to home.
<template name="home">
<button>Click Me</button>
<p>You've pressed the button {{counter}} times.</p>
</template>
Changing the folder structure
-Rename search-box.js to home.
Template.home.helpers({
counter: function () {
return Session.get('counter');
}
});
Template.home.events({
'click button': function () {
// increment the counter when button is clicked
Session.set('counter', Session.get('counter') + 1);
}
});
Changing the folder structure
-Create main.html.
<head>
<title>The amazing Search Box</title>
</head>
Changing the folder structure
-Create app.js.
if (Meteor.isClient) {
// counter starts at 0
Session.setDefault('counter', 0);
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
Changing the folder structure
-Rename search-box.css to home.css.
body {
padding-top: 50px;
}
Let's add some packages
Navigate to the page
meteor add iron:router
meteor
lib/router.js:
Router.configure({
layoutTemplate : 'layout'
});
https://atmospherejs.com/
Adding route to our home template:
Nice Juan, we are just were we started:)
Router.route("/", {
name: "home"
});
Let's add a search-box:
https://gist.github.com/javflores/5a962facb34d5f585838
<template name="searchBox">
<div class="container">
<div class="row">
<div class="span12">
<form id="simpleSearch" class="form-inline">
<input type="text" name="first-name" id="first-name" class="span5" placeholder="First Name">
<input type="text" name="last-name" id="last-name" class="span5" placeholder="Last Name">
<input type="text" name="yob" id="yob" class="span5" placeholder="Year Of Birth">
<input type="text" name="dob" id="dob" class="span5" placeholder="Date Of Birth">
<button type="submit" class="btn btn-primary" value="Search"><i class="fa fa-search fa-fw"></i> Search!</button>
</form>
</div>
</div>
</div>
</template>
<template name="home">
<h1>Let's do some searches</h1>
{{> searchBox}}
</template>
Almost right, need to add font-awesome
Any idea?
Almost right, need to add font-awesome
https://atmospherejs.com/fortawesome/fontawesome
meteor add fortawesome:fontawesome
Let's read search parameter:
Template.searchBox.events({
'submit form': function(event){
event.preventDefault();
var searchParams = {
firstName: event.target[0].value,
lastName: event.target[1].value,
yob: event.target[2].value,
yod: event.target[3].value
};
}
});
Saving our searches...
Template.searchBox.events({
'submit form': function(event){
event.preventDefault();
var searchParams = {
firstName: event.target[0].value,
lastName: event.target[1].value,
yob: event.target[2].value,
yod: event.target[3].value
};
Searches.saveSearch(searchParams);
}
});
Our first collection: Searches. Let's create one
Searches = new Mongo.Collection("searches");
Searches.saveSearch = function(searchParams) {
return Searches.insert(searchParams);
};
Both on the client and on the server.
That is a security risk!
Solution: publish/subscribe
<template name="home">
<h1>Let's do some searches</h1>
{{> searchBox}}
{{> recentSearches}}
</template>
Let's display our recent searches...
Challenge 1.
Display 10 searches
Let's display our recent searches...
Challenge 1.
<template name="recentSearches">
<div class="container">
<div class="row">
<div class="span12">
{{#each searches}}
<h4><strong>Search: </strong>FirstName: {{firstName}}, LastName: {{lastName}}, YearOfBirth: {{yob}}, YearOfDeath: {{yod}}</h4>
{{/each}}
</div>
</div>
</div>
</template>
Tip 1.
Let's display our recent searches...
Challenge 1.
Template.recentSearches.helpers({
searches: function () {
????
}
});
Tip 2.
Let's display our recent searches...
Open two browsers with the app.
Search in one of then, see how the data is refreshed automatically.
Continuous Development
Time to Deploy
meteor deploy my_app_name.meteor.com
Doing some searches...
To be continued...