Converted a bunch of ajax calls to use promises

This commit is contained in:
Robin Ward 2013-04-03 16:06:55 -04:00
parent 33f349a1e6
commit 5ec41d454c
11 changed files with 117 additions and 158 deletions

View File

@ -13,9 +13,9 @@ Discourse.AdminEmailLogsController = Ember.ArrayController.extend(Discourse.Pres
@property sendTestEmailDisabled @property sendTestEmailDisabled
**/ **/
sendTestEmailDisabled: (function() { sendTestEmailDisabled: function() {
return this.blank('testEmailAddress'); return this.blank('testEmailAddress');
}).property('testEmailAddress'), }.property('testEmailAddress'),
/** /**
Sends a test email to the currently entered email address Sends a test email to the currently entered email address
@ -23,17 +23,17 @@ Discourse.AdminEmailLogsController = Ember.ArrayController.extend(Discourse.Pres
@method sendTestEmail @method sendTestEmail
**/ **/
sendTestEmail: function() { sendTestEmail: function() {
var _this = this; this.set('sentTestEmail', false);
_this.set('sentTestEmail', false);
var adminEmailLogsController = this;
Discourse.ajax({ Discourse.ajax({
url: Discourse.getURL("/admin/email_logs/test"), url: Discourse.getURL("/admin/email_logs/test"),
type: 'POST', type: 'POST',
data: { email_address: this.get('testEmailAddress') }, data: { email_address: this.get('testEmailAddress') }
success: function() { }).then(function () {
return _this.set('sentTestEmail', true); adminEmailLogsController.set('sentTestEmail', true);
}
}); });
return false;
} }
}); });

View File

@ -19,14 +19,13 @@ Discourse.AdminDashboard.reopenClass({
@return {jqXHR} a jQuery Promise object @return {jqXHR} a jQuery Promise object
**/ **/
find: function() { find: function() {
var model = Discourse.AdminDashboard.create();
return Discourse.ajax(Discourse.getURL("/admin/dashboard"), { return Discourse.ajax(Discourse.getURL("/admin/dashboard"), {
type: 'GET', type: 'GET',
dataType: 'json', dataType: 'json'
success: function(json) { }).then(function(json) {
model.mergeAttributes(json); var model = Discourse.AdminDashboard.create(json);
model.set('loaded', true); model.set('loaded', true);
} return model;
}); });
}, },
@ -38,14 +37,13 @@ Discourse.AdminDashboard.reopenClass({
@return {jqXHR} a jQuery Promise object @return {jqXHR} a jQuery Promise object
**/ **/
fetchProblems: function() { fetchProblems: function() {
var model = Discourse.AdminDashboard.create();
return Discourse.ajax(Discourse.getURL("/admin/dashboard/problems"), { return Discourse.ajax(Discourse.getURL("/admin/dashboard/problems"), {
type: 'GET', type: 'GET',
dataType: 'json', dataType: 'json'
success: function(json) { }).then(function(json) {
model.mergeAttributes(json); var model = Discourse.AdminDashboard.create(json);
model.set('loaded', true); model.set('loaded', true);
} return model;
}); });
} }
}); });

View File

@ -86,56 +86,48 @@ Discourse.AdminUser = Discourse.Model.extend({
}).property('banned_till', 'banned_at'), }).property('banned_till', 'banned_at'),
ban: function() { ban: function() {
var duration, var duration = parseInt(window.prompt(Em.String.i18n('admin.user.ban_duration')), 10);
_this = this;
if (duration = parseInt(window.prompt(Em.String.i18n('admin.user.ban_duration')), 10)) {
if (duration > 0) { if (duration > 0) {
return Discourse.ajax(Discourse.getURL("/admin/users/") + this.id + "/ban", { Discourse.ajax(Discourse.getURL("/admin/users/") + this.id + "/ban", {
type: 'PUT', type: 'PUT',
data: {duration: duration}, data: {duration: duration}
success: function() { }).then(function () {
// succeeded
window.location.reload(); window.location.reload();
}, }, function(e) {
error: function(e) { // failure
var error = Em.String.i18n('admin.user.ban_failed', { error: "http: " + e.status + " - " + e.body }); var error = Em.String.i18n('admin.user.ban_failed', { error: "http: " + e.status + " - " + e.body });
bootbox.alert(error); bootbox.alert(error);
}
}); });
} }
}
}, },
unban: function() { unban: function() {
var _this = this; Discourse.ajax(Discourse.getURL("/admin/users/") + this.id + "/unban", {
return Discourse.ajax(Discourse.getURL("/admin/users/") + this.id + "/unban", { type: 'PUT'
type: 'PUT', }).then(function() {
success: function() { // succeeded
window.location.reload(); window.location.reload();
}, }, function(e) {
error: function(e) { // failed
var error = Em.String.i18n('admin.user.unban_failed', { error: "http: " + e.status + " - " + e.body }); var error = Em.String.i18n('admin.user.unban_failed', { error: "http: " + e.status + " - " + e.body });
bootbox.alert(error); bootbox.alert(error);
}
}); });
}, },
impersonate: function() { impersonate: function() {
var _this = this; Discourse.ajax(Discourse.getURL("/admin/impersonate"), {
return Discourse.ajax(Discourse.getURL("/admin/impersonate"), {
type: 'POST', type: 'POST',
data: { data: { username_or_email: this.get('username') }
username_or_email: this.get('username') }).then(function() {
}, // succeeded
success: function() {
document.location = "/"; document.location = "/";
}, }, function(e) {
error: function(e) { // failed
_this.set('loading', false);
if (e.status === 404) { if (e.status === 404) {
return bootbox.alert(Em.String.i18n('admin.impersonate.not_found')); bootbox.alert(Em.String.i18n('admin.impersonate.not_found'));
} else { } else {
return bootbox.alert(Em.String.i18n('admin.impersonate.invalid')); bootbox.alert(Em.String.i18n('admin.impersonate.invalid'));
}
} }
}); });
} }
@ -167,18 +159,14 @@ Discourse.AdminUser.reopenClass({
}, },
findAll: function(query, filter) { findAll: function(query, filter) {
var result; var result = Em.A();
result = Em.A();
Discourse.ajax({ Discourse.ajax({
url: Discourse.getURL("/admin/users/list/") + query + ".json", url: Discourse.getURL("/admin/users/list/") + query + ".json",
data: { data: { filter: filter }
filter: filter }).then(function(users) {
}, users.each(function(u) {
success: function(users) { result.pushObject(Discourse.AdminUser.create(u));
return users.each(function(u) {
return result.pushObject(Discourse.AdminUser.create(u));
}); });
}
}); });
return result; return result;
} }

View File

@ -17,16 +17,14 @@ Discourse.EmailLog.reopenClass({
}, },
findAll: function(filter) { findAll: function(filter) {
var result; var result = Em.A();
result = Em.A();
Discourse.ajax({ Discourse.ajax({
url: Discourse.getURL("/admin/email_logs.json"), url: Discourse.getURL("/admin/email_logs.json"),
data: { filter: filter }, data: { filter: filter }
success: function(logs) { }).then(function(logs) {
logs.each(function(log) { logs.each(function(log) {
result.pushObject(Discourse.EmailLog.create(log)); result.pushObject(Discourse.EmailLog.create(log));
}); });
}
}); });
return result; return result;
} }

View File

@ -33,17 +33,17 @@ Discourse.FlaggedPost = Discourse.Post.extend({
return r; return r;
}).property(), }).property(),
lastFlagged: (function() { lastFlagged: function() {
return this.post_actions[0].created_at; return this.post_actions[0].created_at;
}).property(), }.property(),
user: (function() { user: function() {
return this.userLookup[this.user_id]; return this.userLookup[this.user_id];
}).property(), }.property(),
topicHidden: (function() { topicHidden: function() {
return this.get('topic_visible') === 'f'; return this.get('topic_visible') === 'f';
}).property('topic_hidden'), }.property('topic_hidden'),
deletePost: function() { deletePost: function() {
if (this.get('post_number') === "1") { if (this.get('post_number') === "1") {
@ -57,31 +57,24 @@ Discourse.FlaggedPost = Discourse.Post.extend({
return Discourse.ajax(Discourse.getURL("/admin/flags/clear/") + this.id, { type: 'POST', cache: false }); return Discourse.ajax(Discourse.getURL("/admin/flags/clear/") + this.id, { type: 'POST', cache: false });
}, },
hiddenClass: (function() { hiddenClass: function() {
if (this.get('hidden') === "t") return "hidden-post"; if (this.get('hidden') === "t") return "hidden-post";
}).property() }.property()
}); });
Discourse.FlaggedPost.reopenClass({ Discourse.FlaggedPost.reopenClass({
findAll: function(filter) { findAll: function(filter) {
var result; var result = Em.A();
result = Em.A(); Discourse.ajax(Discourse.getURL("/admin/flags/") + filter + ".json").then(function(data) {
Discourse.ajax({ var userLookup = {};
url: Discourse.getURL("/admin/flags/") + filter + ".json",
success: function(data) {
var userLookup;
userLookup = {};
data.users.each(function(u) { data.users.each(function(u) {
userLookup[u.id] = Discourse.User.create(u); userLookup[u.id] = Discourse.User.create(u);
}); });
return data.posts.each(function(p) { data.posts.each(function(p) {
var f; var f = Discourse.FlaggedPost.create(p);
f = Discourse.FlaggedPost.create(p);
f.userLookup = userLookup; f.userLookup = userLookup;
return result.pushObject(f); result.pushObject(f);
}); });
}
}); });
return result; return result;
} }

View File

@ -26,17 +26,15 @@ Discourse.GithubCommit = Discourse.Model.extend({
Discourse.GithubCommit.reopenClass({ Discourse.GithubCommit.reopenClass({
findAll: function() { findAll: function() {
var result; var result = Em.A();
result = Em.A();
Discourse.ajax( "https://api.github.com/repos/discourse/discourse/commits?callback=callback", { Discourse.ajax( "https://api.github.com/repos/discourse/discourse/commits?callback=callback", {
dataType: 'jsonp', dataType: 'jsonp',
type: 'get', type: 'get',
data: { per_page: 25 }, data: { per_page: 25 },
success: function(response, textStatus, jqXHR) { }).then(function (response) {
response.data.each(function(commit) { response.data.each(function(commit) {
result.pushObject( Discourse.GithubCommit.create(commit) ); result.pushObject( Discourse.GithubCommit.create(commit) );
}); });
}
}); });
return result; return result;
} }

View File

@ -93,10 +93,7 @@ Discourse.Report = Discourse.Model.extend({
Discourse.Report.reopenClass({ Discourse.Report.reopenClass({
find: function(type) { find: function(type) {
var model = Discourse.Report.create({type: type}); var model = Discourse.Report.create({type: type});
Discourse.ajax(Discourse.getURL("/admin/reports/") + type, { Discourse.ajax(Discourse.getURL("/admin/reports/") + type).then(function (json) {
type: 'GET',
success: function(json) {
// Add a percent field to each tuple // Add a percent field to each tuple
var maxY = 0; var maxY = 0;
json.report.data.forEach(function (row) { json.report.data.forEach(function (row) {
@ -107,10 +104,8 @@ Discourse.Report.reopenClass({
row.percentage = Math.round((row.y / maxY) * 100); row.percentage = Math.round((row.y / maxY) * 100);
}); });
} }
model.mergeAttributes(json.report); model.mergeAttributes(json.report);
model.set('loaded', true); model.set('loaded', true);
}
}); });
return(model); return(model);
} }

View File

@ -87,26 +87,18 @@ var SiteCustomizations = Ember.ArrayProxy.extend({
Discourse.SiteCustomization.reopenClass({ Discourse.SiteCustomization.reopenClass({
findAll: function() { findAll: function() {
var content, var customizations = SiteCustomizations.create({ content: [], loading: true });
_this = this;
content = SiteCustomizations.create({
content: [],
loading: true
});
Discourse.ajax({ Discourse.ajax({
url: Discourse.getURL("/admin/site_customizations"), url: Discourse.getURL("/admin/site_customizations"),
dataType: "json", dataType: "json"
success: function(data) { }).then(function (data) {
if (data) { if (data) {
data.site_customizations.each(function(c) { data.site_customizations.each(function(c) {
var item; customizations.pushObject(Discourse.SiteCustomization.create(c));
item = Discourse.SiteCustomization.create(c);
return content.pushObject(item);
}); });
} }
return content.set('loading', false); customizations.set('loading', false);
} });
}); return customizations;
return content;
} }
}); });

View File

@ -74,10 +74,9 @@ Discourse.SiteSetting = Discourse.Model.extend({
var setting = this; var setting = this;
return Discourse.ajax(Discourse.getURL("/admin/site_settings/") + (this.get('setting')), { return Discourse.ajax(Discourse.getURL("/admin/site_settings/") + (this.get('setting')), {
data: { value: this.get('value') }, data: { value: this.get('value') },
type: 'PUT', type: 'PUT'
success: function() { }).then(function() {
setting.set('originalValue', setting.get('value')); setting.set('originalValue', setting.get('value'));
}
}); });
} }
}); });

View File

@ -166,19 +166,17 @@ Discourse = Ember.Application.createWithMixins({
**/ **/
logout: function() { logout: function() {
Discourse.KeyValueStore.abandonLocal(); Discourse.KeyValueStore.abandonLocal();
return Discourse.ajax(Discourse.getURL("/session/") + this.get('currentUser.username'), { Discourse.ajax(Discourse.getURL("/session/") + this.get('currentUser.username'), {
type: 'DELETE', type: 'DELETE'
success: function(result) { }).then(function() {
// To keep lots of our variables unbound, we can handle a redirect on logging out. // Reloading will refresh unbound properties
window.location.reload(); window.location.reload();
}
}); });
}, },
authenticationComplete: function(options) { authenticationComplete: function(options) {
// TODO, how to dispatch this to the view without the container? // TODO, how to dispatch this to the view without the container?
var loginView; var loginView = Discourse.__container__.lookup('controller:modal').get('currentView');
loginView = Discourse.__container__.lookup('controller:modal').get('currentView');
return loginView.authenticationComplete(options); return loginView.authenticationComplete(options);
}, },

View File

@ -64,7 +64,7 @@ Discourse.Onebox = {
var onebox = this; var onebox = this;
promise.then(function(html) { promise.then(function(html) {
// successfully loaded onebox // loaded onebox
loadingFinished(); loadingFinished();
onebox.localCache[url] = html; onebox.localCache[url] = html;