computed properties with parameters

52 views
Skip to first unread message

Chris Conroy

unread,
Feb 22, 2017, 1:35:34 PM2/22/17
to racti...@googlegroups.com
The documentation for computed properties is not clear on support for parameters: http://docs.ractivejs.org/0.8/computed-properties

In the example given, the first option under "Data context for computed properties" is to use a function in data that takes a parameter rather than a computed property. However, this then loses the dependency tracking of a computed property. When I try to add a parameter to a computed property, I get function not found errors.

I can easily reference the relevant properties from my template as parameters to a normal function to work around this, but this can be more verbose than necessary.

Is there any way to pass parameters from the template into a computed property?

Chris Reeves

unread,
Feb 22, 2017, 2:00:14 PM2/22/17
to Ractive.js
Actually, dependency tracking still happens with expressions that call data functions. If the deps are not passed as args to the function, you just have to make sure that you pull values using ractive.get inside the function body. When said keypaths are updated, any expression calling a data function referencing them will recompute.

There's no way to pass parameters to a computation, because then it wouldn't have a single value.


Thanks, Chris

--
You received this message because you are subscribed to the Google Groups "Ractive.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ractive-js+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Conroy

unread,
Mar 14, 2017, 1:08:40 PM3/14/17
to racti...@googlegroups.com
Ping: any thoughts here?

Donny Velazquez

unread,
Mar 15, 2017, 8:47:06 AM3/15/17
to Ractive.js
I have used this hack before to use arguments with computed properties.

computed: {
           
IsActiveList: function () {
               
return (function (viewType) {
                   
return viewType == this.get("active_list") ? "active" : "";
               
}).bind(this);
           
}
       
}


I call it like this in the view.

{{IsActiveList(0)}}


Reply all
Reply to author
Forward
0 new messages