Try our conversational search powered by Generative AI!

Dojo widget "PostCreate" is called twice

Vote:
 

I am making a very simple dojo widget that looks like this.

define([
    // Dojo
        "dojo/_base/declare",
        'dojo/dom',
        'dojo/dom-construct',
        "dojo/text!./Templates/htmlpage.html",
    // Dijit
        "dijit/_TemplatedMixin",
        "dijit/_WidgetBase",
    //CMS
        "epi-cms/_ContentContextMixin"],
    function (
    // Dojo
        declare,
        dom,
        domConstruct,
        template,
    // Dijit
        _TemplatedMixin,
        _WidgetBase,
    //CMS
    _ContentContextMixin) {
        return declare('namespace',
        [_WidgetBase, _TemplatedMixin, _ContentContextMixin], {
            templateString: template,
            postCreate: function () {
                this.inherited(arguments);
                var id = this._currentContext.id;
                id = parseInt(id.substring(0, id.indexOf('_')));

                var self = this;
                jQuery.ajax({
                    url: '/api/InfoScreenApi/GetParentSlides?id=' + id,
                    dataType: 'json',
                    success: function (data) {
                        var ulList = dom.byId('slideNamesList');
                        for (var i = 0; i < data.length; i++) {
                            domConstruct.place('
  • ' + data[i] + '
  • ', ulList); } } }); }, isValid: function () { return true; }, }) });

    The problem I'm having is that this module seems to be called twice, making the list I am populating end up being twice the size with doublets.

    The question is, is this normal behavior for the dojo widget or am I doing something wrong?  Thanks.

    #143045
    Jan 11, 2016 12:30
    Vote:
     

    Hi,

    I that the event should be executed once per widget instance.

    If you used this widget twice as your property model editor then it will be executed twice.

    You could try to set breakpoint in postCreate method and see what is the property name:

    this.metadata.name 

    If they are different, that it just simply means that those are different instances.

    #143046
    Jan 11, 2016 13:49
    Vote:
     

    Hi and thanks for the answer.

    I have the instance once on this page. It should then be called once as you write. The weird thing is that the view, in this case a simple ul-list, is static, meaning that it is populated twice for the same

    property. See this image of the ul-list in the episerver form-editor. Each line is a li-node. It should only be two li-nodes.

    Even more strange (I think), the id of the property is "namespace_0" and "namespace_1". (couldn't find the metadata prop, used this.id instead). This should mean it is different instances, but I can't see that there are two instances... I only call the page once.

    To make it work I can change the for-loop code to this. This way the element is populated only once.

    for (var i = 0; i < data.length; i++) {
                                self.slideNamesList.innerHTML += '<li>' + data[i] + '</li>'
                                //domConstruct.place('<li>' + data[i] + '</li>', ulList);
                            }

    The commented is the old code. I don't understand anything :P

    #143048
    Jan 11, 2016 14:10
    * You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.