Hi guys,
within “Blaze Components” or within Meteor in general:
How do I generically run multiple autoruns?
Is this possible at all, or do I have to hardcore?
@mitar : Do you have an idea how to do this?
For example:
// Let's link some "aldeed:Autoform-fields" with an ReactiveDict
ExampleComponent extends BlazeComponent {
onCreated() {
var fieldNamesArray = ['name', 'surename', 'country'];
this.filterDict = new ReactiveDict('filter');
fieldNamesArray.forEach(function(element, index) {
// Generic Autorun (this THROWS an ERROR right now)
this.autorun(this.filterDict.set(element, AutoForm.getFieldValue(element, 'formId')));
});
}
}
shock
November 4, 2015, 6:13pm
2
onCreated() {
self = this;
…
self.autorun(self.filterDict.set(element, AutoForm.getFieldValue(element, 'formId')));
1 Like
@shock : Yeah this works! Thanks a lot for pointing this out!
But I actually stumbled upon another problem: Is it possible to use autorun within a mixin’s.onCreated?
This code is giving me problems:
Is it because the mixin does not have its own template?
Is there a way to make this work within the mixin?
class MixinComponent extends BlazeComponent {
onCreated() {
var self = this;
// THIS THROWS ERROR "Uncaught Error: The component has to be created before calling 'autorun'"
self.autorun(function() {console.log('autorun MixinComponent')});
}
}
class ExampleComponent extends BlazeComponent {
mixins() {
return [MixinComponent];
}
template() {
return 'ExampleComponent';
}
onCreated() {
var self = this;
self.autorun(function() {console.log('autorun ExampleComponent')});
}
}
ExampleComponent.register('ExampleComponent');
Ahhh… I think I got it: seems like self.mixinParent().autorun(function() {console.log('autorun MixinComponent')});
does the job!
alternative, you could use a ES6 fat arrow here, as it presents a lexical this
fieldNamesArray.forEach((element, index) => {
this.autorun(this.filterDict.set(element, AutoForm.getFieldValue(element, 'formId')));
});
1 Like
mitar
November 4, 2015, 11:02pm
6
In my apps I have my own base classes for mixins, so that it is simpler to use them:
class UIComponent extends CommonComponent
storageUrl: (filename, kwargs) ->
Storage.url filename
isSandstorm: ->
!!__meteor_runtime_config__.SANDSTORM
$eq: (args...) ->
# Removing kwargs.
args.pop() if args[args.length - 1] instanceof Spacebars.kw
first = args[0]
for arg in args[1..] when not EJSON.equals first, arg
return false
true
formatStartEndDate: (start, end, datetimeFormat, timeFormat) ->
start = null if start instanceof Spacebars.kw
end = null if end instanceof Spacebars.kw
This file has been truncated. show original
Also, for your case I would simple use computed field API.
@mitar : Thanks a lot for your response! So far a really really love your “Blaze Components”! It helps SO MUCH to structure my project!! BIG THUMBS UP!!!
Does the “computed field” package allow me to a single instance as a dictionary?
I need to be able to use it like this:
thebarty:
fieldNamesArray.forEach(function(element, index) {
// Generic Autorun (this THROWS an ERROR right now)
this.autorun(this.filterDict.set(element, AutoForm.getFieldValue(element, ‘formId’)));
});
Thats a great idea! You should put that into the docs and make it part of the framework.
mitar
November 5, 2015, 8:35am
9
Why you need filterDict
and would not just have a set of fields instead?
mitar
November 5, 2015, 8:37am
10
I want to keep core minimal. But there is already a ticket to publish examples of such base classes and mixins: https://github.com/peerlibrary/meteor-blaze-components/issues/86
But yes, the recommended way is to create your own base class and then use that in your components. And then you can add any common stuff there. Both for components and mixins.