How to attach an object's function to a Dio.js event?











up vote
2
down vote

favorite












The Dio.js GitHub page



My Dio.js issue on github with owner's suggestion that didn't work



The goal is to render dom elements and attach an event that is in the same object as the render function



When the webpage loads, I call an init function that render some divs and attach a click event to a function in the same object.



var thecheckbox = (function() {
function x() {}

x.prototype.isActivated = false // True when the user click the checkbox
x.prototype.init = function() {
let bodydiv = document.createElement('div')
document.body.appendChild(bodydiv)


d.render(this.render, bodydiv)
}
x.prototype.init = function() {
let mybody = document.createElement('div')
document.body.appendChild(mybody)


d.render(this.render, protekBody)
}
x.prototype.activate = function() {
console.log('Activate')
var _this = this
if (!_this.isActivated) {
_this.isActivated = true
var checkbox = document.getElementsByClassName('checkbox')[0]
checkbox.getElementsByClassName(
'checkbox2'
)[0].style.display = 'block'

}
}
x.prototype.render = function() {
var _this = this
return d.h(
'div',
{ class: 'normal light' },
d.h(
'div',
{
class: 'content'
},
d.h(
'div',
{
class: 'inline-block',
onClick: this.activate // <<<---------
},

),
d.h(
'div',
{ class: 'inline-block' },
d.h(
'div',
{ class: 'center' },
d.h('div', { class: 'label center' }, strings.checkme)
)
)
)
)
}

return new x()
})()

// Initialize
_addEvent(document, 'ready', function() {
thecheckbox.init()
})


I talked to the owner of the repo here (https://github.com/thysultan/dio.js/issues/85) and it still doesn't work as he explained.



I need to be able to access the object's properties outside the scope of Dio but attach an event to a dio dom element.



I tried converting the object to a regular object (not instantiated) and call d.render(d.h(thecheckbox), mydiv) but it doesn't works.










share|improve this question




















  • 1




    I am not certain of the context where you are working, but in the block you point to you are referencing this rather than _this. This could be why you don't get to access this.activate
    – Moustachiste
    Nov 29 at 19:58












  • @Moustachiste Thanks for your comment. Sorry for the confusion, it's because I've tried both. TypeError: object is undefined; can't access its "then" property thrown by dio.js function: function thenable(object) { return ( typeof object.then === 'function' && typeof object.catch === 'function' ) }
    – HypeWolf
    Nov 29 at 20:49















up vote
2
down vote

favorite












The Dio.js GitHub page



My Dio.js issue on github with owner's suggestion that didn't work



The goal is to render dom elements and attach an event that is in the same object as the render function



When the webpage loads, I call an init function that render some divs and attach a click event to a function in the same object.



var thecheckbox = (function() {
function x() {}

x.prototype.isActivated = false // True when the user click the checkbox
x.prototype.init = function() {
let bodydiv = document.createElement('div')
document.body.appendChild(bodydiv)


d.render(this.render, bodydiv)
}
x.prototype.init = function() {
let mybody = document.createElement('div')
document.body.appendChild(mybody)


d.render(this.render, protekBody)
}
x.prototype.activate = function() {
console.log('Activate')
var _this = this
if (!_this.isActivated) {
_this.isActivated = true
var checkbox = document.getElementsByClassName('checkbox')[0]
checkbox.getElementsByClassName(
'checkbox2'
)[0].style.display = 'block'

}
}
x.prototype.render = function() {
var _this = this
return d.h(
'div',
{ class: 'normal light' },
d.h(
'div',
{
class: 'content'
},
d.h(
'div',
{
class: 'inline-block',
onClick: this.activate // <<<---------
},

),
d.h(
'div',
{ class: 'inline-block' },
d.h(
'div',
{ class: 'center' },
d.h('div', { class: 'label center' }, strings.checkme)
)
)
)
)
}

return new x()
})()

// Initialize
_addEvent(document, 'ready', function() {
thecheckbox.init()
})


I talked to the owner of the repo here (https://github.com/thysultan/dio.js/issues/85) and it still doesn't work as he explained.



I need to be able to access the object's properties outside the scope of Dio but attach an event to a dio dom element.



I tried converting the object to a regular object (not instantiated) and call d.render(d.h(thecheckbox), mydiv) but it doesn't works.










share|improve this question




















  • 1




    I am not certain of the context where you are working, but in the block you point to you are referencing this rather than _this. This could be why you don't get to access this.activate
    – Moustachiste
    Nov 29 at 19:58












  • @Moustachiste Thanks for your comment. Sorry for the confusion, it's because I've tried both. TypeError: object is undefined; can't access its "then" property thrown by dio.js function: function thenable(object) { return ( typeof object.then === 'function' && typeof object.catch === 'function' ) }
    – HypeWolf
    Nov 29 at 20:49













up vote
2
down vote

favorite









up vote
2
down vote

favorite











The Dio.js GitHub page



My Dio.js issue on github with owner's suggestion that didn't work



The goal is to render dom elements and attach an event that is in the same object as the render function



When the webpage loads, I call an init function that render some divs and attach a click event to a function in the same object.



var thecheckbox = (function() {
function x() {}

x.prototype.isActivated = false // True when the user click the checkbox
x.prototype.init = function() {
let bodydiv = document.createElement('div')
document.body.appendChild(bodydiv)


d.render(this.render, bodydiv)
}
x.prototype.init = function() {
let mybody = document.createElement('div')
document.body.appendChild(mybody)


d.render(this.render, protekBody)
}
x.prototype.activate = function() {
console.log('Activate')
var _this = this
if (!_this.isActivated) {
_this.isActivated = true
var checkbox = document.getElementsByClassName('checkbox')[0]
checkbox.getElementsByClassName(
'checkbox2'
)[0].style.display = 'block'

}
}
x.prototype.render = function() {
var _this = this
return d.h(
'div',
{ class: 'normal light' },
d.h(
'div',
{
class: 'content'
},
d.h(
'div',
{
class: 'inline-block',
onClick: this.activate // <<<---------
},

),
d.h(
'div',
{ class: 'inline-block' },
d.h(
'div',
{ class: 'center' },
d.h('div', { class: 'label center' }, strings.checkme)
)
)
)
)
}

return new x()
})()

// Initialize
_addEvent(document, 'ready', function() {
thecheckbox.init()
})


I talked to the owner of the repo here (https://github.com/thysultan/dio.js/issues/85) and it still doesn't work as he explained.



I need to be able to access the object's properties outside the scope of Dio but attach an event to a dio dom element.



I tried converting the object to a regular object (not instantiated) and call d.render(d.h(thecheckbox), mydiv) but it doesn't works.










share|improve this question















The Dio.js GitHub page



My Dio.js issue on github with owner's suggestion that didn't work



The goal is to render dom elements and attach an event that is in the same object as the render function



When the webpage loads, I call an init function that render some divs and attach a click event to a function in the same object.



var thecheckbox = (function() {
function x() {}

x.prototype.isActivated = false // True when the user click the checkbox
x.prototype.init = function() {
let bodydiv = document.createElement('div')
document.body.appendChild(bodydiv)


d.render(this.render, bodydiv)
}
x.prototype.init = function() {
let mybody = document.createElement('div')
document.body.appendChild(mybody)


d.render(this.render, protekBody)
}
x.prototype.activate = function() {
console.log('Activate')
var _this = this
if (!_this.isActivated) {
_this.isActivated = true
var checkbox = document.getElementsByClassName('checkbox')[0]
checkbox.getElementsByClassName(
'checkbox2'
)[0].style.display = 'block'

}
}
x.prototype.render = function() {
var _this = this
return d.h(
'div',
{ class: 'normal light' },
d.h(
'div',
{
class: 'content'
},
d.h(
'div',
{
class: 'inline-block',
onClick: this.activate // <<<---------
},

),
d.h(
'div',
{ class: 'inline-block' },
d.h(
'div',
{ class: 'center' },
d.h('div', { class: 'label center' }, strings.checkme)
)
)
)
)
}

return new x()
})()

// Initialize
_addEvent(document, 'ready', function() {
thecheckbox.init()
})


I talked to the owner of the repo here (https://github.com/thysultan/dio.js/issues/85) and it still doesn't work as he explained.



I need to be able to access the object's properties outside the scope of Dio but attach an event to a dio dom element.



I tried converting the object to a regular object (not instantiated) and call d.render(d.h(thecheckbox), mydiv) but it doesn't works.







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 at 20:50

























asked Nov 22 at 17:01









HypeWolf

10710




10710








  • 1




    I am not certain of the context where you are working, but in the block you point to you are referencing this rather than _this. This could be why you don't get to access this.activate
    – Moustachiste
    Nov 29 at 19:58












  • @Moustachiste Thanks for your comment. Sorry for the confusion, it's because I've tried both. TypeError: object is undefined; can't access its "then" property thrown by dio.js function: function thenable(object) { return ( typeof object.then === 'function' && typeof object.catch === 'function' ) }
    – HypeWolf
    Nov 29 at 20:49














  • 1




    I am not certain of the context where you are working, but in the block you point to you are referencing this rather than _this. This could be why you don't get to access this.activate
    – Moustachiste
    Nov 29 at 19:58












  • @Moustachiste Thanks for your comment. Sorry for the confusion, it's because I've tried both. TypeError: object is undefined; can't access its "then" property thrown by dio.js function: function thenable(object) { return ( typeof object.then === 'function' && typeof object.catch === 'function' ) }
    – HypeWolf
    Nov 29 at 20:49








1




1




I am not certain of the context where you are working, but in the block you point to you are referencing this rather than _this. This could be why you don't get to access this.activate
– Moustachiste
Nov 29 at 19:58






I am not certain of the context where you are working, but in the block you point to you are referencing this rather than _this. This could be why you don't get to access this.activate
– Moustachiste
Nov 29 at 19:58














@Moustachiste Thanks for your comment. Sorry for the confusion, it's because I've tried both. TypeError: object is undefined; can't access its "then" property thrown by dio.js function: function thenable(object) { return ( typeof object.then === 'function' && typeof object.catch === 'function' ) }
– HypeWolf
Nov 29 at 20:49




@Moustachiste Thanks for your comment. Sorry for the confusion, it's because I've tried both. TypeError: object is undefined; can't access its "then" property thrown by dio.js function: function thenable(object) { return ( typeof object.then === 'function' && typeof object.catch === 'function' ) }
– HypeWolf
Nov 29 at 20:49












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted
+50










@HypeWolf



Maybe you could try the following:



function x() {
this.activate = this.activate.bind(this)
}


or as alternative:



onClick: () => this.activate()


And I think you should also do something like:



d.render(() => this.render(), protekBody)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435515%2fhow-to-attach-an-objects-function-to-a-dio-js-event%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted
    +50










    @HypeWolf



    Maybe you could try the following:



    function x() {
    this.activate = this.activate.bind(this)
    }


    or as alternative:



    onClick: () => this.activate()


    And I think you should also do something like:



    d.render(() => this.render(), protekBody)





    share|improve this answer



























      up vote
      0
      down vote



      accepted
      +50










      @HypeWolf



      Maybe you could try the following:



      function x() {
      this.activate = this.activate.bind(this)
      }


      or as alternative:



      onClick: () => this.activate()


      And I think you should also do something like:



      d.render(() => this.render(), protekBody)





      share|improve this answer

























        up vote
        0
        down vote



        accepted
        +50







        up vote
        0
        down vote



        accepted
        +50




        +50




        @HypeWolf



        Maybe you could try the following:



        function x() {
        this.activate = this.activate.bind(this)
        }


        or as alternative:



        onClick: () => this.activate()


        And I think you should also do something like:



        d.render(() => this.render(), protekBody)





        share|improve this answer














        @HypeWolf



        Maybe you could try the following:



        function x() {
        this.activate = this.activate.bind(this)
        }


        or as alternative:



        onClick: () => this.activate()


        And I think you should also do something like:



        d.render(() => this.render(), protekBody)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 29 at 22:46

























        answered Nov 29 at 22:20









        Natasha

        661




        661






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435515%2fhow-to-attach-an-objects-function-to-a-dio-js-event%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo