The first one is a lot cleaner and simpler so I definitely prefer it.
Yeah, that is my common pattern, too. Are there any performance differences between these two? If I got it right, the second one isnāt reactive, but the first one is. So Iām wondering if I have some more of this properties which arenāt needed for any template displaying issues, would they slow down my app on low performance devices?
In theory yes, in practice no. It falls under the category of āit just doesnāt matterā.
I donāt understand. If the dependencies donāt change then the autorun shouldnāt execute.
@manuel just btw, Iām getting this in my browser console
Don't have debug information for [T#createViewModel]. Please report it at https://viewmodel.org/help. In the mean time you can turn off checks with `ViewModel.ignoreErrors = true`. See https://viewmodel.org/docs/misc#ignoreErrors for more information.
Iām using 2.9.3 and set persists to false.
Donāt update viewmodel-debug. Itās for the latest ViewModel.
Ah okay, but after upgrading to v4 I get:
Don't have debug information for [$default]. Please report it at https://viewmodel.org/help. In the mean time you can turn off checks with `ViewModel.ignoreErrors = true`. See https://viewmodel.org/docs/misc#ignoreErrors for more information.
What versions are you using?
Ah sorry, I had to remove and add it again, now works fine, thanks.
Probs with readonly
html attr here:
ViewModel.addAttributeBinding(['disabled', 'readonly','tabindex'])
input(maxlength='30' autocomplete="off" $b= "tabindex: tabindex, readonly: readonly")
This produces a readonly input, which was not my intent.
<input maxlength="30" autocomplete="off" b-id="60" tabindex="0" readonly="undefined">
My intent was to control whether input is readonly via setting viewmodel.readonly(true)
How can I remove this attribute completely when I donāt need it and set it when I do need it?
The attribute binding sets the text of an attribute to what the property contains. It has no notion of special attributes which need to be removed if certain conditions are met.
You would have to add a custom binding:
ViewModel.addBinding({
name: 'readonly',
autorun(bindArg){
const isReadOnly = bindArg.getVmValue();
if (isReadOnly) {
bindArg.element.attr('readonly', 'readonly');
} else {
bindArg.element.removeAttr('readonly');
}
}
});
Does it make sense to publish single reusable bindings as npm packages?
For small stuff like this, maybe. It would be like underscore or jQuery, some people will like to have a grab bag of bindings, others wonāt.
Where I see the most value is in libraries of bindings specifically designed for CSS frameworks like Bootstrap, Semantic UI, etc. Things to make it easier to produce lists, dropdowns, modals, etc.
Unfortunately I donāt have time to do either.
Thanks, Manuel, youāre a real lifesaver!
btw, seems like readonly
attr shouldnt be present in the list of common attr bindings as it calls for special treatment:
But would you like to?
Iāll update the docs.
But would you like to?
Sure, why not.
Hey @manuel,
is there a common pattern if I want to close a notification list if the user clicks anywhere outside of the document (like on facebook)? At the moment Iām trying to use signals, it recognizes the click but it collides with my binded click events on the single notification icons.
So If a user clicks on the message icon, I set showMessages(true)
[binding] but also setting showMessages(false)
[signal], when the document click signal is fired.
I donāt think I follow. Make me a repro.
I was wondering about similar thing when I was rewriting new guide Todos app to use Viewmodel. It uses original javascript focus and blur events which work in a different way than Viewmodel focus binding. Thereās an option to go with {{on "focus: function()"}}
instead of {{b "focus: function()"}}
but I wondered back then if there is a better way to rewrite that part.
Iāve just used added a click binding on the wrapper, so if the user clicks anywhere on the page, all notifications will be closed (I am calling the notification VM via ViewModel.findOne(ānotificationsā).closeAll())
I really want a repro so weāre in the same page.