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

Adds can.Compute, and "compute" type and setter / getter paring for define pugin #1486

Merged
merged 19 commits into from Mar 3, 2015

Conversation

justinbmeyer
Copy link
Contributor

You can:

c = new can.Compute(5);
c.get()
c.set(6)
c.bind("change", callback)
MyMap = can.Map.extend({
  define: {value: {type: "compute"} }
})
var myMap = new MyMap();
var c = can.compute(5);

myMap.attr("value",c);
myMapp.attr("value") //-> 5

c(6)
myMapp.attr("value") //-> 6

var c2 = can.compute("a")
myMap.attr("value",c2);
myMapp.attr("value") //-> "a"
MyMap = can.Map.extend({
  define: {
    num: {
      set: function(newValue){
        return +newValue;
      },
      get: function(lastSetValue){
        return Math.pow( lastSetValue, this.attr("pow") )
      }
    }
  }
})

var myMap = new MyMap({pow: 2});
myMap.attr("num","4");
myMap.attr("num") //-> 16

@justinbmeyer
Copy link
Contributor Author

@daffl I'm assigning this to you as this might break a few of the compute.async tests. You can probably comment them out in the legacy tests b/c they aren't a documented API.

@justinbmeyer justinbmeyer added this to the 2.2.0 milestone Mar 3, 2015
@akagomez
Copy link
Contributor

akagomez commented Mar 3, 2015

From the looks of it, this is now possible too:

can.define.powerOfTwo = {
  set: function(newValue){
    return +newValue;
  },
  get: function(lastSetValue){
    return Math.pow( lastSetValue, 2)
  }
}

MyMap = can.Map.extend({
  define: {
    num: {
      type: 'powerOfTwo'
    }
  }
})

var myMap = new MyMap();
myMap.attr("num","4");
myMap.attr("num") //-> 16

};
compute.bind = can.proxy(internalCompute.bind, internalCompute);
compute.unbind = can.proxy(internalCompute.unbind, internalCompute);
compute.isComputed = can.proxy(internalCompute.isComputed, internalCompute);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this supposed to work? For some reason it is only breaking for Mootools but can.proxy(true, internalCompute) doesn't seem right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isComputed is a boolean. So I think this should be compute.isComputed = true

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my playing with this branch I noticed that isComputed was no longer a boolean and was uber confused. Glad that's cleared up.

daffl added a commit that referenced this pull request Mar 3, 2015
Adds can.Compute, and "compute" type and setter / getter paring for define pugin
@daffl daffl merged commit 32f1f66 into master Mar 3, 2015
@daffl daffl deleted the tests-for-compute-type-1409 branch March 3, 2015 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants