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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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