Search code examples
ractivejs

Informing RactiveJS about new object properties in magic mode


I'm new to Ractive.js and using "magic mode" so that updates to my JS objects automatically trigger updates to my UI. Everything is working great except that I have some properties which are added to my bound objects after my Ractive instances are rendered. This means that Ractive doesn't see changes to those properties, so the "magic" doesn't work for them.

As a workaround, I've found that I can just initialize those properties with 'empty' values before passing them to Ractive, but it feels a little unnatural.

Is there a preferred way to inform Ractive that I've added a new property to an object that it should start tracking?


Solution

  • My experince is that you can du that in 2 ways.

    The first is the one you describe in your post. The second is to "add" the property through the template that you use to render the object.

    What ractive actually do when you set magic mode to true is "wrapping/replacing" your field with a property wich monitores the changes to the field.

    I have had som issues using magicmode when i use Object.DefineProperty on my data objcect. (stuff get called twice)

    I would go for soulution 1, and define all fields ahead of time. Also be aware of setting your field(wich is actually now a property replaced by ractive) to something that is not a valuetype as that will again overwrite the property and mess up magicmode.