Comparing version 3.0.0-pre.13 to 3.0.0-pre.14
@@ -313,1 +313,28 @@ /* jshint asi:true */ | ||
}); | ||
test("Basic Map.prototype.compute", function () { | ||
var state = new Map({ | ||
category: 5, | ||
productType: 4 | ||
}); | ||
var catCompute = state.compute('category'); | ||
var prodCompute = state.compute('productType'); | ||
catCompute.bind("change", function (ev, val, old) { | ||
equal(val, 6, "correct"); | ||
equal(old, 5, "correct"); | ||
}); | ||
state.bind('productType', function(ev, val, old) { | ||
equal(val, 5, "correct"); | ||
equal(old, 4, "correct"); | ||
}); | ||
state.attr("category", 6); | ||
prodCompute(5); | ||
catCompute.unbind("change"); | ||
state.unbind("productType"); | ||
}); |
@@ -24,2 +24,4 @@ /* jshint -W079 */ | ||
var Observation = require('can-observation'); | ||
var ObserveReader = require('can-observation/reader/reader'); | ||
var canCompute = require('can-compute'); | ||
@@ -30,2 +32,3 @@ var namespace = require("can-util/namespace"); | ||
var deepAssign = require("can-util/js/deep-assign/deep-assign"); | ||
var isFunction = require("can-util/js/is-function/is-function"); | ||
var assign = require("can-util/js/assign/assign"); | ||
@@ -606,2 +609,23 @@ var types = require("can-util/js/types/types"); | ||
// ### compute | ||
// Creates a compute that represents a value on this map. If the property is a function | ||
// on the prototype, a "function" compute wil be created. | ||
// Otherwise, a compute will be created that reads the observable attributes | ||
compute: function (prop) { | ||
if (isFunction(this.constructor.prototype[prop])) { | ||
return canCompute(this[prop], this); | ||
} else { | ||
var reads = ObserveReader.reads(prop); | ||
var last = reads.length - 1; | ||
return canCompute(function (newVal) { | ||
if (arguments.length) { | ||
ObserveReader.write(this, reads[last].key, newVal); | ||
} else { | ||
return ObserveReader.get(this, reads[last].key); | ||
} | ||
}, this); | ||
} | ||
}, | ||
// ### each | ||
@@ -608,0 +632,0 @@ // loops through all the key-value pairs on this map. |
{ | ||
"name": "can-map", | ||
"version": "3.0.0-pre.13", | ||
"version": "3.0.0-pre.14", | ||
"description": "Observable Objects", | ||
@@ -49,2 +49,3 @@ "homepage": "http://canjs.com", | ||
"dependencies": { | ||
"can-compute": "^3.0.0-pre.15", | ||
"can-construct": "^3.0.0-pre.8", | ||
@@ -51,0 +52,0 @@ "can-event": "^3.0.0-pre.9", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71786
1328
5
+ Addedcan-compute@^3.0.0-pre.15
+ Addedcan-compute@3.3.10(transitive)
+ Addedcan-reflect-promise@1.1.5(transitive)
+ Addedcan-stache-key@0.1.4(transitive)