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

Define getters run twice. setVal is missing on first run. #2230

Closed
marshallswain opened this issue Feb 2, 2016 · 7 comments · Fixed by #2239
Closed

Define getters run twice. setVal is missing on first run. #2230

marshallswain opened this issue Feb 2, 2016 · 7 comments · Fixed by #2239
Assignees
Milestone

Comments

@marshallswain
Copy link
Member

With the latest version of Can, all of my define getters are getting run twice. On the first run, setVal is absent. On the second, it is present.

See this JSBin

can.Component.extend({
  tag: "home-page",
  template: can.view('home-template'),
  viewModel: {
    define: {
      names: {
        get(val, setVal){

          console.log('ARGUMENT COUNT:' + arguments.length);

          // This check is now necessary
          if (setVal) {
            console.log('There you are, setVal!');
            return val;
          }

          console.log('no setVal');
          return [];
        }
      }
    }
  }
});

@marshallswain marshallswain changed the title setVal is missing. Define getters run twice, setVal is missing on first run. Feb 2, 2016
@daffl
Copy link
Contributor

daffl commented Feb 2, 2016

Is that bound to a select field?

@marshallswain
Copy link
Member Author

Not in the JSBin: http://jsbin.com/ruvako/edit?html,js,console,output

It's simply

{{names}}

@marshallswain marshallswain changed the title Define getters run twice, setVal is missing on first run. Define getters run twice. setVal is missing on first run. Feb 2, 2016
@dylanrtt
Copy link
Contributor

dylanrtt commented Feb 2, 2016

Here is a simpler example:
http://jsbin.com/qayicihipa/edit?html,js,console,output

It doesn't seem to happen when you bind directly to the map before the stache binding.

The same behavior occurs if you try to read the property before binding it.

@justinbmeyer
Copy link
Contributor

This is due to the return readButDontObserveCompute(computedAttr.compute); added in #1915.

@justinbmeyer
Copy link
Contributor

Instead of the solution for #1915, we should probably observe on the compute and not on the property itself. Basically flip the behavior around. Perhaps __get should check if it's a computed property and not call can.__observe around here: https://github.com/canjs/canjs/blob/minor/map/map.js#L246

@justinbmeyer
Copy link
Contributor

A more simple test would probably have something like:

c = can.compute(function(){
  return m.attr("p");
})

Where p is a async defined property on map m.

@justinbmeyer justinbmeyer self-assigned this Feb 3, 2016
@daffl daffl added this to the 2.3.14 milestone Feb 5, 2016
@daffl
Copy link
Contributor

daffl commented Feb 5, 2016

Caused by #2224

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

Successfully merging a pull request may close this issue.

4 participants