Tagged: Ember

Refactoring and Making Promises

After spending the last year writing Ember.js code I've realized one really big thing - Ember is about promises. I recently had a chance to refactor some code with a number of asynchrounous calls. The original logic used timers and polling to monitor the state of a bunch of recursive calls to the Google API to retrieve a complete folder structure. Timers get the job done, but promises make it elegant.

Original Code

Requirement: wait until the entire tree is loaded before transition.

 App.GoogleFile = Ember.Object.extend({ }); App.ApplicationRoute = Ember.Route.extend({ model: function(){ var model = App.GoogleFile.create({ id: $('meta[name="google-folder-id"]').attr('content'); children: Ember.ArrayProxy.create({content: []}) }); this.loadChildren(model.get('children')); return model; }, afterModel: function(transition){ if(transition.targetName == "application.index"){ this.wait(model.get('children'), function(){ this.transitionT...

Continue Reading →

Session timeout for your Ember application

We ran into an issue with sessions timing out while users were doing client side stuff with an Ember application we're building. Once the session timed out they could no longer make authenticated requests of course and all ajax calls would fail.

Here's a couple of bits of code that might be helpful.

First capture 401 errors in your REST adapter and send the user to the sign in page if they've been logged out:

var Adapter = DS.RESTAdapter.extend({

ajaxError: function(jqXHR){ if(jqXHR.status == 401){ window.location.href = '/users/sign_in?timeout=true'; } if(jqXHR){ jqXHR.then = null; } return jqXHR; } });

We wanted something a bit more user friendly than a redirect so we proactively warn the user and then send keep alives as needed. The solution is an Ember component that uses Twitter Bootstrap modals to warn the user. The code assumes you have Twitter Bootstrap setup. If you don't want to use Twitter Bootstrap then it should be pretty easy to swap out the htm...

Continue Reading →