Code Monkey home page Code Monkey logo

Comments (7)

johno avatar johno commented on May 22, 2024

I'm wondering if you already have an approach you're considering for handling the global configuration for the service.

My initial idea for an approach was just to expand upon the defaultX properties like so:

defaultTimeout() {
  return this._getGlobalDefaults().timeout || 3000;
},

// ... 

_getGlobalDefaults() {
  return Ember.ENV.flashMessageDefaults || {};
}

This has the benefit of working with the existing API (which I know is used by the flash object itself). But, it's a bit verbose, too. Is this similar to what you were intending @poteto? Or were you intending on going a different route?

from ember-cli-flash.

poteto avatar poteto commented on May 22, 2024

I'm happy to rip out internal API for something better! I don't mind verbosity as long as it's understandable. Originally, I was going to do something like this:

// pseudo
import Ember from 'ember';

const defaults = {
  timeout      : 3000,
  priority     : 100,
  sticky       : false,
  showProgress : false,
  type         : 'info'
};

export default Ember.Service.extend({
  _setDefaults: on('init', function() {
    const { flashMessageDefaults: userDefaults } = Ember.ENV;

    Object.keys(defaults).forEach((key) => {
      const classifiedKey    = key.classify();
      const defaultKey       = `default${classifiedKey}`;
      const defaultValue     = defaults[key];
      const userDefaultValue = userDefaults[key];
      let valueToSet         = userDefaultValue ? userDefaultValue : defaultValue;

      set(this, defaultKey, valueToSet);
    });
  })
});

The idea is to still allow people to set those defaults directly on the service, e.g.

get(this, 'flashes').set('defaultTimeout', 5000);

from ember-cli-flash.

johno avatar johno commented on May 22, 2024

Yeah, my proposed approach would be problematic because it wouldn't allow people to set the default directly. Observing init is a great approach. Though, I'd imagine we can merge defaults and userDefaults to clean up the loop?

export default Ember.Service.extend({

  // ...

  _getDefaults() {
   const serviceDefaults = {
      timeout      : 3000,
      priority     : 100,
      sticky       : false,
      showProgress : false,
      type         : 'info'
    };

    const { flashMessageDefaults: userDefaults } = config;
    return Ember.$.extend({}, userDefaults, serviceDefaults);
  },

  _setDefaults: on('init', function() {
    var defaults = this._getDefaults();

    Object.keys(defaults).map((key) => {
      const classifiedKey = key.classify();
      const defaultKey    = `default${classifiedKey}`;

      set(this, defaultKey, defaults[key]);
    });
  })
});

from ember-cli-flash.

poteto avatar poteto commented on May 22, 2024

Yeah, that's a lot cleaner. We can probably also use Ember.merge instead of jQuery, I believe

from ember-cli-flash.

johno avatar johno commented on May 22, 2024

Oh sweet, TIL!

from ember-cli-flash.

poteto avatar poteto commented on May 22, 2024

Teamwork!

from ember-cli-flash.

poteto avatar poteto commented on May 22, 2024

Closed in #36.

from ember-cli-flash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.