Displaying data synchronously using Ember.js

I am trying to display a list of images synchronously from urls using Ember.js

For a preliminary idea on how to start using Ember.js please refer to my earlier post
Ember.js – Binding a simple string of data

what we want to achieve ?

I have a set of images which I get from outside urls.
I will bind those images to the front end view (HTML) using ember.js.
The images are fetched synchronously.

Prerequisites

  1. We need to download files from – http://emberjs.com/
  2. We need 3 files from ember site
    • ember-1.5.1.js
    • handlebars-1.1.2.js
    • jquery-1.10.2.js

How to set Up the example

  1. Please create a folder photos in main directory where you want to run the example. In my case it is (c:/wamp/www/photos)
  2. Create a file index.html under photos folder
  3. Create a subfolder js under photos. Create another subfolder libs under js(c:/wamp/www/photos/js/libs)
  4. Now save the 3 files ember-1.5.1.js,handlebars-1.1.2.js, jquery-1.10.2.js under js/libs
  5. Create a javascript file application.js under js folder (c:/wamp/www/photos/js/application.js)
  6. Create a javascript file router.js under js folder (c:/wamp/www/photos/js/router.js)

Now paste the following code inside index.html

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Ember.js •Displaying images synchronously</title>  
  </head>
  <body>  	     
   <section id="main">
        <ul style="list-style-type:none;">
          <!-- TODO: SHOW LIST OF IMAGES HERE -->
        </ul>
     </section> 
   
  <script src="js/libs/jquery-1.10.2.min.js"></script>
  <script src="js/libs/handlebars-1.0.0.js"></script>
  <script src="js/libs/ember.js"></script> 
  <script src="js/application.js"></script>
  <script src="js/router.js"></script>  
  </body>
</html>

We have created a bare bones html page with only the references to the necessary javascript files.
There is a placeholder of type list which will finally display all the images

Now paste the following code inside application.js

window.App = Ember.Application.create();

This would create a new ember application with namespace ‘App’

Now paste the following code inside router.js

App.Router.map(function(){
	this.resource('photos', {path:'/'});
})

App.PhotosRoute = Ember.Route.extend({
  model: function() {
    return [{
      title: "Tomster",
      url: "http://emberjs.com/images/about/ember-productivity-sm.png"
    }, {
      title: "Eiffel Tower",
      url: "http://emberjs.com/images/about/ember-structure-sm.png"
    }];
  }
});

This does 2 things

Maps ember to data-template-name = photos when user hits the root url(index.html)
creates a new class PhotosRoute from Ember.Route class and binds the template to a model which has a list of images. Each image has a title and a url

Finally we have to add handlebar template tags to bind the view (HTML) to the model. So inside index.html, we wrap the body section element with handlebar template script tags

and add {{#each}} attribute to iterate over each list element, and also a {{bind-attr src=url}} tag to display the actual image url for each image

So index.html now looks like this

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Ember.js •Displaying images synchronously</title>  
  </head>
  <body>
  	 <script type="text/x-handlebars" data-template-name="photos">      
	 <section id="main">
        <ul id="todo-list" style="list-style-type:none;">
           {{#each}}	
          <li>
            <img {{bind-attr src=url}} />
          </li>
		    {{/each}}
        </ul>
     </section> 
   </script>
	<script src="js/libs/jquery-1.10.2.min.js"></script>
  <script src="js/libs/handlebars-1.0.0.js"></script>
  <script src="js/libs/ember.js"></script> 
  <script src="js/application.js"></script>
  <script src="js/router.js"></script>  
  </body>
</html>

Now you can look at the page. In my case it is
C:/wamp/www/photos/index.html

The page should look like the demo below:

Ember.js •Displaying images synchronously

****************************************************************

Ember.js – Binding a simple string of data

Binding a Simple String of Data using Ember.js

In the following post I will bind a simple string (firstname and lastname) to HTML using ember.js.
My target is to show-

  • What libraries to use for Ember.js
  • How to familiarize oneself with ember.js

We will download files from emberjs.com

Basic features of ember.js

Ember.js is a javascript MVC framework.
It has 4 parts

  1. Model – stores the data
  2. Template – binds data from the model and displays to the user(HTML)
  3. Controller – gets input from template or model and updates the model or template respectively
  4. Router – routes the url to the respective template, also routes the model to the respective template

How To Start

Please go here – http://emberjs.com/
Click on ‘Download the Starter Kit’ and download ‘starter-kit-1.5.1.zip’
There are 3 js files we need starter-kit-1.5.1 -> js -> libs

  • ember-1.5.1.js
  • handlebars-1.1.2.js
  • jquery-1.10.2.js

How to set up our application

Inside the main folder where you want to create the application (in my case it is c:/wamp/www)

  1. Create a new folder – Ember-Tutorial-1
  2. Create an index.html file inside it (c:/wamp/www/Ember-Tutorial-1/index.html)
  3. Create a js folder and then a libs folder under js folder (c:/wamp/www/Ember-Tutorial-1/js/libs)
  4. Save the 3 js files (ember-1.5.1.js,handlebars-1.1.2.js,jquery-1.10.2.js) under
    js/libs (c:/wamp/www/Ember-Tutorial-1/js/libs)

Now paste the following code inside index.html

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Ember.js Binding string of data</title>
    <style>
    	div{
    		margin:200px 100px;
    	}
    </style>
  </head>
  <body>  	
  	   <div>	
           Hello, <strong>FirstName LastName</strong>!
	   </div>
  <script src="js/libs/jquery-1.10.2.min.js"></script>
  <script src="js/libs/handlebars-1.0.0.js"></script>
  <script src="js/libs/ember.js"></script>
  <script src="js/application.js"></script>
 
  </body>
</html>

We will bind ‘firstname’ and ‘lastname’ with actual values

Now create a javascript file application.js under js folder
(c:/wamp/www/Ember-Tutorial-1/js/application.js)
and paste the following code

// Test1 is the namespace for the application
window.Test1 = Ember.Application.create();

// defines  class ApplicationController by extending
// ember ApplicationController class
Test1.ApplicationController = Ember.Controller.extend({
  firstName: "Ujjaini",
  lastName: "Mukherjee"
});

The application.js file –

  1. creates a namespace Test1 for the whole application
  2. creates a new class ApplicationController by extending Ember.Controller class
  3. This controller hardcodes firstname and lastname so that they can be bound to the HTML
  4. In normal cases , Controllers contain the application logic and will not contain data.
    Data is contained in the model. But only for this example I have hardcoded these values

Now go to index.html
and inside body tags wrap the HTML code by using handlebar template tag.
Also, replace the FirstName and LastName using dynamic {{firstName}} and {{lastName}}

The code looks like this

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Ember.js Binding string of data</title>
    <style>
    	div{
    		margin:200px 100px;
    	}
    </style>
  </head>
  <body>
  	 <script type="text/x-handlebars">
  	   <div>	
           Hello, <strong>{{firstName}} {{lastName}}</strong>!
	   </div>
	</script>

  <script src="js/libs/jquery-1.10.2.min.js"></script>
  <script src="js/libs/handlebars-1.0.0.js"></script>
  <script src="js/libs/ember.js"></script>
  <script src="js/application.js"></script>
 
  </body>
</html>

Now check the page.In my case it is -C:/wamp/www/Ember-Tutorial-1/index.html

If everything runs correctly, you should be able to see

Hello Ujjaini Mukherjee

Please see the actual demo here

Ember.js Binding string of data

Please Note: In this application I am only using template and controller. This is because we are only trying to display a string value. I am trying to get new users to familiarize themselves with ember.js. But for actual applications all 4 features – model, application, template and router needs to be used