February 4, 2015
ngToronto Angular Meetup notes – January 2015
Promise-based architecture by Yuri
- think of promises as wrappers around uncertain results
- promises separate defining a request and response handler (callback) at the same time.
- writing functions that accept promises as arguments and return promises, you can make something that almost looks like synchronous code
Check the table in the presentation for useful definitions of promise behaviour (“when the callback…”)
Avoid $q.defer, Denodify instead
- usually you will use $q.defer wrong (usually an Antipattern)
- see http://github.com/rangle/angular-promisify for a similar function for Angular
- $q Angular service documentation – it is similar to q library
Trivial Promises
- $q.when(x) returns an ok promise
- $q.reject(e) returns a rejected promise
Stay consistent
- functions should never ‘sometimes’ return a promise. Always return a promise, or never return a promise. See $q.when and $q.reject
- return a promise if you aren’t sure whether code will eventually be async or not async
Avoid Optimistic Code
- optimistic code is code where you assume that data will eventually be loaded
- ask for a promise instead, even if you think the data loaded already
Neat things with promises
- simple caching – take advantage of promises allowing
then()
called many times - simple prefetching – call a load before you need it, use the returned promise when you need it
- postpone requests (see slide, I can’t type fast enough)
- ramda! Check pPipe & pCompose
- ES6 Generators! Use yield and promises to make synchronous-looking code
layout: ../../layouts/BlogPost.astro
Hexo + AngularJS by Matias
Hexo is:
- well documented and well designed static site generator
- very configurable
- runs on nodejs
Static sites are good because:
- Host on AWS S3 (cheap and fast) and CloudFlare (even faster!)
- You get a local copy of your site that’s always up to date by default. Your writing happens locally, and is saved in version control
Hexo is good because:
- Hexo rebuilds pages and sites quickly,
hexo server
command watches for content changes and live-reloads them in your browser while you are writing posts - posts are written in markdown with a small YAML header
- your content can live as files in a Git repo, and custom theme in another Git repo (possibly a private repo)
- templates use common js templating formats – ejs, jade
- uses excellent tools like Warehouse.js
- supports custom pages too (not just posts)
How do I handle dynamic behaviour without a backend?
3rd party services and more JS!
- Newsletter signup? Try MailChimp
- Code editor in posts? Make a custom tag for Hexo to parse which uses Angular to add interactivity
- Searching? Try lunr.js
- Check Mashape for more 3rd party API ideas to integrate
Angular idea
How about capturing all internal link clicks using an Angular directive, and replacing the default behaviour with pushstate, an AJAX call to load the requested page, and an animation to load the content into the current page? That gives you even faster page loads and room to animate content coming and going. Yearofmoo.com is doing this now.