component / reactive

Tiny reactive template engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Preprocessing data

timoxley opened this issue · comments

Considering we already have "post-processing" filters via data-attr="data | filter", perhaps it makes sense to be able to pre-process data as well.

For example, say all Dates should be formatted in a particular way, rather than having to add this filter to every date output in my template, I could do something more generic, such as:

reactive(el, data, view)
.filter(function(content) {
   if (content instanceof Date) return moment(content).format('LLL')
   return content
})

or perhaps I want to handle undefined/null in a particular way (this solves #11, #8):

reactive(el, data, view)
.filter(function(content) {
   if (content == null) return ''
})

Possibly related to #6.

hmm yeah maybe! I suppose you could mostly do this with the view sub"class" / mixin thing

though that still has the problem of necessarily requiring at least one |. This starts to get unweildy if you have multiple 'default' formatters: e.g. date formats dates and clean handles null/undefined properties (perhaps the date formatter returns null for invalid dates).

<div class="row">
  <span class="label">Planned</span>
  <span data-text="planned_start_date | date | clean"></span>
  <span data-text="planned_finish_date | date | clean"></span>
</div>

I'm thinking about this like middleware… these formatters are like route middleware, and the problem is we don't yet have any global middleware.

it doesn't have to be a formatter, you can re-define the property at the view level with a method, that's usually what I do, you could for example do

MyView.prototype.created_at =
MyView.prototype.updated_at = function(...

tough call, I haven't used it enough yet to get a better idea of what we could use, in our app at least things are pretty localized so we dont have a lot of global-ish state like that

these should be fine sync, maybe we should just emit some events, some generic some granular so you can cap into whatever you need for sweeping changes