Using save for report model
Instead of checking the status code I am using success and error callbacks from model-save. In that case we have to return json in the controller for signaling that the request was sucessfully.
This commit is contained in:
parent
1a4ab274a3
commit
6f65ef8437
3 changed files with 24 additions and 28 deletions
|
|
@ -1,3 +1,4 @@
|
|||
app.models.Report = Backbone.Model.extend({
|
||||
urlRoot: '/report',
|
||||
type: 'POST'
|
||||
});
|
||||
|
|
|
|||
|
|
@ -84,33 +84,30 @@ app.views.Base = Backbone.View.extend({
|
|||
report: function(evt) {
|
||||
if(evt) { evt.preventDefault(); }
|
||||
var msg = prompt(Diaspora.I18n.t('report.prompt'), Diaspora.I18n.t('report.prompt_default'));
|
||||
if (msg == null) return;
|
||||
if (msg == null) {
|
||||
return;
|
||||
}
|
||||
var data = {
|
||||
report: {
|
||||
post_id: this.model.id,
|
||||
post_type: $(evt.currentTarget).data("type"),
|
||||
text: msg
|
||||
}
|
||||
};
|
||||
|
||||
var report = new app.models.Report();
|
||||
var id = this.model.id;
|
||||
var type = $(evt.currentTarget).data("type");
|
||||
|
||||
report.fetch({
|
||||
data: {
|
||||
report: {
|
||||
post_id: id,
|
||||
post_type: type,
|
||||
text: msg
|
||||
}
|
||||
report.save(data, {
|
||||
success: function(model, response) {
|
||||
Diaspora.page.flashMessages.render({
|
||||
success: true,
|
||||
notice: Diaspora.I18n.t('report.status.created')
|
||||
});
|
||||
},
|
||||
type: 'POST',
|
||||
statusCode: {
|
||||
200: function(xhr) {
|
||||
Diaspora.page.flashMessages.render({
|
||||
success: true,
|
||||
notice: Diaspora.I18n.t('report.status.created')
|
||||
});
|
||||
},
|
||||
400: function(xhr) {
|
||||
Diaspora.page.flashMessages.render({
|
||||
success: false,
|
||||
notice: Diaspora.I18n.t('report.status.exists')
|
||||
});
|
||||
}
|
||||
error: function(model, response) {
|
||||
Diaspora.page.flashMessages.render({
|
||||
success: false,
|
||||
notice: Diaspora.I18n.t('report.status.exists')
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
|
|
|||
|
|
@ -29,10 +29,8 @@ class ReportController < ApplicationController
|
|||
def create
|
||||
report = current_user.reports.new(report_params)
|
||||
if report.save
|
||||
flash.now[:notice] = I18n.t 'report.status.created'
|
||||
render :nothing => true, :status => 200
|
||||
render :json => true, :status => 200
|
||||
else
|
||||
flash.now[:error] = I18n.t 'report.status.failed'
|
||||
render :nothing => true, :status => 409
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue