How to attach an object's function to a Dio.js event?
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
add a comment |
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
1
I am not certain of the context where you are working, but in the block you point to you are referencingthis
rather than_this
. This could be why you don't get to accessthis.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
add a comment |
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
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
javascript
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 referencingthis
rather than_this
. This could be why you don't get to accessthis.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
add a comment |
1
I am not certain of the context where you are working, but in the block you point to you are referencingthis
rather than_this
. This could be why you don't get to accessthis.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
add a comment |
1 Answer
1
active
oldest
votes
@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)
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
@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)
add a comment |
@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)
add a comment |
@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)
@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)
edited Nov 29 '18 at 22:46
answered Nov 29 '18 at 22:20
NatashaNatasha
674
674
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 accessthis.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