Search code examples
ractivejs

Ractive computed attributes returned in get()


ref this jsfiddle

html:

<main />
<div id='result' />

code:

window.ractive = new Ractive({
  el: 'main',
  template: '<p>a thing called {{thing}}</p>',
  computed: { thing : function(){return "kablooie"} }
});
$('#result').html(JSON.stringify(ractive.get()))

The ractive.get() here does return the value of the attribute "thing". Even though the docs say that computed attributes are not returned by get().

Is this intentional behaviour or a bug?


Solution

  • In edge Ractive (will be 0.8) which you are using, we added the computed and mapped properties to the root get via ractive.get() as a feature request.

    See this issue for current proposal to be able to get only the root data object via ractive.get('.'), which would mean:

    window.ractive = new Ractive({
      el: 'main',
      data: { foo: 'foo' },
      template: '<p>a thing called {{thing}}</p>',
      computed: { thing : function(){return "kablooie"} }
    });
    
    console.log( JSON.stringify( ractive.get() ) );
    // { foo: 'foo', thing: 'kablooie' }
    
    console.log( JSON.stringify( ractive.get('.') ) );
    // { foo: 'foo' }