refactor global event broker and user blocking from stream posts

This commit is contained in:
Florian Staudacher 2014-08-17 13:52:35 +02:00
parent 163b76ae3d
commit 4fd9bae056
5 changed files with 28 additions and 11 deletions

View file

@ -19,7 +19,16 @@ var app = {
views: {}, views: {},
pages: {}, pages: {},
forms: {}, 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) { user: function(userAttrs) {
if(userAttrs) { return this._user = new app.models.User(userAttrs) } if(userAttrs) { return this._user = new app.models.User(userAttrs) }

View file

@ -27,6 +27,14 @@ app.models.Post = Backbone.Model.extend(_.extend({}, app.models.formatDateMixin,
return this.get("author") 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){ toggleFavorite : function(options){
this.set({favorite : !this.get("favorite")}) this.set({favorite : !this.get("favorite")})

View file

@ -28,7 +28,7 @@ app.views.StreamPost = app.views.Post.extend({
initialize : function(){ initialize : function(){
var personId = this.model.get('author').id; 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); this.model.on('remove', this.remove, this);
//subviews //subviews
@ -75,14 +75,13 @@ app.views.StreamPost = app.views.Post.extend({
if(evt) { evt.preventDefault(); } if(evt) { evt.preventDefault(); }
if(!confirm(Diaspora.I18n.t('ignore_user'))) { return } if(!confirm(Diaspora.I18n.t('ignore_user'))) { return }
var personId = this.model.get("author").id; this.model.blockAuthor()
var block = new app.models.Block(); .fail(function() {
Diaspora.page.flashMessages.render({
block.save({block : {person_id : personId}}, { success: false,
success : function(){ notice: Diaspora.I18n.t('ignore_failed')
app.vent.trigger('remove:author:posts:'+personId); });
} });
})
}, },
remove : function() { remove : function() {

View file

@ -16,6 +16,7 @@ en:
created: "The report was successfully created" created: "The report was successfully created"
exists: "The report already exists" exists: "The report already exists"
ignore_user: "Ignore this user?" ignore_user: "Ignore this user?"
ignore_failed: "Unable to ignore this user"
and: "and" and: "and"
comma: "," comma: ","
edit: "Edit" edit: "Edit"

View file

@ -21,7 +21,7 @@ describe("app.views.StreamPost", function(){
it("setup remove:author:posts:#{id} to #remove", function(){ it("setup remove:author:posts:#{id} to #remove", function(){
spyOn(_PostViewClass.prototype, 'remove'); spyOn(_PostViewClass.prototype, 'remove');
view = new _PostViewClass({model : this.statusMessage}); view = new _PostViewClass({model : this.statusMessage});
app.vent.trigger('remove:author:posts:'+authorId); app.events.trigger('person:block:'+authorId);
expect(_PostViewClass.prototype.remove).toHaveBeenCalled(); expect(_PostViewClass.prototype.remove).toHaveBeenCalled();
}); });
}); });