Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement request: putting expressions/functions outside data #1996

Closed
dagnelies opened this issue Jun 15, 2015 · 9 comments
Closed

enhancement request: putting expressions/functions outside data #1996

dagnelies opened this issue Jun 15, 2015 · 9 comments

Comments

@dagnelies
Copy link
Contributor

Hi,

This is a humble request / vote to put expression functions outside data in the long term.

There are several reasons for this:

  • functions are not data, they are functions, why are they here in the first place?
  • I think it is a fairly common scenario that people use REST APIs to get/set the whole data at once. However, currently, this would blow up the helper functions. Sending the whole wouldn't work either, because the helper functions are mixed with the data.
  • Combine this with potential adaptators which load/save your data automatically and the functions would be in the way too.

...this is of course not dramatic, simply putting the data in a specific place does the trick, but i still think keeping only data in data would be nicer.

So where would I put the functions? Ideally, I'd put them anywhere in the root, or in a predefined place like functions or expr, whatever.

@fskreuz
Copy link
Contributor

fskreuz commented Jun 16, 2015

I remember this one before, and I resorted to using computed props. There was also an example of helper functions in Ractive.defaults to patch in libs into components. Might help you with the globals issue too.

Here's the related issue: #956 (more than a thousand issues ago) :trollface:

@Rich-Harris
Copy link
Member

Yeah, it has come up once or twice (gawd, more than a thousand issues? Get ready to party like it's #1999) - I can understand where you're coming from @dagnelies. It is something that you can do by adding helper functions to the prototype, but it could be more intuitive. I don't think it'll make it into 0.8 (want to limit the scope of that release), but consider it on the radar.

@evs-chris
Copy link
Contributor

I just wanted to toss in my two cents that I'd like to keep the ability to have functions in the data intact because it can be pretty handy to swap out functionality (sorting, side-effecting - for shame, I know -, etc) from the viewmodel. I have been planning to experiment with something more akin to filters from other template systems, where the filter fn that lives outside the data is called with the Ractive instance as the context and the data as the first param, to see how that plays out.

@Rich-Harris Rich-Harris added this to the future milestone Dec 7, 2015
@evs-chris
Copy link
Contributor

I think with access to @this, @this.root, and @shared, there are a few reasonable places to put functions, so I'm going to close this.

@dagnelies
Copy link
Contributor Author

out of curiosity, what's @shared?

@evs-chris
Copy link
Contributor

It's like @global but only for ractive instances, so it's not subject to outside interference.

@dagnelies
Copy link
Contributor Author

thanks for the info. Btw, good work.

@kouts
Copy link

kouts commented Nov 14, 2016

So how to use this? something like Ractive.shared.myfunc = function(){ ... ?

@evs-chris
Copy link
Contributor

evs-chris commented Nov 14, 2016

@kouts, no it's only reachable in a ractive-observable way: ractive.set('@shared.myfunc', function() {...}). All ractive instances can read and write to the same @shared keypaths, and all ractive instances will receive updates as @shared keypaths that they reference change.

UPDATE: not sure how, but I typoed set as get

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants