Share
Embed
Intro to Ember & Ember CLI
From the April 2015 EmberATX meetup in Austin, TX.
tehviking
April 24, 2015
Transcript
126 slides
-
w e l c o m e t o t
h e n e x t l e v e l -
Ember CLI: UP AND RUNNING! The stuff you need to
build next-level native web apps -
WHAT WE’LL COVER: ¶ Ember CLI basics ¶ Building an
app with CLI ¶ Migrating to Ember-CLI -
Ember lets me focus on our unique business problems, not
boilerplate code i just wanna code! -
I don’t want to spend my limited time on earth
shaving yaks. WHY ember? -
Let’s see, we have a community, a shared problem, and
Ember philosophy... so what do we do? -
An opinionated suite of command line tools for building Ember
apps. Enter ember cli. -
“You should begin moving your app to Ember CLI as
soon as possible.” - Tom Dale, Javascript Ombudsman WHY ember cli? -
So let’s get up to speed. And get back to
what we like: making stuff. -
the pleasure º Generators º Fast builds º Add-on ecosystem
º Testing integration º ES6, Sass support -
THE PAIN ‡ Learning ES6 modules ‡ Cryptic error messages
like “cannot find file tmp/3n8glcv7cv6sgs ‡ Migrating existing apps to Ember-CLI -
basic concepts - Installing - Generating - Using ES6 modules
- Folder structure - Assets - Testing - Deployment -
LMGTFY ember generate route images ember generate component pixelate-image and
my favorite... ember generate acceptance-test images (...let me generate that for you) -
Ember GENERATORS Controller Model Route Resource Template Mixin Component Helper
Initializer Adapter Serializer Service Transform Util View -
Ember GENERATORS Acceptance-test* Adapter-test Component-test Controller-test Helper-test Initializer-test Mixin-test Model-test
Route-test Serializer-test Service-test Transform-test Util-test View-test *(All but this are generated automatically) -
Ember GENERATORS Addon In-repo-addon (& lib) App Blueprint Http-mock Http-proxy
Server Test-helper Plus custom generators from installed addons ...Or create your own -
App.ImagesRoute = Ember.Route.extend({ model: function() { return App.imageData(); } });
app/routes/images-route.js From globals... -
App.ImagesRoute = Ember.Route.extend({ model: function() { return App.imageData(); } });
app/routes/images-route.js From globals... Ember is global imageData comes from global App (at least, we hope) -
import Ember from 'ember'; import imageData from '../utils/image-data'; var
ImagesRoute = Ember.Route.extend({ model: function() { return imageData(); } }); export default ImagesRoute; app/routes/images.js TO ES6 Modules -
import Ember from 'ember'; import imageData from '../utils/image-data'; var
ImagesRoute = Ember.Route.extend({ model: function() { return imageData(); } }); export default ImagesRoute; app/routes/images.js to ES6 Modules Ember comes from ember.js -
import Ember from 'ember'; import imageData from '../utils/image-data'; var
ImagesRoute = Ember.Route.extend({ model: function() { return imageData(); } }); export default ImagesRoute; app/routes/images.js to ES6 Modules imageData comes from ../utils -
import Ember from 'ember'; import imageData from '../utils/image-data'; var
ImagesRoute = Ember.Route.extend({ model: function() { return imageData(); } }); export default ImagesRoute; app/routes/images.js to ES6 Modules export so others can import -
ember generate resource foo --pod and if you want, in
your env: podModulePrefix: 'pixelfy-me/pods' Maybe you’ll like it -
The smartest DevOps engineer is our collective community ember cli
ends the build tool yak shave -
ApplicationRoute (generated): ‘/’ ProductRoute: ‘/:id’ Rendered into products {{outlet}} URL:
/ products / 230 ProductsRoute: ‘/products’ Rendered into application {{outlet}} -
<x-select value={{bob}} action=“selectPerson”> {{each people as |person|}} <x-option value={{person}}>{{person.name}}</x-option> {{/each}}
</x-select> EMBER COMPONENTS OF THE NEAR FUTURE -
COMPUTED PROPERTIES app/controllers/cart.js export default Ember.Controller.extend({ cartItemSubtotals: Ember.computed.mapBy("model.cartItems", "subtotal"), orderSubtotal:
Ember.computed.sum("cartItemSubtotals"), shippingCost: Ember.computed(function() { return 0; }), orderTax: Ember.computed("orderSubtotal", function() { var taxCents = (this.get("orderSubtotal") * 100) * 0.0825; return taxCents / 100; }), orderTotal: Ember.computed("orderTax", "orderSubtotal", function() { return this.get("orderSubtotal") + this.get("orderTax"); }), }); -
COMPUTED MACROS app/controllers/cart.js export default Ember.Controller.extend({ cartItemSubtotals: Ember.computed.mapBy("model.cartItems", "subtotal"), orderSubtotal:
Ember.computed.sum("cartItemSubtotals"), shippingCost: Ember.computed(function() { return 0; }), orderTax: Ember.computed("orderSubtotal", function() { var taxCents = (this.get("orderSubtotal") * 100) * 0.0825; return taxCents / 100; }), orderTotal: Ember.computed("orderTax", "orderSubtotal", function() { return this.get("orderSubtotal") + this.get("orderTax"); }), }); -
IF YOU UNDERSTAND: • Generating with Ember CLI • Router
& Routes • Components • Computed Properties YOU CAN BUILD AWESOME STUFF -
Ember CLI lets me focus on our unique business problems,
not janky build systems i just wanna code! -
look out, batman! it’s the pixeler! Shut up, Robin! Worst.
Villain. Ever. -
Let’s build an app! ember new pixelfy-me cd pixelfy-me git
remote add origin https://github.com/thefrontside/pixelfy-me.git git fetch --tags open the folder in your text editor of choice -
Task 1: List Images What we’ll do: Generate Images resource
Write Model hook for Images Route Install helper for image data Display images on Images page -
git checkout task-1 npm install bower install (Stuck? You can
git checkout at any task with git checkout task-<task number>) Task 1: List Images -
import Ember from 'ember'; export default Ember.Object.extend({ url: ""
}); app/models/image.js “downgrade” model -
import Ember from 'ember'; export default Ember.Route.extend({ model: function()
{ return imageData(); } }); app/routes/images.js Load data in model hook KABOOOOOM! (doesn’t exist) -
import Ember from 'ember'; import imageData from 'ember-cli-sample-image-data'; export
default Ember.Route.extend({ model: function() { return imageData(); } }); app/routes/images.js import it in the route -
... "8": { id: "8", url: "/img/yeezus.jpg" } }; if(arguments.length)
{ return Image.create(images[id]); } else { var imagesArray = Object.keys(images).map(function(k){ return Image.create(images[k]); }); return imagesArray; } inside the addon’s hidden volcano lair... P.S. it’s not really that magical -
Let’s talk about what just happened: - Generated a route
& model - Tweaked the route & model slightly - Installed an addon - Used ES6 to include the addon helper That loads images! -
task 2: show image What we’ll do: Generate images/show route
Add dynamic segment to show route Write Model hook for Images Show Route Link to Image from Images page Display image on Image Show section -
Generate Images show route ember generate route images/show (again, don’t
overwrite that hbs file!) -
Router.map(function() { this.resource('images', function() { this.route('show', {path: ":id"}); }); });
app/router.js Add dynamic segment -
import Ember from 'ember'; import imageData from 'ember-cli-sample-image-data'; export
default Ember.Route.extend({ model: function(params) { return imageData(params.id); } }); app/routes/images/show.js Add model hook & import statement -
{{#each image in model}} <li> {{#link-to "images.show" image}} <img class="gallery-image"
src={{image.url}}> {{/link-to}} </li> {{/each}} app/templates/images.hbs link to image from images template -
<div class="row"> <div class="col-md-12"> <img src={{model.url}}> <!-- Insert the pixelate-image
component, bind src to model.url, bind value to pixelatePercentage --> ! Implement pixelate-image here ! </div> </div> app/templates/images/show.hbs Display image in the show template -
To recap what we just built: - Generated a nested
route with / - Used a dynamic segment - Re-used ES6 for our imageData helper - Edited template files to display stuff images/show done! -
task 3: pixelatE What we’ll do: Install pixelate-image component Wrap
image in pixelate component Install {{emberx-slider}} component Drop in {{x-slider}} component -
Let’s look at what we did: - Installed pixelate-image custom
addon - Installed emberx-slider addon - Tied the two together to make MAGIC So yeah -
task 4: Style it What we’ll do: Install ember-cli-sass addon
Install bootstrap-sass bower package rename app.css to app.scss include bootstrap include custom SCSS -
COPY & PASTE CSS FILES Files are here (grab the
tar file): http://bit.ly/cli-training-styles - app.scss - _bootswatch.scss - bootstrap_imports.scss - bootstrap_variables.scss Copy or move them to app/styles -
Now we have a fully styled app! - Installed ember-cli-sass
to compile Sass - Installed bootstrap-sass - Used Sass @include for granular control so stylin’ -
task 5: test it Add karma, karma-cil, karma-mocha, karma-chai- plugins,
karma-chrome-launcher, karma-ember- preprocessor, karma-mocha, karma-phantomjs- launcher, karma-junit-reporter to package.json Compile and install PhantomJS npm install Install ember-mocha-adapter and chai-jquery via Bower Set up a karma.conf file In your karma.conf: Customize your frameworks section in karma.conf to include mocha, chai, sinon-chai, and chai-jquery Set up a Grunt task to build your Sass on each test run Include your vendor dependencies in karma.conf Include your code in files section Debug file load order issues Add & configure handlebars karma preprocessor Then, create test-helper.js & include it in karma.conf In your test-helper.js: Configure chai and mocha defaults Set Ember.testing to true Set App.setupForTesting to true Call App.injectTestHelpers() Set up app on each test run Tear down app after each test run Ensure beforeEach and afterEach use the run loop Use the .done() callback for async beforeEach What we’ll do: -
task 5: test it j/k lol ;) ember generate acceptance-test
images ember test -
task 5: test it What we’ll do: Generate “images” acceptance
test Remove generated Ember Data test Write an assertion Run the tests -
test('visiting /images', function(assert) { visit('/images'); andThen(function() { assert.equal(Ember.$(".spec-gallery-image").length, 8);
}); }); tests/acceptance/images-test.js basic acceptance test -
run tests $ ember test --serve 1..14 # tests 14
# pass 14 # fail 0 # ok -
bonus: gravatars What we’ll do: git checkout bonus-stage Set up
gravatar route & link Handle download action install JavaScript-md5 bower component Pixelate images in the gravatar template -
downloadImage: function() { var link = document.createElement("a"); var uri =
Ember.$("img.pixelated-image").attr("src"); link.download = "pixelfy.png"; link.href = uri; link.click(); } app/routes/gravatar.js handle download image -
var app = new EmberApp(); app.import("bower_components/JavaScript-MD5/js/md5.js"); module.exports =
app.toTree(); Brocfile.js import md5 in brocfile -
This was more complicated. - Set up gravatar route -
Handled download action - Installed bower component - Included lib in Brocfile and .jshintrc - Mixed pixelate and gravatar together It’s done! -
Now we can get back to focusing on moving fast
and shipping, with a better set of tools i just wanna code!