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












2















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 '18 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 '18 at 20:49
















2















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 '18 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 '18 at 20:49














2












2








2








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 '18 at 20:50







HypeWolf

















asked Nov 22 '18 at 17:01









HypeWolfHypeWolf

12412




12412








  • 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 20:49












1 Answer
1






active

oldest

votes


















0





+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',
    autoActivateHeartbeat: false,
    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









    0





    +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






























      0





      +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




























        0





        +50







        0





        +50



        0




        +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 '18 at 22:46

























        answered Nov 29 '18 at 22:20









        NatashaNatasha

        674




        674






























            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.




            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

            Wiesbaden

            Marschland

            Dieringhausen