diff --git a/app/assets/javascripts/app/app.js b/app/assets/javascripts/app/app.js index 6ed0a220d..9125703cd 100644 --- a/app/assets/javascripts/app/app.js +++ b/app/assets/javascripts/app/app.js @@ -19,7 +19,16 @@ var app = { views: {}, pages: {}, forms: {}, - vent: _.extend({}, Backbone.Events), + + // global event broker - use event names in the form of "object:action:data" + // [object]: the class of the acting object + // [action]: infinitive verb naming the performed action + // [data]: (optional) unique name or ID of the specific instance + // e.g. "person:ignore:123" + // if your event has to pass more than one datum (singular) - or in case you + // need structured data - specify them as arguments to the `#trigger` call + // e.g. `app.events.trigger('example:event', {more: 'data'})` + events: _.extend({}, Backbone.Events), user: function(userAttrs) { if(userAttrs) { return this._user = new app.models.User(userAttrs) } diff --git a/app/assets/javascripts/app/models/post.js b/app/assets/javascripts/app/models/post.js index 51623c5ae..74a3cec90 100644 --- a/app/assets/javascripts/app/models/post.js +++ b/app/assets/javascripts/app/models/post.js @@ -27,6 +27,14 @@ app.models.Post = Backbone.Model.extend(_.extend({}, app.models.formatDateMixin, return this.get("author") }, + blockAuthor: function() { + var personId = this.get("author").id; + var block = new app.models.Block(); + + return block.save({block : {person_id : personId}}) + .done(function(){ app.events.trigger('person:block:'+personId); }); + }, + toggleFavorite : function(options){ this.set({favorite : !this.get("favorite")}) diff --git a/app/assets/javascripts/app/views/stream_post_views.js b/app/assets/javascripts/app/views/stream_post_views.js index 1699f326d..6993bd79e 100644 --- a/app/assets/javascripts/app/views/stream_post_views.js +++ b/app/assets/javascripts/app/views/stream_post_views.js @@ -28,7 +28,7 @@ app.views.StreamPost = app.views.Post.extend({ initialize : function(){ var personId = this.model.get('author').id; - app.vent.on('remove:author:posts:'+personId, this.remove, this); + app.events.on('person:block:'+personId, this.remove, this); this.model.on('remove', this.remove, this); //subviews @@ -75,14 +75,13 @@ app.views.StreamPost = app.views.Post.extend({ if(evt) { evt.preventDefault(); } if(!confirm(Diaspora.I18n.t('ignore_user'))) { return } - var personId = this.model.get("author").id; - var block = new app.models.Block(); - - block.save({block : {person_id : personId}}, { - success : function(){ - app.vent.trigger('remove:author:posts:'+personId); - } - }) + this.model.blockAuthor() + .fail(function() { + Diaspora.page.flashMessages.render({ + success: false, + notice: Diaspora.I18n.t('ignore_failed') + }); + }); }, remove : function() { diff --git a/config/locales/javascript/javascript.en.yml b/config/locales/javascript/javascript.en.yml index a8e9ea909..222b48a83 100644 --- a/config/locales/javascript/javascript.en.yml +++ b/config/locales/javascript/javascript.en.yml @@ -16,6 +16,7 @@ en: created: "The report was successfully created" exists: "The report already exists" ignore_user: "Ignore this user?" + ignore_failed: "Unable to ignore this user" and: "and" comma: "," edit: "Edit" diff --git a/spec/javascripts/app/views/stream_post_spec.js b/spec/javascripts/app/views/stream_post_spec.js index a5793d9ab..57fbcb09c 100644 --- a/spec/javascripts/app/views/stream_post_spec.js +++ b/spec/javascripts/app/views/stream_post_spec.js @@ -21,7 +21,7 @@ describe("app.views.StreamPost", function(){ it("setup remove:author:posts:#{id} to #remove", function(){ spyOn(_PostViewClass.prototype, 'remove'); view = new _PostViewClass({model : this.statusMessage}); - app.vent.trigger('remove:author:posts:'+authorId); + app.events.trigger('person:block:'+authorId); expect(_PostViewClass.prototype.remove).toHaveBeenCalled(); }); });