can-define
Advanced tools
Comparing version 0.1.0 to 0.3.0
"format cjs"; | ||
var can = require("can/util/"); | ||
var event = require("can/event/"); | ||
require("can/map/map_helpers"); | ||
require("can/compute/"); | ||
var event = require("can-event"); | ||
var eventLifecycle = require("can-event/lifecycle/lifecycle"); | ||
var canBatch = require("can-event/batch/batch"); | ||
var compute = require("can-compute"); | ||
var ObserveInfo = require("can-observe-info"); | ||
var canEach = require("can-util/js/each/each"); | ||
var isEmptyObject = require("can-util/js/is-empty-object/is-empty-object"); | ||
var assign = require("can-util/js/assign/assign"); | ||
var dev = require("can-util/js/dev/dev"); | ||
var behaviors, eventsProto, getPropDefineBehavior, define, | ||
@@ -21,3 +29,3 @@ make, makeDefinition, replaceWith; | ||
// a `getter` and `setter` function for `Object.defineProperty`. | ||
can.each(defines, function(d, prop){ | ||
canEach(defines, function(d, prop){ | ||
@@ -31,3 +39,3 @@ // Figure out the `definition` object. | ||
} | ||
if(can.isEmptyObject(definition)) { | ||
if(isEmptyObject(definition)) { | ||
definition = {type: "*"}; | ||
@@ -40,3 +48,3 @@ } | ||
// Special case definitions that have only `type: "*"`. | ||
if(type && can.isEmptyObject(definition) && type === "*") { | ||
if(type && isEmptyObject(definition) && type === "*") { | ||
Object.defineProperty(objPrototype, prop, { | ||
@@ -167,3 +175,3 @@ get: make.get.data(prop), | ||
var constructor = function(props){ | ||
can.simpleExtend(this, props); | ||
assign(this, props); | ||
}; | ||
@@ -181,6 +189,6 @@ define(constructor.prototype, defines); | ||
return { | ||
compute: can.compute.async(defaultValue && defaultValue(), get, map), | ||
compute: compute.async(defaultValue && defaultValue(), get, map), | ||
count: 0, | ||
handler: function (ev, newVal, oldVal) { | ||
can.batch.trigger(map, { | ||
canBatch.trigger.call(map, { | ||
type: prop, | ||
@@ -211,3 +219,3 @@ target: map | ||
can.batch.trigger(this, { | ||
canBatch.trigger.call(this, { | ||
type: prop, | ||
@@ -231,3 +239,3 @@ target: this | ||
can.batch.start(); | ||
canBatch.start(); | ||
var setterCalled = false, | ||
@@ -245,3 +253,3 @@ current = getCurrent.call(this), | ||
if(setterCalled) { | ||
can.batch.stop(); | ||
canBatch.stop(); | ||
} else { | ||
@@ -256,3 +264,3 @@ if(hasGetter) { | ||
} | ||
can.batch.stop(); | ||
canBatch.stop(); | ||
} | ||
@@ -263,3 +271,3 @@ // this is a side effect, it didn't take a value | ||
setEvents.call(this, value); | ||
can.batch.stop(); | ||
canBatch.stop(); | ||
return; | ||
@@ -272,3 +280,3 @@ } | ||
// and we shouldn't do anything. | ||
can.batch.stop(); | ||
canBatch.stop(); | ||
} | ||
@@ -279,6 +287,6 @@ // we are expecting something | ||
asyncTimer = setTimeout(function () { | ||
can.dev.warn('can/map/setter.js: Setter "' + prop + '" did not return a value or call the setter callback.'); | ||
}, can.dev.warnTimeout); | ||
dev.warn('can/map/setter.js: Setter "' + prop + '" did not return a value or call the setter callback.'); | ||
}, dev.warnTimeout); | ||
//!steal-remove-end | ||
can.batch.stop(); | ||
canBatch.stop(); | ||
return; | ||
@@ -293,3 +301,3 @@ } | ||
setEvents.call(this, setValue); | ||
can.batch.stop(); | ||
canBatch.stop(); | ||
} | ||
@@ -300,3 +308,3 @@ // this is a side effect, it didn't take a value | ||
setEvents.call(this, value); | ||
can.batch.stop(); | ||
canBatch.stop(); | ||
return; | ||
@@ -309,3 +317,3 @@ } | ||
setEvents.call(this, undefined); | ||
can.batch.stop(); | ||
canBatch.stop(); | ||
} | ||
@@ -316,6 +324,6 @@ // we are expecting something | ||
asyncTimer = setTimeout(function () { | ||
can.dev.warn('can/map/setter.js: Setter "' + prop + '" did not return a value or call the setter callback.'); | ||
}, can.dev.warnTimeout); | ||
dev.warn('can/map/setter.js: Setter "' + prop + '" did not return a value or call the setter callback.'); | ||
}, dev.warnTimeout); | ||
//!steal-remove-end | ||
can.batch.stop(); | ||
canBatch.stop(); | ||
return; | ||
@@ -418,3 +426,3 @@ } | ||
return function(){ | ||
can.__observe(this, prop); | ||
ObserveInfo.observe(this, prop); | ||
return this._data[prop]; | ||
@@ -473,5 +481,5 @@ }; | ||
eventsProto = can.simpleExtend({}, event); | ||
can.simpleExtend(eventsProto, { | ||
bind: function (eventName, handler) { | ||
eventsProto = assign({}, event); | ||
assign(eventsProto, { | ||
addEventListener: function (eventName, handler) { | ||
@@ -482,3 +490,3 @@ var computedBinding = this._computed && this._computed[eventName]; | ||
computedBinding.count = 1; | ||
computedBinding.compute.bind("change", computedBinding.handler); | ||
computedBinding.compute.addEventListener("change", computedBinding.handler); | ||
} else { | ||
@@ -490,3 +498,3 @@ computedBinding.count++; | ||
return can.bindAndSetup.apply(this, arguments); | ||
return eventLifecycle.addAndSetup.apply(this, arguments); | ||
}, | ||
@@ -498,3 +506,3 @@ | ||
// stop forwarding events of the computed property to this map. | ||
unbind: function (eventName, handler) { | ||
removeEventListener: function (eventName, handler) { | ||
var computedBinding = this._computed && this._computed[eventName]; | ||
@@ -504,3 +512,3 @@ if (computedBinding) { | ||
computedBinding.count = 0; | ||
computedBinding.compute.unbind("change", computedBinding.handler); | ||
computedBinding.compute.removeEventListener("change", computedBinding.handler); | ||
} else { | ||
@@ -512,3 +520,3 @@ computedBinding.count--; | ||
return can.unbindAndTeardown.apply(this, arguments); | ||
return eventLifecycle.removeAndTeardown.apply(this, arguments); | ||
@@ -524,2 +532,5 @@ }, | ||
}); | ||
eventsProto.on = eventsProto.bind = eventsProto.addEventListener; | ||
eventsProto.off = eventsProto.unbind = eventsProto.removeEventListener; | ||
delete eventsProto.one; | ||
@@ -585,2 +596,2 @@ | ||
} | ||
}; | ||
}; |
{ | ||
"name": "can-define", | ||
"version": "0.1.0", | ||
"version": "0.3.0", | ||
"description": "Like can.Map, but without the .attr method.", | ||
@@ -33,10 +33,15 @@ "main": "can-define.js", | ||
"dependencies": { | ||
"can": "^2.3.15" | ||
"can-compute": "^3.0.0-pre.2", | ||
"can-event": "^3.0.0-pre.2", | ||
"can-observe-info": "^3.0.0-pre.4", | ||
"can-util": "^3.0.0-pre.10" | ||
}, | ||
"devDependencies": { | ||
"can-list": "^3.0.0-pre.1", | ||
"can-stache": "canjs/can-stache#master", | ||
"jshint": "^2.9.1", | ||
"serve": "^1.4.0", | ||
"steal": "^0.12.0", | ||
"steal": "^0.16.0", | ||
"steal-qunit": "^0.1.1", | ||
"steal-tools": "^0.15.5", | ||
"steal-tools": "^0.16.0", | ||
"testee": "^0.2.5" | ||
@@ -43,0 +48,0 @@ }, |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.org/canjs/can-define.svg?branch=master)](https://travis-ci.org/canjs/can-define) | ||
# can-define | ||
@@ -2,0 +4,0 @@ |
967
test.js
var QUnit = require("steal-qunit"); | ||
var compute = require("can/compute/"); | ||
var compute = require("can-compute"); | ||
var define = require("can-define"); | ||
var stache = require("can/view/stache/"); | ||
var stache = require("can-stache"); | ||
var CanList = require("can-list"); | ||
var canBatch = require("can-event/batch/batch"); | ||
var isArray = require("can-util/js/is-array/is-array"); | ||
QUnit.test("basics on a prototype", 5, function() { | ||
var Person = function(first, last) { | ||
this.first = first; | ||
this.last = last; | ||
}; | ||
define(Person.prototype, { | ||
first: "*", | ||
last: "*", | ||
fullName: { | ||
get: function() { | ||
return this.first + " " + this.last; | ||
} | ||
} | ||
}); | ||
var Person = function(first, last) { | ||
this.first = first; | ||
this.last = last; | ||
}; | ||
define(Person.prototype, { | ||
first: "*", | ||
last: "*", | ||
fullName: { | ||
get: function() { | ||
return this.first + " " + this.last; | ||
} | ||
} | ||
}); | ||
var p = new Person("Mohamed", "Cherif"); | ||
var p = new Person("Mohamed", "Cherif"); | ||
p.bind("fullName", function(ev, newVal, oldVal) { | ||
QUnit.equal(oldVal, "Mohamed Cherif"); | ||
QUnit.equal(newVal, "Justin Meyer"); | ||
}); | ||
equal(p.fullName, "Mohamed Cherif", "fullName initialized right"); | ||
p.bind("fullName", function(ev, newVal, oldVal) { | ||
QUnit.equal(oldVal, "Mohamed Cherif"); | ||
QUnit.equal(newVal, "Justin Meyer"); | ||
}); | ||
p.bind("first", function(el, newVal, oldVal) { | ||
QUnit.equal(newVal, "Justin", "first new value"); | ||
QUnit.equal(oldVal, "Mohamed", "first old value"); | ||
}); | ||
equal(p.fullName, "Mohamed Cherif", "fullName initialized right"); | ||
can.batch.start(); | ||
p.first = "Justin"; | ||
p.last = "Meyer"; | ||
can.batch.stop(); | ||
p.bind("first", function(el, newVal, oldVal) { | ||
QUnit.equal(newVal, "Justin", "first new value"); | ||
QUnit.equal(oldVal, "Mohamed", "first old value"); | ||
}); | ||
canBatch.start(); | ||
p.first = "Justin"; | ||
p.last = "Meyer"; | ||
canBatch.stop(); | ||
}); | ||
QUnit.test('basics set', 2,function() { | ||
var Defined = function(prop) { | ||
this.prop = prop; | ||
}; | ||
QUnit.test('basics set', 2, function() { | ||
var Defined = function(prop) { | ||
this.prop = prop; | ||
}; | ||
define(Defined.prototype, { | ||
prop: { | ||
set: function(newVal) { | ||
return "foo" + newVal; | ||
} | ||
} | ||
}); | ||
define(Defined.prototype, { | ||
prop: { | ||
set: function(newVal) { | ||
return "foo" + newVal; | ||
} | ||
} | ||
}); | ||
var def = new Defined(); | ||
def.prop = "bar"; | ||
var def = new Defined(); | ||
def.prop = "bar"; | ||
QUnit.equal(def.prop, "foobar", "setter works"); | ||
QUnit.equal(def.prop, "foobar", "setter works"); | ||
var DefinedCB = function(prop) { | ||
this.prop = prop; | ||
}; | ||
var DefinedCB = function(prop) { | ||
this.prop = prop; | ||
}; | ||
define(DefinedCB.prototype, { | ||
prop: { | ||
set: function(newVal, setter) { | ||
setter("foo" + newVal); | ||
} | ||
} | ||
}); | ||
define(DefinedCB.prototype, { | ||
prop: { | ||
set: function(newVal, setter) { | ||
setter("foo" + newVal); | ||
} | ||
} | ||
}); | ||
var defCallback = new DefinedCB(); | ||
defCallback.prop = "bar"; | ||
QUnit.equal(defCallback.prop, "foobar", "setter callback works"); | ||
var defCallback = new DefinedCB(); | ||
defCallback.prop = "bar"; | ||
QUnit.equal(defCallback.prop, "foobar", "setter callback works"); | ||
@@ -83,28 +87,28 @@ }); | ||
QUnit.expect(6); | ||
QUnit.expect(6); | ||
var Typer = function(arrayWithAddedItem, listWithAddedItem) { | ||
this.arrayWithAddedItem = arrayWithAddedItem; | ||
this.listWithAddedItem = listWithAddedItem; | ||
}; | ||
var Typer = function(arrayWithAddedItem, listWithAddedItem) { | ||
this.arrayWithAddedItem = arrayWithAddedItem; | ||
this.listWithAddedItem = listWithAddedItem; | ||
}; | ||
define(Typer.prototype, { | ||
arrayWithAddedItem: { | ||
type: function(value) { | ||
if (value && value.push) { | ||
value.push("item"); | ||
} | ||
return value; | ||
} | ||
}, | ||
listWithAddedItem: { | ||
type: function(value) { | ||
if (value && value.push) { | ||
value.push("item"); | ||
} | ||
return value; | ||
}, | ||
Type: can.List | ||
} | ||
}); | ||
define(Typer.prototype, { | ||
arrayWithAddedItem: { | ||
type: function(value) { | ||
if (value && value.push) { | ||
value.push("item"); | ||
} | ||
return value; | ||
} | ||
}, | ||
listWithAddedItem: { | ||
type: function(value) { | ||
if (value && value.push) { | ||
value.push("item"); | ||
} | ||
return value; | ||
}, | ||
Type: CanList | ||
} | ||
}); | ||
@@ -114,23 +118,23 @@ | ||
var t = new Typer(); | ||
deepEqual(Object.keys(t), [], "no keys"); | ||
var t = new Typer(); | ||
deepEqual(Object.keys(t), [], "no keys"); | ||
var array = []; | ||
t.arrayWithAddedItem = array; | ||
var array = []; | ||
t.arrayWithAddedItem = array; | ||
deepEqual(array, ["item"], "updated array"); | ||
QUnit.equal(t.arrayWithAddedItem, array, "leave value as array"); | ||
deepEqual(array, ["item"], "updated array"); | ||
QUnit.equal(t.arrayWithAddedItem, array, "leave value as array"); | ||
t.listWithAddedItem = []; | ||
t.listWithAddedItem = []; | ||
QUnit.ok(t.listWithAddedItem instanceof can.List, "convert to can.List"); | ||
QUnit.equal(t.listWithAddedItem[0], "item", "has item in it"); | ||
QUnit.ok(t.listWithAddedItem instanceof CanList, "convert to CanList"); | ||
QUnit.equal(t.listWithAddedItem[0], "item", "has item in it"); | ||
can.compute(function(){ | ||
compute(function() { | ||
return t.listWithAddedItem.attr("length"); | ||
}).bind("change", function(ev, newVal) { | ||
QUnit.equal(newVal, 2, "got a length change"); | ||
}); | ||
}).addEventListener("change", function(ev, newVal) { | ||
QUnit.equal(newVal, 2, "got a length change"); | ||
}); | ||
t.listWithAddedItem.push("another item"); | ||
t.listWithAddedItem.push("another item"); | ||
@@ -140,27 +144,27 @@ }); | ||
QUnit.test("basic Type", function() { | ||
var Foo = function(name) { | ||
this.name = name; | ||
}; | ||
Foo.prototype.getName = function() { | ||
return this.name; | ||
}; | ||
var Foo = function(name) { | ||
this.name = name; | ||
}; | ||
Foo.prototype.getName = function() { | ||
return this.name; | ||
}; | ||
var Typer = function(foo) { | ||
this.foo = foo; | ||
}; | ||
var Typer = function(foo) { | ||
this.foo = foo; | ||
}; | ||
define(Typer.prototype, { | ||
foo: { | ||
Type: Foo | ||
} | ||
}); | ||
define(Typer.prototype, { | ||
foo: { | ||
Type: Foo | ||
} | ||
}); | ||
var t = new Typer("Justin"); | ||
QUnit.equal(t.foo.getName(), "Justin", "correctly created an instance"); | ||
var t = new Typer("Justin"); | ||
QUnit.equal(t.foo.getName(), "Justin", "correctly created an instance"); | ||
var brian = new Foo("brian"); | ||
var brian = new Foo("brian"); | ||
t.foo = brian; | ||
t.foo = brian; | ||
QUnit.equal(t.foo, brian, "same instances"); | ||
QUnit.equal(t.foo, brian, "same instances"); | ||
@@ -171,58 +175,58 @@ }); | ||
var Typer = function(date, string, number, bool, htmlbool, leaveAlone) { | ||
this.date = date, | ||
this.string = string, | ||
this.number = number, | ||
this.bool = bool, | ||
this.htmlbool = htmlbool, | ||
this.leaveAlone = leaveAlone | ||
}; | ||
var Typer = function(date, string, number, bool, htmlbool, leaveAlone) { | ||
this.date = date, | ||
this.string = string, | ||
this.number = number, | ||
this.bool = bool, | ||
this.htmlbool = htmlbool, | ||
this.leaveAlone = leaveAlone | ||
}; | ||
define(Typer.prototype, { | ||
date: { | ||
type: 'date' | ||
}, | ||
string: { | ||
type: 'string' | ||
}, | ||
number: { | ||
type: 'number' | ||
}, | ||
bool: { | ||
type: 'boolean' | ||
}, | ||
htmlbool: { | ||
type: 'htmlbool' | ||
}, | ||
leaveAlone: { | ||
type: '*' | ||
}, | ||
}); | ||
define(Typer.prototype, { | ||
date: { | ||
type: 'date' | ||
}, | ||
string: { | ||
type: 'string' | ||
}, | ||
number: { | ||
type: 'number' | ||
}, | ||
bool: { | ||
type: 'boolean' | ||
}, | ||
htmlbool: { | ||
type: 'htmlbool' | ||
}, | ||
leaveAlone: { | ||
type: '*' | ||
}, | ||
}); | ||
var obj = {}; | ||
var obj = {}; | ||
var t = new Typer( | ||
1395896701516, | ||
5, | ||
'5', | ||
'false', | ||
"", | ||
obj | ||
); | ||
var t = new Typer( | ||
1395896701516, | ||
5, | ||
'5', | ||
'false', | ||
"", | ||
obj | ||
); | ||
QUnit.ok(t.date instanceof Date, "converted to date"); | ||
QUnit.ok(t.date instanceof Date, "converted to date"); | ||
QUnit.equal(t.string, '5', "converted to string"); | ||
QUnit.equal(t.string, '5', "converted to string"); | ||
QUnit.equal(t.number, 5, "converted to number"); | ||
QUnit.equal(t.number, 5, "converted to number"); | ||
QUnit.equal(t.bool, false, "converted to boolean"); | ||
QUnit.equal(t.bool, false, "converted to boolean"); | ||
QUnit.equal(t.htmlbool, true, "converted to htmlbool"); | ||
QUnit.equal(t.htmlbool, true, "converted to htmlbool"); | ||
QUnit.equal(t.leaveAlone, obj, "left as object"); | ||
QUnit.equal(t.leaveAlone, obj, "left as object"); | ||
t.number = '15'; | ||
t.number = '15'; | ||
QUnit.ok(t.number === 15, "converted to number"); | ||
QUnit.ok(t.number === 15, "converted to number"); | ||
@@ -233,39 +237,39 @@ }); | ||
var Typer = function(prop) { | ||
if (prop !== undefined) { | ||
this.prop = prop; | ||
} | ||
}; | ||
var Typer = function(prop) { | ||
if (prop !== undefined) { | ||
this.prop = prop; | ||
} | ||
}; | ||
define(Typer.prototype, { | ||
prop: { | ||
value: 'foo' | ||
} | ||
}); | ||
var t = new Typer(); | ||
define(Typer.prototype, { | ||
prop: { | ||
value: 'foo' | ||
} | ||
}); | ||
var t = new Typer(); | ||
QUnit.equal(t.prop, "foo", "value is used as default value"); | ||
QUnit.equal(t.prop, "foo", "value is used as default value"); | ||
var Typer2 = function(prop) { | ||
if (prop !== undefined) { | ||
this.prop = prop; | ||
} | ||
}; | ||
var Typer2 = function(prop) { | ||
if (prop !== undefined) { | ||
this.prop = prop; | ||
} | ||
}; | ||
define(Typer2.prototype, { | ||
prop: { | ||
value: function() { | ||
return []; | ||
}, | ||
type: "*" | ||
} | ||
}); | ||
define(Typer2.prototype, { | ||
prop: { | ||
value: function() { | ||
return []; | ||
}, | ||
type: "*" | ||
} | ||
}); | ||
var t1 = new Typer2(), | ||
t2 = new Typer2(); | ||
QUnit.ok(t1.prop !== t2.prop, "different array instances"); | ||
QUnit.ok(can.isArray(t1.prop), "its an array"); | ||
var t1 = new Typer2(), | ||
t2 = new Typer2(); | ||
QUnit.ok(t1.prop !== t2.prop, "different array instances"); | ||
QUnit.ok(isArray(t1.prop), "its an array"); | ||
}); | ||
@@ -275,18 +279,18 @@ | ||
var Typer = function(prop) { | ||
//this.prop = prop; | ||
}; | ||
define(Typer.prototype, { | ||
var Typer = function(prop) { | ||
//this.prop = prop; | ||
}; | ||
define(Typer.prototype, { | ||
prop: { | ||
Value: Array, | ||
type: "*" | ||
} | ||
prop: { | ||
Value: Array, | ||
type: "*" | ||
} | ||
}); | ||
}); | ||
var t1 = new Typer(), | ||
t2 = new Typer(); | ||
QUnit.ok(t1.prop !== t2.prop, "different array instances"); | ||
QUnit.ok(can.isArray(t1.prop), "its an array"); | ||
var t1 = new Typer(), | ||
t2 = new Typer(); | ||
QUnit.ok(t1.prop !== t2.prop, "different array instances"); | ||
QUnit.ok(isArray(t1.prop), "its an array"); | ||
@@ -296,16 +300,16 @@ | ||
test("setter with no arguments and returns undefined does the default behavior, the setter is for side effects only", function () { | ||
test("setter with no arguments and returns undefined does the default behavior, the setter is for side effects only", function() { | ||
var Typer = function(prop) { | ||
//this.prop = prop; | ||
}; | ||
define(Typer.prototype, { | ||
//this.prop = prop; | ||
}; | ||
define(Typer.prototype, { | ||
prop: { | ||
set: function () { | ||
prop: { | ||
set: function() { | ||
this.foo = "bar"; | ||
} | ||
}, | ||
foo: "*" | ||
}, | ||
foo: "*" | ||
}); | ||
}); | ||
@@ -315,16 +319,18 @@ var t = new Typer(); | ||
t.prop = false; | ||
deepEqual(t.props(), { foo: "bar", prop: false }, "got the right props"); | ||
deepEqual(t.props(), { | ||
foo: "bar", | ||
prop: false | ||
}, "got the right props"); | ||
}); | ||
test("type happens before the set", 2,function () { | ||
var Typer = function() { | ||
}; | ||
define(Typer.prototype, { | ||
test("type happens before the set", 2, function() { | ||
prop: { | ||
var Typer = function() {}; | ||
define(Typer.prototype, { | ||
prop: { | ||
type: "number", | ||
set: function (newValue) { | ||
set: function(newValue) { | ||
equal(typeof newValue, "number", "got a number"); | ||
@@ -335,3 +341,3 @@ return newValue + 1; | ||
}); | ||
}); | ||
@@ -345,5 +351,5 @@ var map = new Typer(); | ||
test("getter and setter work", function () { | ||
test("getter and setter work", function() { | ||
expect(5); | ||
var Paginate = define.Constructor({ | ||
@@ -353,6 +359,6 @@ limit: "*", | ||
page: { | ||
set: function (newVal) { | ||
set: function(newVal) { | ||
this.offset = (parseInt(newVal) - 1) * this.limit; | ||
}, | ||
get: function () { | ||
get: function() { | ||
return Math.floor(this.offset / this.limit) + 1; | ||
@@ -363,7 +369,10 @@ } | ||
var p = new Paginate({limit: 10, offset: 20}); | ||
var p = new Paginate({ | ||
limit: 10, | ||
offset: 20 | ||
}); | ||
equal(p.page, 3, "page get right"); | ||
p.bind("page", function (ev, newValue, oldValue) { | ||
p.bind("page", function(ev, newValue, oldValue) { | ||
equal(newValue, 2, "got new value event"); | ||
@@ -382,5 +391,5 @@ equal(oldValue, 3, "got old value event"); | ||
test("getter with initial value", function(){ | ||
test("getter with initial value", function() { | ||
var compute = can.compute(1); | ||
var comp = compute(1); | ||
@@ -391,5 +400,5 @@ var Grabber = define.Constructor({ | ||
Value: Array, | ||
get: function(current, setVal){ | ||
if(setVal){ | ||
current.push( compute() ); | ||
get: function(current, setVal) { | ||
if (setVal) { | ||
current.push(comp()); | ||
} | ||
@@ -404,3 +413,3 @@ return current; | ||
// that there were no errors. | ||
equal(g.vals.length,0,"zero items in array" ); | ||
equal(g.vals.length, 0, "zero items in array"); | ||
@@ -424,3 +433,3 @@ }); | ||
test("Value generator can read other properties", function () { | ||
test("Value generator can read other properties", function() { | ||
var Map = define.Constructor({ | ||
@@ -431,3 +440,3 @@ letters: { | ||
numbers: { | ||
value: [1,2,3] | ||
value: [1, 2, 3] | ||
}, | ||
@@ -441,3 +450,3 @@ definedLetters: { | ||
generatedLetters: { | ||
value: function () { | ||
value: function() { | ||
return 'GHI'; | ||
@@ -447,4 +456,4 @@ } | ||
generatedNumbers: { | ||
value: function () { | ||
return new can.List([7, 8, 9]); | ||
value: function() { | ||
return new CanList([7, 8, 9]); | ||
} | ||
@@ -455,3 +464,3 @@ }, | ||
firstLetter: { | ||
value: function () { | ||
value: function() { | ||
return this.letters.substr(0, 1); | ||
@@ -461,3 +470,3 @@ } | ||
firstNumber: { | ||
value: function () { | ||
value: function() { | ||
return this.numbers[0]; | ||
@@ -469,3 +478,3 @@ } | ||
middleLetter: { | ||
value: function () { | ||
value: function() { | ||
return this.definedLetters.substr(1, 1); | ||
@@ -475,3 +484,3 @@ } | ||
middleNumber: { | ||
value: function () { | ||
value: function() { | ||
return this.definedNumbers[1]; | ||
@@ -483,3 +492,3 @@ } | ||
lastLetter: { | ||
value: function () { | ||
value: function() { | ||
return this.generatedLetters.substr(2, 1); | ||
@@ -489,3 +498,3 @@ } | ||
lastNumber: { | ||
value: function () { | ||
value: function() { | ||
return this.generatedNumbers[2]; | ||
@@ -537,3 +546,3 @@ } | ||
equal(map.someNumber, 5, 'on a set, they should be type converted'); | ||
map.number = '10'; // Custom set should be called | ||
@@ -547,6 +556,6 @@ equal(map.number, 10, 'value of number should be converted to a number'); | ||
var nailedIt = 'Nailed it'; | ||
var Example = define.Constructor({ | ||
name : { | ||
value : nailedIt | ||
name: { | ||
value: nailedIt | ||
} | ||
@@ -556,9 +565,9 @@ }); | ||
var NestedMap = define.Constructor({ | ||
isEnabled : { | ||
value : true | ||
isEnabled: { | ||
value: true | ||
}, | ||
test : { | ||
Value : Example | ||
test: { | ||
Value: Example | ||
}, | ||
examples : { | ||
examples: { | ||
type: { | ||
@@ -570,7 +579,7 @@ one: { | ||
type: { | ||
deep : { | ||
Value : Example | ||
deep: { | ||
Value: Example | ||
} | ||
}, | ||
Value : Object | ||
Value: Object | ||
} | ||
@@ -595,13 +604,13 @@ }, | ||
test('Can make an attr alias a compute (#1470)', 9, function(){ | ||
var computeValue = can.compute(1); | ||
test('Can make an attr alias a compute (#1470)', 9, function() { | ||
var computeValue = compute(1); | ||
var GetMap = define.Constructor({ | ||
value: { | ||
set: function(newValue, setVal, oldValue){ | ||
set: function(newValue, setVal, oldValue) { | ||
//debugger; | ||
if(newValue.isComputed) { | ||
if (newValue.isComputed) { | ||
return newValue; | ||
} | ||
if(oldValue && oldValue.isComputed) { | ||
if (oldValue && oldValue.isComputed) { | ||
oldValue(newValue); | ||
@@ -612,3 +621,3 @@ return oldValue; | ||
}, | ||
get: function(value){ | ||
get: function(value) { | ||
return value && value.isComputed ? value() : value; | ||
@@ -627,5 +636,5 @@ } | ||
getMap.bind("value", function(ev, newVal, oldVal){ | ||
getMap.bind("value", function(ev, newVal, oldVal) { | ||
switch(bindCallbacks) { | ||
switch (bindCallbacks) { | ||
case 0: | ||
@@ -659,3 +668,3 @@ equal(newVal, 2, "0 - bind called with new val"); | ||
// Try setting to a new comptue | ||
var newComputeValue = can.compute(4); | ||
var newComputeValue = compute(4); | ||
@@ -666,6 +675,6 @@ getMap.value = newComputeValue; | ||
test('value and get (#1521)', function () { | ||
// problem here is that previously, can.Map would set `size:1` on | ||
test('value and get (#1521)', function() { | ||
// problem here is that previously, can.Map would set `size:1` on | ||
// the map. This would effectively set the "lastSetValue". | ||
// in this new version, default values are not set. They | ||
@@ -675,7 +684,7 @@ // are only present. later. | ||
// been set, it should get it's value from any default value? | ||
var MyMap = define.Constructor({ | ||
data: { | ||
value: function () { | ||
return new can.List(['test']); | ||
value: function() { | ||
return new CanList(['test']); | ||
} | ||
@@ -685,3 +694,3 @@ }, | ||
value: 1, | ||
get: function (val) { | ||
get: function(val) { | ||
var list = this.data; | ||
@@ -699,3 +708,3 @@ var length = list.attr('length'); | ||
test("One event on getters (#1585)", function(){ | ||
test("One event on getters (#1585)", function() { | ||
var Person = define.Constructor({ | ||
@@ -712,3 +721,6 @@ name: "*", | ||
} else if (this.personId) { | ||
resolve( new Person({name: "Jose", id: 5}) ); | ||
resolve(new Person({ | ||
name: "Jose", | ||
id: 5 | ||
})); | ||
} else { | ||
@@ -728,3 +740,3 @@ return null; | ||
}); | ||
equal(appState.person, null, "no personId and no lastSetValue"); | ||
@@ -741,6 +753,6 @@ | ||
equal(personEvents,2); | ||
equal(personEvents, 2); | ||
}); | ||
test('Can read a defined property with a set/get method (#1648)', function () { | ||
test('Can read a defined property with a set/get method (#1648)', function() { | ||
// Problem: "get" is not passed the correct "lastSetVal" | ||
@@ -752,6 +764,6 @@ // Problem: Cannot read the value of "foo" | ||
value: '', | ||
set: function (setVal) { | ||
set: function(setVal) { | ||
return setVal; | ||
}, | ||
get: function (lastSetVal) { | ||
get: function(lastSetVal) { | ||
return lastSetVal; | ||
@@ -771,26 +783,377 @@ } | ||
QUnit.test('Default values cannot be set (#8)', function () { | ||
var Person = function(){}; | ||
define(Person.prototype, { | ||
first: { | ||
type: 'string', | ||
value: 'Chris' | ||
}, | ||
last: { | ||
type: 'string', | ||
value: 'Gomez' | ||
}, | ||
fullName: { | ||
get: function(){ | ||
return this.first + ' ' + this.last; | ||
} | ||
} | ||
}); | ||
test('Can bind to a defined property with a set/get method (#1648)', 3, function() { | ||
// Problem: "get" is not called before and after the "set" | ||
// Problem: Function bound to "foo" is not called | ||
// Problem: Cannot read the value of "foo" | ||
var p = new Person(); | ||
var Map = define.Constructor({ | ||
foo: { | ||
value: '', | ||
set: function(setVal) { | ||
return setVal; | ||
}, | ||
get: function(lastSetVal) { | ||
return lastSetVal; | ||
} | ||
} | ||
}); | ||
QUnit.equal(p.fullName, 'Chris Gomez', 'Fullname is correct'); | ||
p.first = 'Sara'; | ||
QUnit.equal(p.fullName, 'Sara Gomez', 'Fullname is correct after update'); | ||
}); | ||
var map = new Map(); | ||
map.bind('foo', function() { | ||
ok(true, 'Bound function is called'); | ||
}); | ||
equal(map.foo, '', 'Calling .attr(\'foo\') returned the correct value'); | ||
map.foo = 'baz'; | ||
equal(map.foo, 'baz', 'Calling .attr(\'foo\') returned the correct value'); | ||
}); | ||
test("type converters handle null and undefined in expected ways (1693)", function() { | ||
var Typer = define.Constructor({ | ||
date: { | ||
type: 'date' | ||
}, | ||
string: { | ||
type: 'string' | ||
}, | ||
number: { | ||
type: 'number' | ||
}, | ||
'boolean': { | ||
type: 'boolean' | ||
}, | ||
htmlbool: { | ||
type: 'htmlbool' | ||
}, | ||
leaveAlone: { | ||
type: '*' | ||
} | ||
}); | ||
var t = new Typer({ | ||
date: undefined, | ||
string: undefined, | ||
number: undefined, | ||
'boolean': undefined, | ||
htmlbool: undefined, | ||
leaveAlone: undefined | ||
}); | ||
equal(t.date, undefined, "converted to date"); | ||
equal(t.string, undefined, "converted to string"); | ||
equal(t.number, undefined, "converted to number"); | ||
equal(t.boolean, false, "converted to boolean"); | ||
equal(t.htmlbool, false, "converted to htmlbool"); | ||
equal(t.leaveAlone, undefined, "left as object"); | ||
t = new Typer({ | ||
date: null, | ||
string: null, | ||
number: null, | ||
'boolean': null, | ||
htmlbool: null, | ||
leaveAlone: null | ||
}); | ||
equal(t.date, null, "converted to date"); | ||
equal(t.string, null, "converted to string"); | ||
equal(t.number, null, "converted to number"); | ||
equal(t.boolean, false, "converted to boolean"); | ||
equal(t.htmlbool, false, "converted to htmlbool"); | ||
equal(t.leaveAlone, null, "left as object"); | ||
}); | ||
test('Initial value does not call getter', function() { | ||
expect(0); | ||
var Map = define.Constructor({ | ||
count: { | ||
get: function(lastVal) { | ||
ok(false, 'Should not be called'); | ||
return lastVal; | ||
} | ||
} | ||
}); | ||
new Map({ | ||
count: 100 | ||
}); | ||
}); | ||
test("getters produce change events", function() { | ||
var Map = define.Constructor({ | ||
count: { | ||
get: function(lastVal) { | ||
return lastVal; | ||
} | ||
} | ||
}); | ||
var map = new Map(); | ||
// map.bind("change", function(){ | ||
// ok(true, "change called"); | ||
// }); | ||
map.bind('count', function() { | ||
ok(true, "change called"); | ||
}); | ||
map.count = 22; | ||
}); | ||
test("Asynchronous virtual properties cause extra recomputes (#1915)", function() { | ||
stop(); | ||
var ran = false; | ||
var VM = define.Constructor({ | ||
foo: { | ||
get: function(lastVal, setVal) { | ||
setTimeout(function() { | ||
if (setVal) { | ||
setVal(5); | ||
} | ||
}, 10); | ||
} | ||
}, | ||
bar: { | ||
get: function() { | ||
var foo = this.foo; | ||
if (foo) { | ||
if (ran) { | ||
ok(false, 'Getter ran twice'); | ||
} | ||
ran = true; | ||
return foo * 2; | ||
} | ||
} | ||
} | ||
}); | ||
var vm = new VM(); | ||
vm.bind('bar', function() {}); | ||
setTimeout(function() { | ||
equal(vm.bar, 10); | ||
start(); | ||
}, 200); | ||
}); | ||
test("Stache with single property", function() { | ||
var Typer = define.Constructor({ | ||
foo: { | ||
type: 'string' | ||
} | ||
}); | ||
var template = stache('{{foo}}'); | ||
var t = new Typer({ | ||
foo: 'bar' | ||
}); | ||
var frag = template(t); | ||
equal(frag.firstChild.nodeValue, 'bar'); | ||
t.foo = "baz" | ||
equal(frag.firstChild.nodeValue, 'baz'); | ||
}); | ||
test("Stache with boolean property with {{#if}}", function() { | ||
var nailedIt = 'Nailed it'; | ||
var Example = define.Constructor({ | ||
name: { | ||
value: nailedIt | ||
} | ||
}); | ||
var NestedMap = define.Constructor({ | ||
isEnabled: { | ||
value: true | ||
}, | ||
test: { | ||
Value: Example | ||
}, | ||
examples: { | ||
type: { | ||
one: { | ||
Value: Example | ||
}, | ||
two: { | ||
type: { | ||
deep: { | ||
Value: Example | ||
} | ||
}, | ||
Value: Object | ||
} | ||
}, | ||
Value: Object | ||
} | ||
}); | ||
var nested = new NestedMap(); | ||
var template = stache('{{#if isEnabled}}Enabled{{/if}}'); | ||
var frag = template(nested); | ||
equal(frag.firstChild.nodeValue, 'Enabled'); | ||
}); | ||
test("stache with double property", function() { | ||
var nailedIt = 'Nailed it'; | ||
var Example = define.Constructor({ | ||
name: { | ||
value: nailedIt | ||
} | ||
}); | ||
var NestedMap = define.Constructor({ | ||
isEnabled: { | ||
value: true | ||
}, | ||
test: { | ||
Value: Example | ||
}, | ||
examples: { | ||
type: { | ||
one: { | ||
Value: Example | ||
}, | ||
two: { | ||
type: { | ||
deep: { | ||
Value: Example | ||
} | ||
}, | ||
Value: Object | ||
} | ||
}, | ||
Value: Object | ||
} | ||
}); | ||
var nested = new NestedMap(); | ||
template = stache('{{test.name}}') | ||
frag = template(nested); | ||
equal(frag.firstChild.nodeValue, nailedIt); | ||
}); | ||
test("Stache with one nested property", function() { | ||
var nailedIt = 'Nailed it'; | ||
var Example = define.Constructor({ | ||
name: { | ||
value: nailedIt | ||
} | ||
}); | ||
var NestedMap = define.Constructor({ | ||
isEnabled: { | ||
value: true | ||
}, | ||
test: { | ||
Value: Example | ||
}, | ||
examples: { | ||
type: { | ||
one: { | ||
Value: Example | ||
}, | ||
two: { | ||
type: { | ||
deep: { | ||
Value: Example | ||
} | ||
}, | ||
Value: Object | ||
} | ||
}, | ||
Value: Object | ||
} | ||
}); | ||
var nested = new NestedMap(); | ||
template = stache('{{examples.one.name}}'); | ||
frag = template(nested); | ||
equal(frag.firstChild.nodeValue, nailedIt); | ||
}); | ||
test("Stache with two nested property", function() { | ||
var nailedIt = 'Nailed it'; | ||
var Example = define.Constructor({ | ||
name: { | ||
value: nailedIt | ||
} | ||
}); | ||
var NestedMap = define.Constructor({ | ||
isEnabled: { | ||
value: true | ||
}, | ||
test: { | ||
Value: Example | ||
}, | ||
examples: { | ||
type: { | ||
one: { | ||
Value: Example | ||
}, | ||
two: { | ||
type: { | ||
deep: { | ||
Value: Example | ||
} | ||
}, | ||
Value: Object | ||
} | ||
}, | ||
Value: Object | ||
} | ||
}); | ||
var nested = new NestedMap(); | ||
template = stache('{{examples.two.deep.name}}'); | ||
frag = template(nested); | ||
equal(frag.firstChild.nodeValue, nailedIt); | ||
}); | ||
QUnit.test('Default values cannot be set (#8)', function() { | ||
var Person = function() {}; | ||
define(Person.prototype, { | ||
first: { | ||
type: 'string', | ||
value: 'Chris' | ||
}, | ||
last: { | ||
type: 'string', | ||
value: 'Gomez' | ||
}, | ||
fullName: { | ||
get: function() { | ||
return this.first + ' ' + this.last; | ||
} | ||
} | ||
}); | ||
var p = new Person(); | ||
QUnit.equal(p.fullName, 'Chris Gomez', 'Fullname is correct'); | ||
p.first = 'Sara'; | ||
QUnit.equal(p.fullName, 'Sara Gomez', 'Fullname is correct after update'); | ||
}); |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
41016
11
1424
26
4
8
1
+ Addedcan-compute@^3.0.0-pre.2
+ Addedcan-event@^3.0.0-pre.2
+ Addedcan-util@^3.0.0-pre.10
+ Addedcan-ajax@1.4.1(transitive)
+ Addedcan-assign@1.3.3(transitive)
+ Addedcan-cid@1.3.1(transitive)
+ Addedcan-compute@3.3.10(transitive)
+ Addedcan-deparam@1.2.3(transitive)
+ Addedcan-dom-data-state@0.2.0(transitive)
+ Addedcan-dom-events@1.3.13(transitive)
+ Addedcan-event@3.7.7(transitive)
+ Addedcan-event-dom-enter@1.0.4(transitive)
+ Addedcan-event-dom-radiochange@1.0.5(transitive)
+ Addedcan-globals@1.2.2(transitive)
+ Addedcan-key-tree@1.2.2(transitive)
+ Addedcan-log@1.0.2(transitive)
+ Addedcan-namespace@1.0.0(transitive)
+ Addedcan-observation@3.3.6(transitive)
+ Addedcan-observe-info@3.0.0(transitive)
+ Addedcan-param@1.2.0(transitive)
+ Addedcan-parse-uri@1.2.2(transitive)
+ Addedcan-reflect@1.19.2(transitive)
+ Addedcan-reflect-promise@1.1.5(transitive)
+ Addedcan-stache-key@0.1.4(transitive)
+ Addedcan-symbol@1.7.0(transitive)
+ Addedcan-types@1.4.0(transitive)
+ Addedcan-util@3.14.0(transitive)
- Removedcan@^2.3.15
- Removedcan@2.3.35(transitive)
- Removedcan-simple-dom@0.3.0(transitive)
- Removedjquery@2.2.4(transitive)
- Removedmicro-location@0.1.5(transitive)
- Removedsimple-html-tokenizer@0.2.6(transitive)