问题
I'm using Embers findQuery
method and wonder how to catch 404 errors when there are no results?
this.store.findQuery('customer', { hasProjects: true, getArchivedProjects: archived }).then(function(customers) {
});
If the query is empty, the code inside this then
function doesn't get fired, so I can't even check the type of customers
.
Example:
this.store.findQuery('customer', { hasProjects: true, getArchivedProjects: archived }).then(function(customers) {
console.log('foo')
});
If the query returns a 404, console.log
doesn't be fired.
回答1:
The findQuery function returns a promise. You may then provide two functions to the then(), the first being the success path, the second being the failure path... for example:
this.store.findQuery('customer', { hasProjects: true, getArchivedProjects: archived }).then(function(customers) {
console.log('foo')
}, function(error) { /* do something with error */ });
回答2:
Alternative answer:
Add a error
hook to the corresponding route:
App.CustomersIndexRoute = Ember.Route.extend({
actions: {
error: function(reason) {
if (reason.status === 404) {
// do something ...
}
}
}
})
See: http://emberjs.com/guides/routing/asynchronous-routing/#toc_when-promises-reject.
来源:https://stackoverflow.com/questions/20905772/catch-404-error-when-using-store-findquery