nippur72 / RiotTS

RiotJS for TypeScript
MIT License
101 stars 8 forks source link

Process child custom tag #5

Closed Kiho closed 9 years ago

Kiho commented 9 years ago

Can you explain how I can convert the code below to typescript?

    riot.tag('raw', '<span></span>', function(opts) {
        this.root.innerHTML = opts.r;
    });
nippur72 commented 9 years ago

it should be something like this:

@template("<raw><span></span></raw>")
class RawElement extends Riot.Element {
   constructor(opts) {
      super();
      this.root.innerHTML = opts.r;
   }
}
RawElement.register();
Kiho commented 9 years ago

I think it's correct if it is stand alone. But I like to convert code in this page to typescript - https://github.com/vinnyroundfoot/Riot-Table

@template("elements/rtable.html") class Rtable extends Riot.Element { // ............ constructor(opts) { super(); this.on("mount", () => { this.init(); }); }

init = function () {
    // ............
    riot.tag('raw', '<span></span>', function (opts) {
        this.root.innerHTML = opts.r;
    });
    // .............
    return this;
};

// .............

};

Rtable.register();

nippur72 commented 9 years ago

in other words Rtable defines another element called "raw". Just define/register it before Rtable, e.g.:

class RawElement ....
RawElement.register(); //

class RTable ...
RTable.register();

and you can drop it from the init function.

Kiho commented 9 years ago

That's working for me, actual problem was caused by Visual Studio IDE. When I pasting HTML template, some variables were lowercased. VS does not like RiotJS's loop syntax.

Thanks.

Kiho commented 9 years ago

I am done converting here - https://github.com/Kiho/Riot-Table# Thanks again.

nippur72 commented 9 years ago

Regarding the VS IDE, it's a shame, the TypeScript editor setting has a lot of bugs, I used to open issues on the typescript repo, but they fixed only one.

Regarding Riot-Table, very nice work. BTW, RiotTS has helper functions for lifecycle methods, so if you want you can write it this way:

/* instead of
constructor(opts) {
        super();
        this.on("mount", () => {
            this.init();
        });
    }
*/

// just this
mounted() {
   this.init();
}