Replies: 3 comments
-
Swapping out an instance's template is a pretty heavy thing to do, but there is a feature that's designed to handle dynamism related to templates. If you need to swap bits of template around often or to something dynamically created, I'd recommend using partials. Not only can a partial be changed by name, a partial can render an inline chunk of template from the data. In that example, whatever template you enter in the textarea is rendered once the change is committed to the data on blur. You're not going to benefit significantly from Ractive over a more relatively simple template to HTML string renderer like mustache or handlebars if you manage actually changing the DOM yourself. If you're not attaching a Ractive instance to the DOM to let it render, you can just swap out its template and rerun toHTML. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the example code using partials (which incidentally doesn't seem to run properly in Chrome on Mac, but runs ok in Firefox and Safari e.g. even this simple example fails in Chrome). I enhanced your example and added a data attribute. Typing in var r = new Ractive({
target: '#target',
template: '#template',
observe: {
str(v) {
try {
this.set('partial', Ractive.parse(v))
} catch {}
}
},
data: { a: 1, b: 2} // <--- I added this
}) Got resetTemplate workingSetting partials aside for the moment, I managed to get the On subscribing to global dataSo now, if I have an instance of ractive for each HTML target, how do I implement a global data object? Having each ractive instance point its But how to go in the other direction, to broadcast changes from globalData? How does some arbitrary part of my application (e.g. in the model domain) set a value on globalData and have all the reactive instances respond and update their UI elements? I don't want to maintain a global list of ractive instances and call .set on each of them each time there is a change in the globalData. So I tried creating a special dedicated ractive instance <div id=target></div>
var globalData = {
msg: "hoopy",
count: 43
}
var ractiveGlobalData = new Ractive({
data: globalData
})
var r = new Ractive({
target: '#target',
template: 'count is {{count}}',
data: globalData
})
const onObserverChange = () => {
if (!r) return
console.log(globalData.count)
// How to force r to re-render?
}
ractiveGlobalData.observe('*', onObserverChange)
// model change, unrelated to UI, but expect all reactive objects with a target to re-render
ractiveGlobalData.set('count', 123) Of course there might be a totally different way of achieving this which I am missing. |
Beta Was this translation helpful? Give feedback.
-
Chrome is being a bit weird lately. I had to run the example a few times before it actually rendered anything in Chrome, and it's not clear why. No errors in the console, nothing to indicate a problem. Most of my applications set up a single root instance that manages all of the data and acts as a sync point with the server, if there is one, via a websocket. This instance does not have to have a template or be rendered. I then use <div id=target></div>
var store = new Ractive({
data: {
msg: "hoopy",
count: 43
}
})
var r = new Ractive({
target: '#target',
template: `count is {{store.count}} <button on-click="@.add('store.count')">++</button>`,
on: {
init() { this.link('', 'store', { instance: store }); }
}
});
store.observe('*', (v, _o, k) => {
console.log(`${k} is now ${v}`);
}, { init: false });
store.set('count', 123); |
Beta Was this translation helpful? Give feedback.
-
My use case is user editable templates, which I just can't seem to get working in ractive. A ractive object loves having data change, but not the template itself. You seem to have to recreate the reactive object from scratch each time a template changes?
Recreating the reactive object each time its template changes is
dataStore.observe('*', updateRenderedText)
- unobserving and re-observing is a challengeI did finally find a routine
resetTemplate
in the documentation which I tried to usebut get errors like
I don't have
target:
in the reactive object set to anything, preferring to use .toHTML() and then inserting that into my html myself.Surely it can't be that hard? Or am I simply using ractive wrong and shouldn't be considering changing templates dynamically.
Beta Was this translation helpful? Give feedback.
All reactions