Skip to content

dovrosenberg/meteor-astronomy-displayFormat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Astronomy Display Formatting

The Astronomy package extends your Mongo documents with functionalities defined in the schema. It's the model layer (in MVC pattern) for Meteor or for people coming from relational databases environment, it's the Object-relational mapping system (ORM).

This module adds the ability to specify a default formatting function for each field in a schema (or each type in a schema) for use in the view layer.

Installation

$ meteor add jagi:astronomy

if you haven't added it already. Then

$ meteor add dovrosenberg:astronomy-displayformats

Usage

Refer to the Astronomy package for detailed explanations of the underlying framework.

WARNING: As of version 1.0.0, the attribute used is called displayFormat, rather than format.

Defining formats

Formats are defined in a manner very similar to types.

Astro.createDisplayFormat({
  name: 'currency',
  displayFormat: function(value) {
    return "$" + value;
  }
});

You can then apply this format to a field:

Item = Astro.Class({
  name: 'Item',
  collection: Items,
  fields: {
     price: {
        type: 'number',
        displayFormat: 'currency'
    }
  }
});

var item = new Item({price:14});
console.log(item.getFormatted('price'));  // outputs $14

You can also tie a format to a specific type:

Astro.createDisplayFormat({
  name: 'currency',
  displayFormat: function(value) {
    return "$" + value;
  },
  defaultForTypes: ['number']
});

Item = Astro.Class({
  name: 'Item',
  collection: Items,
  fields: {
     price: {
        type: 'number'  // note: no 'format' defined
    }
  }
});

var item = new Item({price:14});
console.log(item.getFormatted('price'));  // still outputs $14

This default format for a type will be overridden by any format applied to a specific field.

Getting formatted values

This module adds to each class a getFormatted() function that mirrors the get() function, but returns a formatted value.

// Return the formatted value of the "phone" field
obj.getFormatted('phone');
// Return only "title" and "commentsCount" fields' formatted values.
obj.getFormatted(['title', 'commentsCount']);
// Returns object with all fields and their corresponding formatted values
obj.getFormatted();

All of these methods trigger beforeget and afterget events as expected.

Parameters

Finally, any format function can take second parameter:

Astro.createDisplayFormat({
  name: 'currency',
  displayFormat: function(value, decimals) {
     ...
  }
});

And the value is passed in simply as:

item.getFormatted('price',2);

This second value can also be useful to allow for specifying different formats for different circumstances (for example on inputs vs on display):

Astro.createDisplayFormat({
  name: 'currency',
  displayFormat: function(value, formatToUse) {
    switch (formatToUse) {
      ...
    }
  }
});

License

MIT

About

Display formatting module for jagi:meteor-astronomy

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published