the basics of ember objects

19
EMBER OBJECTS A guide to Ember Objects and their awesomeness.

Upload: jason-schmidt

Post on 13-Jul-2015

155 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: The basics of Ember Objects

EMBER OBJECTSA guide to Ember Objects and their awesomeness.

Page 2: The basics of Ember Objects

POJOS VS. EMBER OBJECTS

Page 3: The basics of Ember Objects

POJOS (PLAIN OLE' JAVASCRIPT OBJECTS) var Hunter = function() { this.isHappy = false; this.shootDeer = function() { var randomNumber = Math.floor(Math.random() * 10); if (randomNumber % 2 === 0) { this.isHappy = true; } }};

var jason = new Hunter();

jason.isHappy; // falsejason['isHappy']; // alt way of getting propertyjason.shootDeer(); // calling a methodjason.isHappy; // true or falsejason.isHappy = true; // true

Page 4: The basics of Ember Objects

EMBER OBJECTS var Hunter = Ember.Object.extend({ isHappy: false, shootDeer: function() { var randomNumber = Math.floor(Math.random() * 10); if (randomNumber % 2 === 0) { this.set('isHappy', true); } }});

var jason = Hunter.create();

jason.get('isHappy'); // falsejason.shootDeer();jason.get('isHappy'); // true or falsejason.set('isHappy', true); // true

Page 5: The basics of Ember Objects

EXAMPLES OF EMBER OBJECTS IN OUR APPSShow examples from Employer app.

RoutesControllersViewsComponents

Page 6: The basics of Ember Objects

WHAT'S SO COOL ABOUT EMBER OBJECTS?Computed Properties & Macros

Observers

Bindings

Page 7: The basics of Ember Objects

COMPUTED PROPERTIESComputed properties let you declare functions as properties.You create one by defining a computed property as a function,

which Ember will automatically call when you ask for theproperty. You can then use it the same way you would any

normal, static property.

It's super handy for taking one or more normal properties andtransforming or manipulating their data to create a new value.

Page 8: The basics of Ember Objects

COMPUTED PROPERTY BASIC EXAMPLE App.Person = Ember.Object.extend({ firstName: null, lastName: null, fullName: function() { return this.get('firstName') + ' ' + this.get('lastName'); }.property('firstName', 'lastName')});

var jason = App.Person.create({ firstName: "Jason", lastName: "Schmidt"});

jason.get('fullName'); // "Jason Schmidt"

Page 9: The basics of Ember Objects

COMPUTED PROPERTY REAL WORLD EXAMPLE // In ControllerApp.ApplicationController = Ember.Controller.extend({ isAuthenticated: function() { return App.AuthManager.isAuthenticated(); }.property('App.AuthManager.apiKey')});

// In Handlebars Template<ul> {{#if isAuthenticated}} <li><a href="#">Log Out</a></li> {{else}} <li><a href="#">Log In</a></li> {{/if}}</ul>

Page 10: The basics of Ember Objects

COMPUTED PROPERTY MACRO EXAMPLE // Long versionApp.ApplicationController = Ember.Controller.extend({ hasMultipleRoles: function() { return this.get('currentUser.roles.length') > 1; }.property('currentUser.roles.length')});

// Macro versionApp.ApplicationController = Ember.Controller.extend({ hasMultipleRoles: Ember.computed.gt('currentUser.roles.length', 1);});

Page 11: The basics of Ember Objects

OBSERVERSEmber supports observing any property, including computed

properties. You can set up an observer on an object by using theobserves method on a function.

Page 12: The basics of Ember Objects

OBSERVER EXAMPLE var Hunter = Ember.Object.extend({ isHappy: false, loadedGun: false, shootDeer: function() { if (this.get('loadedGun')) { var randomNumber = Math.floor(Math.random() * 10); if (randomNumber % 2 === 0) { this.set('isHappy', true); } } }.observes('loadedGun')});

var jason = Hunter.create();

jason.set('loadedGun', true); // shootDeer method will 'fire' when loadedGun property changes

Page 13: The basics of Ember Objects

BINDINGSA binding creates a link between two properties such that when

one changes, the other one is updated to the new valueautomatically. Bindings can connect properties on the same

object, or across two different objects. Unlike most otherframeworks that include some sort of binding implementation,

bindings in Ember.js can be used with any object, not justbetween views and models.

Page 14: The basics of Ember Objects

BINDING BASIC EXAMPLE // In ControllerApp.SomeController = Ember.Controller.extend({ someProperty: someValue});

// In Handlebars Template<p>{{someProperty}}</p>

Page 15: The basics of Ember Objects

COMPUTED PROPERTIES, OBSERVERS ANDBINDINGS, OH MY!

WHAT TO USE WHEN?

Page 16: The basics of Ember Objects

COMPUTED PROPERTIESUse computed properties to build a new property by

synthesizing other properties. Computed properties should notcontain application behavior, and should generally not cause any

side-effects when called.

Page 17: The basics of Ember Objects

OBSERVERSObservers should contain behavior that reacts to changes inanother property. Observers are especially useful when youneed to perform some behavior after a binding has finished

synchronizing.

Page 18: The basics of Ember Objects

BINDINGSBindings are most often used to ensure objects in two differentlayers are always in sync. For example, you bind your views to

your controller using Handlebars.

Page 19: The basics of Ember Objects

REFERENCESEmber Objects (From Ember Guides)Ember Objects APIVideo: POJOs vs Ember Objects, Ember.getComputed Property Macros (Evil Trout Article)