Sorry, my questions are probably confusing the situation a bit more, but basically the āTemplate.greeting.helperā is controlled by the package, Iām able to extend it with a viewmodel, but still need to access the code contained in the Template.greeting.helper (if possible).
Itās seeming like in this case it might be easier to not use viewmodel on this page.
Thanks for the prompt responses! And apologies for any confusion!
Back to my original response, I donāt think you can access a helper outside the markup if you canāt modify the way the helper is defined. This isnāt a VM issue, youāre going to run into the same problem without VM.
My Blaze is definitely rusty so ask the question in this forum, someone might know the answer: āHow can I access a helper method from code (I have the template instance) if I canāt modify the way the helper is defined (itās in a 3rd party package)?ā
Helper map for a template can be accessed from within a viewmodel at this.templateInstance.view.template.__helpers.
So, you can get in your example firstName helper:
// get function
let firstNameFunction = this.templateInstance.view.template.__helpers.get('firstName');
// invoke function
let firstName = this.templateInstance.view.template.__helpers.get('firstName')();
@manuel, I was wondering - is there a way to add a generic property to all viewmodels? Similar to a mixin only without declaring a mixin in every viewmodel?
Yes it is a debug tool. Cool one to be clear.
It pretty much smillar to redux store view, except, it allows full control over viewmodel during development.
Such as setting inputs, data, props, firing events and etc. When you include it in your app, look for new UI el.
I donāt remember the last time I used VM Explorer for Blaze so I donāt know if it even works anymore. My guess is that it doesnāt work for VM Blaze v2 (but of course Iāve been wrong before). The React version works similarly and you can see how it works at https://viewmodel.org by clicking on the paper plane icon:
One way to look at VM for Blaze is as being part of the old Meteor stack. Blaze itself is pretty much frozen and so is VM for Blaze. VM is pretty much feature complete so itās just a matter of fixing eventual bugs. So āaging gracefullyā is the most appropriate term
Btw, Manuel - does VM feature set (by any chance) include a way to profile what triggered reactive update to vm property or caused vm method to rerun? Iād like to log that data for development purposesā¦
Thatās a tough one. I donāt have a good answer. Itās technically possible for VM to log when properties are changed and also when functions rerun due to a change in their dependencies, but Iām not going to code that in any time soon.
You can do a quick and dirty monkey patch by updating the loadProperties method to wrap the functions in a another that logs when the function is about to run:
@loadProperties = (toLoad, container) ->
loadObj = (obj) ->
for key, value of obj when not ViewModel.properties[key]
if ViewModel.reserved[key]
throw new Error "Can't use reserved word '" + key + "' as a view model property."
else
if _.isFunction(value)
# we don't care, just take the new function
container[key] = value
# Instead do something like:
#
# wrapped = (args...) ->
# console.log "About to run: " + key
# value args
#
# And then:
# container[key] = wrapped
And also funProp:
funProp = (value) ->
if arguments.length
if _value isnt value
changeValue = ->
# console.log "Going to change " + _value + " to " + value
It will probably be a good idea to pass the property name to makeReactiveProperty to log it too. Right now funProps are anonymous.
Incidentally, @manuel, I was thinking about a scenario that I could not use viewmodel forā¦
Imagine a mortgage calculator, where number of years affects the monthly payment, AND the monthly payment might be set by the user to a lower value, and expect that the # of years would changeā¦ In this case, which field is the raw one, and which is the derived?
Whatās solved this for me is that redux āsmearsā the state out over time. I write a reducer that changes the state underneath, depending on which quantity the user wants to adjust. Then I have a pure-function renderer which, minimally, updates the DOM based on which parts have changed.
Itās been interesting to work more with Reactā¦ I was just looking back to see if I wanted to use viewmodel for my current use case, and thought itād be worth an ask if you saw a way to do this in VM. (perhaps by grabbing events on derived properties, and turning them into changes on the āleafā propertiesā¦)