Last active
December 30, 2015 17:29
-
-
Save ilovett/7861344 to your computer and use it in GitHub Desktop.
Ember.js - Improving Error.Route with more explicit error code handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var App = Ember.Application.create(); | |
/** App.Error | |
* | |
* Extend the JavaScript Error object to have a code | |
* which we can pass around the app. | |
* | |
* @param {string} message A message describing the error | |
* @param {integer} code A generic code describing the error | |
*/ | |
App.Error = function(message, code) { | |
this.name = 'Error'; | |
this.message = message; | |
this.code = code; | |
this.stack = (new Error()).stack; | |
} | |
App.Error.prototype = new Error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.ErrorRoute = Ember.Route.extend({ | |
'renderTemplate' : function() { | |
switch (this.get('controller.content.code')) { | |
case 404 : return this.render('404'); | |
default : return this.render(); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.Translation.reopenClass({ | |
'findOne' : function(data) { | |
var d = new Ember.Deferred(); | |
Ember.$.ajax({ | |
'url' : '/api/v1/translation/', | |
'context' : this, | |
'dataType' : 'json', | |
'data' : data | |
}) | |
.done(function(data) { | |
try { | |
if ( ! data.objects[0]) | |
throw new App.Error("Unable to find translation", 404); | |
d.resolve(App.Translation.create(data.objects[0])); | |
} | |
catch (error) { | |
d.reject(error); | |
} | |
}) | |
.fail(function(data) { | |
d.reject(); | |
}); | |
return d; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.TranslationRoute = Ember.Route.extend({ | |
'model' : function(params) { | |
return App.Translation.findOne({ 'hash' : params.translation_hash }); | |
}, | |
'serialize' : function(model) { | |
"use strict"; | |
return { | |
'translation_hash' : model.get('hash'), | |
}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment