samedi 3 octobre 2015

Class (or namespace) functions in Ember

I have an Ember model class that has a definition like so:

export default Ember.Object.extend({
...
});

And below it (in the same file) is a utility function that the class makes use of:

function padDigits(number, digits) {
    return new Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
}

But, keeping this in the model class is a poor strategy. It should be moved to a utility file or class.

I would like to be able to do something like:

var paddedNum = Numbers.padDigits(theNum, 2);

But I'm looking for documentation on how to either (1) create a Numbers class that has "static" functions so I don't need to call Numbers.create().padDigits(...), or (2) namespace my utility functions so that they can be only accessed if prefixed properly, like:

import Numbers from '../utils/numbers';

This may be more of a Javascript question than an Ember question, but I'm trying to accomplish this in the context of an Ember CLI app.




Aucun commentaire:

Enregistrer un commentaire