Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$getElement returns wrong results when there is no $refresh method #163

Open
divdavem opened this issue May 3, 2014 · 1 comment
Open
Labels

Comments

@divdavem
Copy link
Member

divdavem commented May 3, 2014

Plunk

Here is the plunk to reproduce the issue:
http://embed.plnkr.co/arLGKhwphORMRl9gmh1O

Click on the button.

Expected behavior: an alert is displayed with the following content:
On click: this.$getElement(0) = BUTTON

Actual behavior: the content of the alert is wrong:
On click: this.$getElement(0) = H1

HSP file

var klass = require("hsp/klass");
var Ctrl = klass({
  // Uncomment the following line and the $getElement method will work fine:
  // $refresh: function () {},
  click: function () {
    var expectedButton = this.$getElement(0);
    alert("On click: this.$getElement(0) = " + expectedButton.tagName);
  }
});
# template myCpt using c:Ctrl
  <button onclick="{c.click()}">Click me</button>
# /template
# template main
  <h1>Title</h1>
  <#myCpt />
# /template
main().render("output"); 

Note

$getElement returns a correct value if there is a $refresh method in the controller.
This seems to be linked to the needCommentNodes variable in cptcomponent.js:

var needCommentNodes=(this.createPathObservers() || this.ctlConstuctor.$refresh);

It is confusing to have a method which can return wrong values. If we do not want to always include comment nodes, it would be better to remove the $getElement method and to pass elements as parameters to $refresh, as discussed here

Hashspace version

Tested with version 4491708.

@divdavem divdavem added the bug label May 3, 2014
@benouat
Copy link
Member

benouat commented May 5, 2014

I already discussed that with @b-laporte , and apparently his initial plan was to only authorized the usage of $getElement() when you have a $refresh() implementation.
I was also thinking about dynamically adding that $getElement()method only when you define a $refresh() one.

For the solution regarding arguments, it can simply not be done like that. 😞 Just think about customisation, if you override a template, but keep the same controller, then your arguments might be different according to the changed template, and then you root element is different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants