I have a different implementation of cross-component communication here http://jsfiddle.net/c641oog2/ than what's described here: http://lhorie.github.io/mithril/components.html#librarization. The goal is to create an easily integrable & scalable (to be re-used in other components) components, i.e librarization.
Main parts of my code:
var autocompleter = function(options) {
...
autocompleter.vm = {
list: m.prop(options.list || []),
observers: m.prop(options.observers || []),
...
select: function(item) {
for(observer in autocompleter.vm.observers()) {
autocompleter.vm.observers()[observer](item); //notify all observers of selection
}
}
//initialization later on...
this.userAC = new autocompleter({list: this.users(), observers: [this.selectedUser]})
The main difference is in how the components communicate with each other. In my implementation, I decided to use observers, and in the documentation's implementation, he has implemented by creating pure functions which are then used in the "view" function of dashboard, where correct arguments are passed to the "view" function of the autocomplete function.
My questions:
- If you had to pick between these two implementation, why would you pick one over the other?
- In the functional programming model, is an OOP concept such as the observer pattern frowned upon?
- Is there a more terse but scalable way to implement this in either FP / using a different pattern?