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

Support Classes #125

Closed
nijikokun opened this issue Aug 12, 2013 · 9 comments
Closed

Support Classes #125

nijikokun opened this issue Aug 12, 2013 · 9 comments

Comments

@nijikokun
Copy link

Any intent to support classes, or a more in-depth selection capability for elements and templates?

@Rich-Harris
Copy link
Member

When initialising, you can use any selector supported by document.querySelector(). (If you pass in a string it will first try document.getElementById( options.el ) then fall back to document.querySelector( options.el ).)

You can also do ractive.find( selector ) and ractive.findAll( selector ) which are equivalent to ractive.el.querySelector( selector ) and ractive.el.querySelectorAll( selector ) respectively:

ractive = new Ractive({
  el: 'aside .inner', // or whatever
  template: '#tpl'
});

// later...
width = ractive.find( '.yardstick' ).clientWidth;

Does this answer the question? Let me know if I can close this issue. Thanks

@nijikokun
Copy link
Author

However is that only for the element, and not the template? If it works for both then I see this as able to be closed, but from my attempts I haven't been able to use classes with templates.

@Rich-Harris
Copy link
Member

Yes - it's very hard to tell if a string is a selector (for an element containing a template) or the template itself, so only strings that begin with a # character are treated as selectors. But you can always do template: document.querySelector( selector ).innerHTML if needs be

@nijikokun
Copy link
Author

Wouldn't it be easier to split the two rather than attempting to support both in one tag (easier, but presents more problems than pros), by having template and source / html / raw you could get around having to wonder whether one is a element or source code (html, mustache, etc).

Just a thought, another thought:

You could check to see whether the string has html elements or mustache tags, if it does not the it must be a selector.

@Rich-Harris
Copy link
Member

I see where you're coming from. I'm always hesitant about adding new options and methods though unless they're absolutely necessary, and personally I haven't come across a situation where I had any need for grabbing templates from elements without IDs.

(This is probably because in the course of developing Ractive I've discovered what happens if you let a browser parse HTML before JavaScript gets its hands on it with .innerHTML - the horrors I've seen! - and so I only ever put templates in <script type='text/ractive'> tags if I'm not loading them with AMD or AJAX, rather than in the 'real' DOM.)

Future versions of Ractive will have support for nested components (it's in there now, in an experimental form) which should make things easier - in the meantime, could you elaborate on your use case to help me understand whether this is something that needs to be supported? Thanks

@weepy
Copy link

weepy commented Aug 13, 2013

wouldn't a template always start with a '<' ?

On Mon, Aug 12, 2013 at 11:25 PM, Rich-Harris [email protected]:

I see where you're coming from. I'm always hesitant about adding new
options and methods though unless they're absolutely necessary, and
personally I haven't come across a situation where I had any need for
grabbing templates from elements without IDs.

(This is probably because in the course of developing Ractive I've
discovered what happens if you let a browser parse HTML before JavaScript
gets its hands on it with .innerHTML - the horrors I've seen!https://github.com/Rich-Harris/Ractive/issues/117\- and so I only ever put templates in <script type='text/ractive'> tags if I'm not loading them with AMD or AJAX,
rather than in the 'real' DOM.)

Future versions of Ractive will have support for nested components (it's
in there now, in an experimental formhttps://github.com/Rich-Harris/Ractive/issues/74)
which should make things easier - in the meantime, could you elaborate on
your use case to help me understand whether this is something that needs to
be supported? Thanks


Reply to this email directly or view it on GitHubhttps://github.com/Rich-Harris/Ractive/issues/125#issuecomment-22530689
.

@Rich-Harris
Copy link
Member

Not necessarily! Most would but it's by no means guaranteed - I often have element-less templates when I'm testing, if a starting '<' was a requirement it would cause all manner of confusion

@nijikokun
Copy link
Author

I forgot to respond to this, most of it is conflicting names and I got around it by appending stuff and was just wondering if you ever thought to support it. It would also make using ractive a lot easier as el and template aren't really explained as to what they support being passed along.

@Rich-Harris
Copy link
Member

Ah ok. It's not on the roadmap right now as I don't see a widespread need for this, and as you say it can easily be worked around. Take your point about the documentation - it was slightly out of date, I've just updated the wiki.

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

No branches or pull requests

3 participants