36
EMBER.JS FOR A CAKEPHP DEVELOPER

Ember.js for a CakePHP Developer

Embed Size (px)

DESCRIPTION

Ember.js is a front end JavaScript MVC framework, but probably won't map to the exact same MVC patterns as CakePHP does on the server side. I cover the major features of Ember.js and attempt to make the connection to CakePHP classes where it applies.

Citation preview

Page 1: Ember.js for a CakePHP Developer

EMBER.JS FOR ACAKEPHPDEVELOPER

Page 2: Ember.js for a CakePHP Developer

EMBER.JSA framework for creating ambitious web applications.

Page 3: Ember.js for a CakePHP Developer

1.0 RELEASED!

Page 4: Ember.js for a CakePHP Developer

OBJECTS

Page 5: Ember.js for a CakePHP Developer

EXTENDING/CREATING OBJECTSvar Person, p;

Person = Ember.Object.extend({ sayHello: function() { alert('Hello!'); }});

p = Person.create();

p.sayHello();

Page 6: Ember.js for a CakePHP Developer

DATA BINDINGSvar Person, p;

Person = Ember.Object.extend({ message: 'Hello', responseBinding: 'message', yell: function() { alert(this.get('response')); }});

p = Person.create();

p.yell(); // alert('Hello')

p.set('message', 'Goodbye');

p.yell(); // alert('Goodbye')

Page 7: Ember.js for a CakePHP Developer

COMPUTED PROPERTIESvar Person, p;

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

p = Person.create({ firstName: 'Joey', lastName: 'Trapp' });

p.get('fullName'); // "Joey Trapp"

p.set('firstName', 'Connor');

p.get('fullName'); // "Connor Trapp"

Page 8: Ember.js for a CakePHP Developer

CONVENTIONS

Page 9: Ember.js for a CakePHP Developer

Type NameRoute this.resource('projects');Route (class) App.ProjectsRouteController App.ProjectsControllerView App.ProjectsViewTemplate Ember.TEMPLATES['projects']

Page 10: Ember.js for a CakePHP Developer

TEMPLATES

Page 11: Ember.js for a CakePHP Developer

TEMPLATES EMBEDDED IN HTML<script type="text/x-handlebars" data-template-name="project"> <h1>{{title}}</h1> <p>{{description}}</p></script>

Gets compiled on document read to:Ember.TEMPLATES['project'] = // compiled template function

Page 12: Ember.js for a CakePHP Developer

TEMPLATES FILESIntegrate into build step and compile server side

// webroot/templates/projects/add.hbs

<form {{action save on="submit"}}> <label>Project Name</label> {{input type="text" value=name}}

<input type="submit" value="Save"></form>

Gets compiled with a build tool in development, and aspart of a production build step to:

Ember.TEMPLATES['projects/add'] = // compiled template function

Page 13: Ember.js for a CakePHP Developer

BUILT IN HELPERS<div class="posts">{{#each post in controller}}

{{#if post.isPublished}} <h2>{{#link-to 'post' post}} {{post.title}} {{/link-to}}</h2> {{/if}}

{{else}}

<p>{{#link-to 'posts.new'}}Create the first post{{/link-to}}

{{/each}}</p></div>

And many more

Page 14: Ember.js for a CakePHP Developer

ROUTER

Page 15: Ember.js for a CakePHP Developer

DEFINING ROUTESApp.Router.map(function() { this.route('about'); // #/about});

Routes defined this way can not contain nested routes

Page 16: Ember.js for a CakePHP Developer

DEFINING RESOURCESApp.Router.map(function() { this.resource('conferences'); // #/conferences});

Will render the conferences template in theapplication templates outlet.

Page 17: Ember.js for a CakePHP Developer

NESTING RESOURCESApp.Router.map(function() { this.resource('conferences', function() { // #/conferences this.resource('cakefest'); // #/conferences/cakefest });});

Will render the cakefest template in the conferencestemplates outlet, which is rendered in the application

templates outlet.

Page 18: Ember.js for a CakePHP Developer

DYNAMIC SEGMENTSApp.Router.map(function() { this.resource('conferences', function() { // #/conferences this.resource('conference', { path: ':name' }); // #/conferences/blah });});

By default, the segment value is available in the template.// conference template

<h1>{{name}}</h1><p>...</p>

Page 19: Ember.js for a CakePHP Developer

NESTED ROUTESApp.Router.map(function() { this.resource('conferences', function() { // #/conferences this.route('new'); // #/conferences/new });});

Renders conferences/new template in theconferences templates outlet.

Page 20: Ember.js for a CakePHP Developer

ROUTES

Page 21: Ember.js for a CakePHP Developer

DEFINING DATA FOR TEMPLATEwindow.CONFERENCES = [ Em.Object.create({id: 1, name: 'cakefest' }), Em.Object.create({id: 2, name: 'embercamp' }), Em.Object.create({id: 3, name: 'jsconf' })];

var App = Ember.Application.create();

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

Page 22: Ember.js for a CakePHP Developer

DEFINING ROUTE CLASSESApp.ConferencesRoute = Ember.Route.extend({ model: function() { return window.CONFERENCES; }});

App.ConferenceRoute = Ember.Route.extend({ model: function(params) { return window.CONFERENCES.findProperty( 'id', +params.conference_id ); }});

Page 23: Ember.js for a CakePHP Developer

TEMPLATESconferences.hbs

<h1>Conferences</h1>

{{#each conf in controller}} {{#link-to 'conference' conf}} {{conf.name}} {{/link-to}}{{/each}}

conference.hbs<h1>{{name}} Conference</h1><p>{{desc}}</p>

Page 24: Ember.js for a CakePHP Developer

OTHER CALLBACKSApp.ConferenceRoute = Ember.Route.extend({ model: function(params) { // Return data for the template },

setupController: function(controller, model) { // Receives instance of this controller and // the return value from model hook },

renderTemplate: function() { // Render template manually if you need to do // something unconventional }});

Page 25: Ember.js for a CakePHP Developer

CONTROLLERS

Page 26: Ember.js for a CakePHP Developer

Controllers are long lived in Ember*,

and are the default context for templates.

Page 27: Ember.js for a CakePHP Developer

CONTROLLERDecorates a model, but has no special proxying behavior.

App.ApplicationController = Ember.Controller.extend({ search: '',

query: function() { var query = this.get('search'); this.transitionToRoute('search', { query: query }); }});

Page 28: Ember.js for a CakePHP Developer

OBJECT CONTROLLERActs like an object and proxies to the model property.// In route class (this is the default behavior)setupController: function(controller, model) { controller.set('model', model);}

// Object ControllerApp.ConferenceController = Ember.ObjectController.extend({ isEven: function() { return this.get('name').length % 2 === 0; }.property('name')});

isEven calls this.get('name') which proxies tothis.get('model.name')

Page 29: Ember.js for a CakePHP Developer

ARRAY CONTROLLERActs like an array, but actually performs on the methods

on the model property.App.ConferencesController = Ember.ArrayController.extend({ sortProperties: ['title']});

// conferences template{{#each conf in controller}} <div> <h1>{{conf.title}}</h1> </div>{{/each}}

Looping over controller in a template is actuallylooping over the model property*

Page 30: Ember.js for a CakePHP Developer

VIEWS

Page 31: Ember.js for a CakePHP Developer

Views are primarily used to handle browser events. Sincemany application actions can be handled with the action

helper and controller methods, you'll often not defineviews.

Page 32: Ember.js for a CakePHP Developer

UTILIZE BROWSER EVENTSApp.ConferenceView = App.View.extend({ click: function(e) { alert('Click event was handled'); }});

Page 33: Ember.js for a CakePHP Developer

SETTING VIEWS TAGIn the DOM, your templates are wrapped by a div with a

special id that Ember knows about.App.ConferenceView = Ember.View.extend({ tagName: 'section',});

The element wrapping the template will now be asection

Page 34: Ember.js for a CakePHP Developer

HELPERS

Page 35: Ember.js for a CakePHP Developer

REUSABLE TEMPLATE FUNCTIONSEmber.Handlebars.helper('capitalize', function(str) { return str[0].toUpperCase() + str.slice(1);});

And use the helpers in handlebars templates

<h1>{{title}}</h1><p>by {{capitalize firstName}} {{capitalize lastName}}</p>

Page 36: Ember.js for a CakePHP Developer

QUESTIONS?@joeytrapp

github.com/joeytrapp

@loadsys

github.com/loadsys