-
Notifications
You must be signed in to change notification settings - Fork 396
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
Functions outside data #956
Comments
+1 ... although in this scenario I have been known to use a computed property, when appropriate..
|
Oh yeah, I forgot about computed. Hmm... Will look into that. |
The tutorial was written before computed properties were supported - I'd say they're probably a better solution in this case. Another way to go would be to take advantage of the fact that data is 'inherited' - each instance's
var helpers = Ractive.defaults.data;
helpers.sort = function ( array ) {...};
helpers._ = _; // use underscore/lodash in templates |
@Rich-Harris I don't why, but it never occurred to me that you could set an object in data then call its methods in expressions. With those array sugar libraries you get in-template one-liner expressions. Very powerful. |
@Rich-Harris I did notice that prototype action with subclassing. Reset a child, the parent values shine through. Interesting. |
@fskreuz Yes, the data is "re-configured" on reset. |
This is a very cool <h2>Sorted numbers reversed</h2>
<p>{{ _(sort(numbers)).reverse() }}</p> do this: <h2>Sorted numbers reversed</h2>
<p>{{ sort(numbers) | _(data).reverse() }}</p> |
The chained notation will do fine. Besides, they're standard JS syntax. The pipe looks Django-ish and foreign to JS. |
@martypdx @kurdin Yeah, it's handy! We should probably make mention of it in the docs/tutorials, as it's not obvious that you can do that. I'm with @fskreuz on the syntax - if we introduce non-JS syntax it introduces a load of complexity, both for the library and for people using it. Are we okay to close this issue? |
Closed :D |
In this example, the function that alters the array is inside
data
.However, if I did something that would alter
sort
, it will run oversort
in the data and would break the template. See this demo. Same happens when using reset, which I often use to clear off stuff, especially when reusing modals (clear, dump data, show).Would it be better if these "helper" functions were separate from the data? Like a
helpers
per instance, andhelpers
in global, similar to partials.The text was updated successfully, but these errors were encountered: