Search code examples
javascriptjqueryractivejsgridstack

How do I add gridstack.js to Ractive.js?


I am creating example application with Ractive.js and gridstack.js, but can't figure out how to add gridstack as a decorator. I think this is the proper way to add jQuery elements to ractive.js, please advise if it's not.

After I created a decorator and assigned it to the Dashboard component it actually doesn't work and events from gridstackDecorator aren't cached in the Dashboard component.

I created this Fiddle with the code that doesn't work and the source is below:

Ractive.js component tree will look like:

- App
|--- Dashboard    <-- GridstackDecorator
    |--- Widget
        |--- Widget container components
    |--- ...
    |--- Widget
|--- Other components

HTML template looks like:

<div id="app"></div>

<script>
window.__APP_INITIAL_STATE__ = {
    widgets: [
        {id: 1, name: "First widget", x:0, y:0, width:2, height:2},
        {id: 2, name: "Second widget", x:5, y:0, width:2, height:2},
        {id: 3, name: "Third widget", x:10, y:0, width:2, height:2},
     ],
};
</script>

The gridstack decorator which I try to assign to Dashboard component looks like this:

The update and teardown methods are never called, why is that?

var gridstackDecorator = function gridstackDecorator( node, content ) {
    console.log('new decorator', node, content);

    var $gridstack;
    var $gridstackEl;

    var options = {
        cellHeight: 80,
        verticalMargin: 10,
        height:20,
    };

    $gridstackEl = $(node);
    $gridstackEl.gridstack(options);
    $gridstack = $gridstackEl.data('gridstack');

    $gridstackEl.on('change', function () {
        console.log("change");
        serialize();
    });


    function serialize() {
        var result = _.map($('.grid-stack .grid-stack-item:visible'), function (el) {
            el = $(el);
            var node = el.data('_gridstack_node');
            return {
                id: el.attr('data-custom-id'),
                x: node.x,
                y: node.y,
                width: node.width,
                height: node.height
            };
        });

        return result;
    }


    return {
        update(x,y,z) {
            // NEVER EXECUTES
            console.log("update",x,y,z);
        },
        teardown(x,y,z) {
                // NEVER EXECUTES
            console.log("teardown",x,y,z);
            $gridstack.destroy();
        }
    };
};

Widget component, which will render each gridstack container elements is:

var Widget = Ractive.extend({
    isolated: true,
    template: `<div class="grid-stack-item" data-gs-x="{{x}}" data-gs-y="{{y}}" data-gs-width="{{width}}" data-gs-height="{{height}}">
    <div class="grid-stack-item-content">
        {{id}}-{{name}} (x: {{x}}, y: {{y}})<a href="#" on-click="@this.fire('deleteWidget', event, id)">Xxx</a>
    </div>
</div>`,
    oninit() {
    },
    onrender() {
        console.log("render");
    },
    data: {
        x:10,
        y:10,
        width:100,
        height:100,
    }
})

The Dashboard component, where gridstack decorator is assigned to:

var Dashboard = Ractive.extend({
    isolated: true,
    components: {
        Widget
    },
    decorators: {
        gridstack: gridstackDecorator
    },

    oninit() {
    },

    deleteWidget(id) {
            console.log("deleteWidget", id);
    },

    addWidget(name) {
      var widgets = this.get("widgets");
      var id = widgets.length + 1;

      widgets.push({
        id: id,
        name: name,
        x:1,
        y:1,
        width:2,
        htight:2
      });
      this.set("widgets", widgets);
    },

    updateWidgets(data) {
        console.log("update widgets");
    },

    template: `
<div class="grid-stack" as-gridstack>
    <a on-click="addWidget('New widget')" href="#">Add New</a>
        {{#each widgets}}
        <Widget
                id="{{this.id}}"
                name="{{this.name}}"
                x="{{this.x}}"
                y="{{this.y}}"
                width="{{width}}"
                height="{{height}}"

                on-deleteWidget="@this.deleteWidget(id)"
        >
        </Widget>
        {{/each}}
</div>`
});

And the root component which will render the overall application with the Dashboard component, looks like this:

var App = new Ractive({
    el: '#app',
    template: `<Dashboard widgets={{widgets}}></Dashboard>`,
    components: {
        Dashboard
    },
    data: window.__APP_INITIAL_STATE__
});

Solution

  • You have to instruct Ractive about on which element(s) you want your decorator to be called on by adding "as-${decoratorName}" to the element, like, in your case

    <div as-gridstack [other attributes...]> ... </div>
    

    You can also pass arguments to the update function of a decorator using a command-separated list like like

    <div as-gridstack="arg1, arg2, ..., argN" [other attributes...]> ... </div>
    

    You can find more information at https://ractive.js.org/v0.x/0.8/decorators and https://ractive.js.org/v0.x/0.8/writing-decorator-plugins