Cookbook
Introduction
Welcome to the Ember.js Cookbook! The Cookbook provides answers and solutions to common Ember questions and problems. Anyone is welcome to contribute.
Here are all of the available recipes:
Contributing
- Understanding the Cookbook Format
- Participating If You Know Ember
- Participating If You Don’t Know Ember
- Deciding If A Recipe is a Good Fit
- Suggesting A Recipe
User Interface & Interaction
- Adding CSS Classes to Your Components
- Adding CSS Classes to Your Components Based on Properties
- Focusing a Textfield after It’s Been Inserted
- Displaying Formatted Dates With Moment.js
- Specifying Data-Driven Areas of Templates That Do Not Need To Update
- Using Modal Dialogs
- Resetting scroll on route changes
Event Handling & Data Binding
Helpers & Components
- Creating Reusable Social Share Buttons
- A Spinning Button for Asynchronous Actions
- Adding Google Analytics Tracking
Working with Objects
- Incrementing Or Decrementing A Property
- Setting Multiple Properties At Once
- Continuous Redrawing of Views
If you would like to see more recipes, take a look at the Suggesting A Recipe section.
Contributing
The Ember Cookbook provides answers and solutions to common Ember questions and problems. Anyone is welcome to contribute.
If you are new to Ember, we recommend that you spend some time reading the guides and tutorials before coming to the Cookbook. Cookbook recipes assume that you have a basic understanding of Ember’s concepts.
If you have experience with Ember and would like to contribute to the Cookbook, the discussion section of each recipe is a great place to start.
Recipes
- Understanding the Cookbook Format
- Participating If You Know Ember
- Participating If You Don’t Know Ember
- Deciding If A Recipe is a Good Fit
- Suggesting A Recipe
User Interface & Interaction
Here are some recipes that will help you provide a better user experience.
- Adding CSS Classes to Your Components
- Adding CSS Classes to Your Components Based on Properties
- Focusing a Textfield after It’s Been Inserted
- Displaying Formatted Dates With Moment.js
- Specifying Data-Driven Areas of Templates That Do Not Need To Update
- Using Modal Dialogs
- Resetting scroll on route changes
Event Handling & Data Binding
Problem
You want to base the value of one property on the value of another property.
Solution
Use one of the computed property macros like Ember.computed.alias or Ember.computed.gte
1 App.Person = Ember.Object.extend({
2 firstName : null,
3 lastName : null,
4 surname : Ember.computed.alias("lastName"),
5 eligibleForRetirement: Ember.computed.gte("age", 65)
6 });
Discussion
Ember.js includes a number of macros that will help create properties whose values are based
on the values of other properties, correctly connecting them with bindings so they remain
updated when values change. These all are stored on the Ember.computed object
and documented in the API documentation
Example
Helpers & Components
Problem
You want to create a reusable Tweet button for your application.
Solution
Write a custom component that renders the Tweet button with specific attributes passed in.
1 {{share-twitter data-url="http://emberjs.com"
2 data-text="EmberJS Components are Amazing!"
3 data-size="large"
4 data-hashtags="emberjs"}}
1 App.ShareTwitterComponent = Ember.Component.extend({
2 tagName: 'a',
3 classNames: 'twitter-share-button',
4 attributeBindings: ['data-size', 'data-url', 'data-text', 'data-hashtags']
5 });
Include Twitter’s widget code in your HTML:
1 <script type="text/javascript" src="http://platform.twitter.com/widgets.js" id="\
2 twitter-wjs"></script>
Discussion
Twitter’s widget library expects to find an <a> tag on the page with specific data- attributes applied.
It takes the values of these attributes and, when the <a> tag is clicked, opens an iFrame for twitter sharing.
The share-twitter component takes four options that match the four attributes for the resulting <a> tag:
data-url, data-text, data-size, data-hashtags. These options and their values become properties on the
component object.
The component defines certain attributes of its HTML representation as bound to properties of the object through
its attributeBindings property. When the values of these properties change, the component’s HTML element’s
attributes will be updated to match the new values.
An appropriate tag and css class are applied through the tagName and classNames properties.
Example
Working with Objects
Here are some recipes to help you understand working with Ember Objects.