DEV Community

Cover image for How to identify your view instance by its packagename
CraftkitJS
CraftkitJS

Posted on

How to identify your view instance by its packagename

Instance of Craft.UI.View is placed on the DOM-tree with unique ID,
and it is registered in the window object. The ID is used for instance method call.

Instance ID is always identical. But to be able to identify your instance by its ID expression, you can use packagename class variable.

without packagename

class Example extends Craft.UI.View {
    template(componentId){
        return `
            <div id="root" class="root">
                Hello World!
            </div>
        `;
    }
}

var view = new Example();
view.loadView();
Craft.Core.Context.getRootViewController().appendSubView(view);
Enter fullscreen mode Exit fullscreen mode

This class is placed as:

Alt Text

The instance of Example class is named as Example_2, class name with trailing serial number. This is unique across your application, even if you add more instances of Example class, or even if you add Example class included in another package.

with packagename

To identify class of your instance by its ID, you can use packagename class variable.

class Example extends Craft.UI.View {
    constructor(options){
        super(options);
        this.packagename = 'craftkit.devtoblog.Example';
    }
    template(componentId){
        return `
            <div id="root" class="root">
                Hello World!
            </div>
        `;
    }
}

var view = new Example();
view.loadView();
Craft.Core.Context.getRootViewController().appendSubView(view);
Enter fullscreen mode Exit fullscreen mode

This is placed as:

Alt Text

In this case, instance of Example class is placed as craftkit_devtoblog_Example_4.

You can use this to survey instance pool.

NOTE

The packagename is not restricted by module name itself.

Top comments (0)