RiotTS icon indicating copy to clipboard operation
RiotTS copied to clipboard

Extending a Riot.Element with templates

Open andrewvarga opened this issue 8 years ago • 3 comments

Hi Nino,

this is not a bug, more like a question and it's probably related to riot more than riotTS.

I'm wondering if it's possible that if I have a class which has a template to extend that class but be able to use the parent's template too. The closest I could get to this was to use composition instead of inheritance and <yield/>.

So something like this:

@template(`
   <mycomp>
      <p>mycomp content</p> 
      <yield/>
   </mycomp>
)
class MyComponent extends Riot.Element
{
    // ....
}

// This kinda does what I need (except that MyChild doesn't inherit from MyComp):
`@template(`
   <mychild>
      <mycomp>test</mycomp>
   </mychild>
)
class MyChild extends Riot.Element
{
    // ...
}

// This doesn't work, the template override MyComponent's template and I couldn't find a way to be able to use the parent's template
`@template(`
   <mychild>
      <div>test</div>
   </mychild>
)
class MyChild extends MyComponent
{
    // ...
 }

Or maybe I'm using a wrong approach and I shouldn't even try to do this?

andrewvarga avatar Jan 10 '17 10:01 andrewvarga

Yes @template is overridden because this is how classes are supposed to work. But perhaps in your case you just need to have a 3rd element which is a common ancestor for MyComp and MyChild, e.g.

class MyCommonBase extends Riot.Element { /*...*/ }
class MyComponent extends MyCommonBase { /*...*/ } 
class MyChild extends MyCommonBase { /*...*/ } 

(not sure what you want to achieve though)

nippur72 avatar Jan 10 '17 13:01 nippur72

Thank you that sounds reasonable. I just wanted to reuse the template of my parent class without having to rewrite it, but I will try to explain my use case better.

andrewvarga avatar Jan 10 '17 22:01 andrewvarga

I'll be interested to hear back from @andrewvarga to see if this is working as intended. This relates to issue 40

sidouglas avatar Jan 12 '17 00:01 sidouglas