From 8888ae4b402781b1017efddb17617f83c24dfa1c Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 10 Jul 2013 14:56:00 -0400 Subject: [PATCH] Added some macros to simplify code --- .../discourse/components/computed.js | 17 ++++++++++++++ .../discourse/helpers/i18n_helpers.js | 7 ++---- .../discourse/templates/post.js.handlebars | 2 +- .../discourse/views/actions_history_view.js | 10 +++----- .../views/buttons/dropdown_button_view.js | 6 ++--- .../views/buttons/favorite_button.js | 4 +--- .../discourse/views/hotness_view.js | 11 ++------- .../discourse/views/input_tip_view.js | 14 +++-------- .../discourse/views/nav_item_view.js | 23 ++++++++++--------- .../discourse/views/post_menu_view.js | 17 +++++++++----- .../discourse/views/raw_div_view.js | 8 +++---- .../discourse/views/topic_closing_view.js | 8 +++---- .../discourse/views/topic_status_view.js | 7 ++---- .../views/topic_summary/topic_summary_view.js | 6 ++--- .../views/user/activity_filter_view.js | 5 +--- .../javascripts/discourse/views/view.js | 15 ++++++++++++ app/serializers/post_serializer.rb | 4 ++-- 17 files changed, 82 insertions(+), 82 deletions(-) create mode 100644 app/assets/javascripts/discourse/components/computed.js diff --git a/app/assets/javascripts/discourse/components/computed.js b/app/assets/javascripts/discourse/components/computed.js new file mode 100644 index 000000000..c97ead1ee --- /dev/null +++ b/app/assets/javascripts/discourse/components/computed.js @@ -0,0 +1,17 @@ +Discourse.computed = { + + /** + Returns whether two properties are equal to each other. + + @method propertyEqual + @params {String} p1 the first property + @params {String} p2 the second property + @return {Function} computedProperty function + **/ + propertyEqual: function(p1, p2) { + return Ember.computed(function() { + return this.get(p1) === this.get(p2); + }).property(p1, p2); + } + +} diff --git a/app/assets/javascripts/discourse/helpers/i18n_helpers.js b/app/assets/javascripts/discourse/helpers/i18n_helpers.js index 2027296e5..b6b7af10f 100644 --- a/app/assets/javascripts/discourse/helpers/i18n_helpers.js +++ b/app/assets/javascripts/discourse/helpers/i18n_helpers.js @@ -37,14 +37,11 @@ Ember.Handlebars.registerHelper('i18n', function(property, options) { Ember.Handlebars.registerHelper('countI18n', function(key, options) { var view = Discourse.View.extend({ tagName: 'span', + shouldRerender: Discourse.View.renderIfChanged('countChanged'), render: function(buffer) { buffer.push(I18n.t(key, { count: this.get('count') })); - }, - - countChanged: function() { - this.rerender(); - }.observes('count') + } }); return Ember.Handlebars.helpers.view.call(this, view, options); diff --git a/app/assets/javascripts/discourse/templates/post.js.handlebars b/app/assets/javascripts/discourse/templates/post.js.handlebars index a9f990647..57323e820 100644 --- a/app/assets/javascripts/discourse/templates/post.js.handlebars +++ b/app/assets/javascripts/discourse/templates/post.js.handlebars @@ -60,7 +60,7 @@
{{collection contentBinding="internalLinks" itemViewClass="Discourse.PostLinkView" tagName="ul" classNames="post-links"}} - {{#if controller.details.can_reply_as_new_topic}} + {{#if topic.details.can_reply_as_new_topic}} {{i18n post.reply_as_new_topic}} {{/if}}
diff --git a/app/assets/javascripts/discourse/views/actions_history_view.js b/app/assets/javascripts/discourse/views/actions_history_view.js index c43f6b4b1..8ca6aee17 100644 --- a/app/assets/javascripts/discourse/views/actions_history_view.js +++ b/app/assets/javascripts/discourse/views/actions_history_view.js @@ -11,18 +11,14 @@ Discourse.ActionsHistoryView = Discourse.View.extend({ tagName: 'section', classNameBindings: [':post-actions', 'hidden'], - hidden: (function() { - return this.blank('content'); - }).property('content.@each'), + hidden: Em.computed.empty('content'), - usersChanged: (function() { - return this.rerender(); - }).observes('content.@each', 'content.users.@each'), + shouldRerender: Discourse.View.renderIfChanged('content.@each', 'content.users.@each'), // This was creating way too many bound ifs and subviews in the handlebars version. render: function(buffer) { - if (!this.present('content')) return; + return this.get('content').forEach(function(c) { var actionString, iconsHtml; buffer.push("
"); diff --git a/app/assets/javascripts/discourse/views/buttons/dropdown_button_view.js b/app/assets/javascripts/discourse/views/buttons/dropdown_button_view.js index 9defe80f1..9d9e8bfc6 100644 --- a/app/assets/javascripts/discourse/views/buttons/dropdown_button_view.js +++ b/app/assets/javascripts/discourse/views/buttons/dropdown_button_view.js @@ -10,6 +10,8 @@ Discourse.DropdownButtonView = Discourse.View.extend({ classNames: ['btn-group'], attributeBindings: ['data-not-implemented'], + shouldRerender: Discourse.View.renderIfChanged('text', 'longDescription'), + didInsertElement: function(e) { // If there's a click handler, call it if (this.clicked) { @@ -26,10 +28,6 @@ Discourse.DropdownButtonView = Discourse.View.extend({ this.$('ul li').off('click.dropdown-button'); }, - textChanged: function() { - this.rerender(); - }.observes('text', 'longDescription'), - render: function(buffer) { buffer.push("

" + this.get('title') + "

"); buffer.push("