Ember.js - A JavaScript framework for creating ambitious web applications

34
Dev-day Ember.js A JavaScript framework for creating ambitious web applications Juliana Lucena 26 Oct 2012

description

Dev-day presented at Redu Educational Technologies about Ember.js. It points some Ember.js' benefits and gives an overview.

Transcript of Ember.js - A JavaScript framework for creating ambitious web applications

Page 1: Ember.js - A JavaScript framework for creating ambitious web applications

Dev-dayEmber.js

A JavaScript framework for creating ambitious web applications

Juliana Lucena26 Oct 2012

Page 2: Ember.js - A JavaScript framework for creating ambitious web applications

Why?

• Clearly separate back-end and front-end

• Faster user app interaction

• Makes your life easier :)

2

Page 3: Ember.js - A JavaScript framework for creating ambitious web applications

Ember.js

• Browser based MVC framework

• Eliminates boilerplate

• Provides architecture

• Based on event propagation

3

Page 4: Ember.js - A JavaScript framework for creating ambitious web applications

Dependencies

• jQuery

• Handlebars.js - Template language

<script type="text/x-handlebars" data-template-name="answer"> <a href="#" class="author-name">{{ answer.author.name }}</a> <span class="status-info">respondeu:</span> {{#if answer.created_at}} {{datetime answer.created_at}} {{/if}} <p class="input-user">{{ answer.content.text }}</p></script>

4

Page 6: Ember.js - A JavaScript framework for creating ambitious web applications

Ember.js MVC

6

Page 7: Ember.js - A JavaScript framework for creating ambitious web applications

MVC Workflow

Model

Controller

View

Updates

Model Notifies

Updates

ViewUser action

7

Router• Tracks the app’s state• Affects the application’s view hierarchy• Mediates between the MVC components

URL Usable app stateDeserialize

Serialize

Page 8: Ember.js - A JavaScript framework for creating ambitious web applications

Hello Redu

8

var App = Em.Application.create();

App.ApplicationView = Em.View.extend({ templateName: 'application'});

App.ApplicationController = Em.View.extend({!});

App.Router = Em.Router.extend({ root: Ember.Route.extend({ index: Ember.Route.extend({ route: '/' }) })});

App.initialize();

(...)<body> <script type="text/x-handlebars" data-template-name="application"> <h1>Hello Redu! (chuckle)</h1> </script></body>(...)

index.html

http://jsfiddle.net/x8zpH/1/

Page 9: Ember.js - A JavaScript framework for creating ambitious web applications

M for ModelEmber.Object

9

Page 10: Ember.js - A JavaScript framework for creating ambitious web applications

Ember.Object

• Enhanced JavaScript object model

• Computed Properties (synthetizes properties)

• Observers (reacts to changes)

• Bindings (syncs objects)

10

Always access properties using get and set

Page 11: Ember.js - A JavaScript framework for creating ambitious web applications

11

var App = Ember.Application.create();

App.Person = Em.Object.extend({ firstName: null, lastName: null, fullName: function(){ return this.get('firstName') + " " + this.get('lastName'); }.property('firstName', 'lastName')});

var john = App.Person.create({ firstName: "John", lastName: "Doe"});john.get("fullName"); // John Doe

Computed Property

http://jsfiddle.net/cBWsr/2/femalesCount: function() { var people = this.get('people'); return people.filterProperty('isFemale', true). get('length');}.property('[email protected]')

What about computed property for arrays, hãn?

Ember.Object

Page 12: Ember.js - A JavaScript framework for creating ambitious web applications

12

var App = Ember.Application.create();

App.Person = Em.Object.extend({ login: null, firstName: null,

changedFirstName: function(){ this.set('login', this.get('firstName').toLowerCase()); }.observes('firstName')});

var john = App.Person.create();john.set('firstName', 'John');john.get("login"); // john

Observer

http://jsfiddle.net/X7QBU/3/

Ember.Object

Page 13: Ember.js - A JavaScript framework for creating ambitious web applications

13

App = Ember.Application.create({});

App.wife = Ember.Object.create({ householdIncome: 80000});

App.husband = Ember.Object.create({ householdIncomeBinding: 'App.wife.householdIncome'});

Ember.run.later(function() { // someone gets a raise App.husband.set('householdIncome', 90000);}, 3000);

Binding

http://jsfiddle.net/qQ382/

<script type="text/x-handlebars" > Wifes income: {{App.wife.householdIncome}}<br/> Husbands income: {{App.husband.householdIncome}} </script>

See the magic inside the view

Ember.Object

Page 14: Ember.js - A JavaScript framework for creating ambitious web applications

Ember.Object

• Apply for Models

• Apply for Controllers

• Apply for Views

• Apply for (...)

14

Page 15: Ember.js - A JavaScript framework for creating ambitious web applications

Time for Dojo

15

Page 16: Ember.js - A JavaScript framework for creating ambitious web applications

Time for Dojo

16

• Already?

• It will be in a paused way

• We have so many concepts to discuss

Page 17: Ember.js - A JavaScript framework for creating ambitious web applications

Time for Dojo

17

todo doing verify done

Page 18: Ember.js - A JavaScript framework for creating ambitious web applications

Time for Dojo

18

• Scrum board

• Stories and tasks (executed by a person)

• I want to create stories (tasks)

• I want to edit stories (tasks)

• I want to delete stories (tasks)

• I want to assign a task for me

• No server integration (for now)

Next dev-day /o/(ember-data)

Page 19: Ember.js - A JavaScript framework for creating ambitious web applications

Time for Dojo

• Spoiler: http://jsfiddle.net/37YMc/2/

19

Page 20: Ember.js - A JavaScript framework for creating ambitious web applications

Router

20

Page 21: Ember.js - A JavaScript framework for creating ambitious web applications

Router

21

• Maps URLs to states and vice versa

• Preferred for building large apps

• Can link views based on the state

• Loves name conventions

Page 22: Ember.js - A JavaScript framework for creating ambitious web applications

Router

22

App.Router = Ember.Router.extend({ root: Em.Route.extend({ contacts: Em.Route.extend({ route: '/', redirectsTo: 'index'

index: Em.Route.extend({ route: '/',

connectOutlets: function(router) { router.get('applicationController').connectOutlet('contacts', App.Contacts.findAll()); } }),

contact: Em.Route.extend({ route: '/contacts/:contact_id',

connectOutlets: function(router, context) { router.get('contactsController').connectOutlet('contact', context); }, }) }) })});

Page 23: Ember.js - A JavaScript framework for creating ambitious web applications

WTF is outlet?

23

ContactsView(objects)

router.get('applicationController').connectOutlet('contacts', objects);

Name convention

Name convention

Passed objects

ApplicationView

{{outlet}}

Page 24: Ember.js - A JavaScript framework for creating ambitious web applications

C for Controller

24

Page 25: Ember.js - A JavaScript framework for creating ambitious web applications

C for Controller

• Controller simple controller

• ObjectController support to manipulate an Object

• ArrayController support to manipulate a collection

25

Page 26: Ember.js - A JavaScript framework for creating ambitious web applications

push

C for Controller

26

App.ContactController = Em.ObjectController.extend({ someProperty: 'cool-value',

destroyRecord: function() { this.get('content').destroy(); }});

App.ContactsController = Em.ArrayController.extend({ createContact: function(name) { var contact = App.Contact.create({ name: name }); this.pushObject(contact); },});

pushing a object to a controller’s collection

Accessing the content

Remember that all binding’s magic apply to

Controllers

Page 27: Ember.js - A JavaScript framework for creating ambitious web applications

V for View

27

Page 28: Ember.js - A JavaScript framework for creating ambitious web applications

V for View

28

• Always associated with a template

• Your friend when dealing with browser events

• touch (touchStart), keyboard (keyDown), mouse (mouseDown), form (submit), drag and drop (dragStart),

Page 29: Ember.js - A JavaScript framework for creating ambitious web applications

V for View

29

App.EditContactView = Ember.View.extend({ templateName: 'edit_contact', tagName: 'form', classNames: ['form-horizontal'],

didInsertElement: function() { this._super(); this.$('input:first').focus(); },

submit: function(event) { event.preventDefault(); this.get('controller').updateRecord(); }});

<script type="text/x-handlebars" data-template-name="edit_contact"> {{ view Ember.TextArea }} <button class="button-primary" type="submit">Enviar</button> </script>

DOM event

Browser event

Page 30: Ember.js - A JavaScript framework for creating ambitious web applications

Handlebars helpers

30

Page 31: Ember.js - A JavaScript framework for creating ambitious web applications

Handlebars helpers

31

• {{#if}}

• {{#each}}

• {{view}}

• {{action}}

Page 32: Ember.js - A JavaScript framework for creating ambitious web applications

Final doubts

32

Page 33: Ember.js - A JavaScript framework for creating ambitious web applications

References

• http://emberjs.com/ (updated yesterday!)

• http://emberwatch.com/

33

Page 34: Ember.js - A JavaScript framework for creating ambitious web applications

Thanks. /o/

34

Juliana Lucena