Category custom field value strage behaviour in category edit form


(Janno Liivak) #1

I’m about to create a plugin adding a checkbox to Category settings. All is fine with the field until I uncheck the field. In database I can see that value of custom field is set to “false” but as I reopen category settings the checkbox is now checked although the value is still false. Is there something I’m missing?

Here’s the custom field…

~/bump-topic/assets/javascripts/discourse/pre-initializers/extend-for-bumping.js.es6

import Category from 'discourse/models/category';
import computed from 'ember-addons/ember-computed-decorators';

export default {
  name: 'extend-for-bumping',
  before: 'inject-discourse-objects',
  initialize() {

    Category.reopen({

      @computed('custom_fields.enable_bump_topic')
      enable_bump_topic: {
        get(value) {
          return value === "true";
        },
        set(value) {
          value = value ? "true" : "false";
          this.set("custom_fields.enable_bump_topic", value);
          return value;
        }
      }
    });
  }
};

And the interface part…

~/bump-topic/assets/javascripts/discourse/templates/connectors/category-custom-settings/bump-topic.hbs

{{#if this.siteSettings.bump_topic_enabled}}
<section class='field'>
  <div class="enable-bump-topic">
    <label class="checkbox-label">
      {{input type="checkbox" checked=category.custom_fields.enable_bump_topic}}
      {{i18n 'bump_topic.enable_bump_topic'}}
    </label>
  </div>
</section>
{{/if}}

(SMHassanAlavi) #2

I have a question.
what should we do for adding a custom field to a category?
1.Using plugin.rb:
Category.register_custom_field_type('tracking_number', :integer)
2.using the code above.
Which one should we use?


(Evgeny) #3

Began writing the plugin, used the 2 files and they work.
The hardest part for me to understand how this field should be used further.

import property from 'ember-addons/ember-computed-decorators';
import Category from 'discourse/models/category';

export default {
  name: 'extend-category-for-temp',
  before: 'inject-discourse-objects',
  initialize() {

    Category.reopen({

      @property('custom_fields.enable_topic_temp')
      enable_topic_temp: {
        get(enableField) {
          return enableField === "true";
        },
        set(value) {
          value = value ? "true" : "false";
          this.set("custom_fields.enable_topic_temp", value);
          return value;
        }
      }

    });
  }
};

and

<section class='field'>
  <div class="enable-topic-temp">
    <label class="checkbox-label">
      {{input type="checkbox" checked=category.enable_topic_temp}}
      {{i18n 'temp.allow_temp'}}
    </label>
  </div>
</section>

(Janno Liivak) #4

I’m not sure what’s a question here? … In case you would like a showcase, I managed to complete my plugin and it’s available at:

Maybe you can get ideas from my code :slight_smile: