Skip to content

Commit 9d0f619

Browse files
Improvements to Can-Define Documentation (#387)
* Greatly improved propDefinition examples. * incremental improvements to propDefinition.md * incremental updates, es6 and spacing syntax -- mostly. * Added more verbose examples to docs/define.md * Updeeated examples on default.md. * updated minor spacing and codepen examples. * mostly fixed value.md * fixed all codepen on valuee.md. * fixed spelling * improvements to serailize and identity codepens. * Greatly improved codepen examples for type.md. * removed working with section as it was legacy. This was based on a pseudo-vote from Kevin and Justin. * finished type.documentation * greatly improved type and TypeConstructor examples and documentation. * minor improvements. * fixed spacing in defaultConstructor.md * Improved an example on propDefinition.md * changed example 'hobbies' to more applicable affectations. * updated Getter example. * added comments in example. * greatly improved examples. * removed in favor of directly assigning. * updated example to show relation to original constructor. * fixed tabbing in serialize.md
1 parent 95f4739 commit 9d0f619

12 files changed

+804
-615
lines changed

Diff for: docs/TypeConstructor.md

+71-47
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,99 @@ value.
66

77
@signature `Type`
88

9-
A constructor function can be provided that is called to convert incoming values set on this property, like:
9+
A constructor function can be provided that is called to convert incoming values set on this property, like:
1010

11-
```js
12-
{
13-
prop: {
14-
Type: Person
15-
}
16-
}
17-
```
11+
```js
12+
import {DefineMap} from "can";
13+
import {Person} from "//unpkg.com/can-demo-models@5";
1814

19-
The `Type` constructor is called before the [can-define.types.type] property and before [can-define.types.set]. It checks if the incoming value
20-
is an [instanceof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof) `Type`. If it is, or if it is `null` or `undefined`, it passes the original value through. If not, it passes the original value to `new Type(originalValue)` and returns the
21-
new instance to be set.
15+
const Example = DefineMap.extend({
16+
prop: {
17+
Type: Person
18+
}
19+
});
20+
const ex = new Example();
21+
ex.prop = {first: "Justin", last: "Meyer"};
2222

23-
@signature `{propDefinition}`
23+
console.log( ex.prop instanceof Person ); //-> true
24+
console.log( ex.prop.fullName ); //-> "Justin Meyer"
25+
```
26+
@codepen
2427

25-
A [can-define.types.propDefinition] that defines an inline [can-define/map/map] type. For example:
28+
The `Type` constructor is called before the [can-define.types.type] property and before [can-define.types.set]. It checks if the incoming value
29+
is an [instanceof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof) `Type`. If it is, or if it is `null` or `undefined`, it passes the original value through. If not, it passes the original value to `new Type(originalValue)` and returns the
30+
new instance to be set.
2631

27-
```js
28-
{
29-
address: {
30-
Type: {
31-
street: "string",
32-
city: "string"
33-
}
34-
}
35-
}
36-
```
32+
@signature `{propDefinition}`
3733

38-
@signature `[Type|propDefinition]`
34+
A [can-define.types.propDefinition] that defines an inline [can-define/map/map] type. For example:
3935

40-
Defines an inline [can-define/list/list] type that's an array of `Type` or inline [can-define.types.propDefinition] [can-define/map/map]
41-
instances. For example:
36+
```js
37+
{
38+
address: {
39+
Type: {
40+
street: "string",
41+
city: "string"
42+
}
43+
}
44+
}
45+
```
4246

43-
```js
44-
{
45-
people: {
46-
Type: [ Person ]
47-
},
48-
addresses: {
49-
Type: [ {
50-
street: "string",
51-
city: "string"
52-
} ]
53-
}
54-
}
55-
```
47+
@signature `[Type|propDefinition]`
5648

49+
Defines an inline [can-define/list/list] type that's an array of `Type` or inline [can-define.types.propDefinition] [can-define/map/map]
50+
instances. For example:
51+
52+
```js
53+
import {DefineMap} from "can";
54+
55+
const List = DefineMap.extend({
56+
people: {
57+
Type: [ Person ]
58+
},
59+
addresses: {
60+
Type: [ {
61+
street: "string",
62+
city: "string"
63+
} ]
64+
}
65+
});
66+
67+
const myList = new List({
68+
people: [ {first: "Justin", last: "Meyer"} ],
69+
addresses: [ {street: "11 Example Ave.", city: "Chicago"} ]
70+
});
71+
72+
console.log( myList.serialize() );
73+
```
74+
@codepen
5775

5876
@body
5977

6078
## Use
6179

6280
```js
63-
import { DefineMap } from "can";
81+
import {DefineMap, Reflect as canReflect} from "can";
6482

6583
const Address = DefineMap.extend( {
66-
street: "string",
67-
city: "string"
84+
street: "string",
85+
city: {type:"string", default: "Chicago"}
6886
} );
6987

7088
const Direction = DefineMap.extend( {
71-
from: { Type: Address },
72-
to: Address
89+
from: { Type: Address },
90+
to: Address
7391
} );
7492

7593
const direction = new Direction( {
76-
from: { street: "2060 N. Stave", city: "Chicago" },
77-
to: new Address( { street: "123 Greenview", city: "Libertyville" } )
94+
from: {street: "2060 N. Stave"},
95+
to: new Address( {street: "123 Greenview", city: "Libertyville"} )
7896
} );
97+
98+
console.log( direction.from instanceof Address ); //-> true
99+
console.log( direction.serialize() ); //-> {
100+
// from: {city: "Chicago", street: "2060 N. Stave"}
101+
// to: {city: "Libertyville", street: "123 Greenview"}
102+
// }
79103
```
80-
@codepen
104+
@codepen

Diff for: docs/default.md

+42-26
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,48 @@ is read for the first time.
66

77
@signature `default()`
88

9-
A function can be provided that returns the default value used for this property, like:
9+
A function can be provided that returns the default value used for this property, like:
1010

11-
```js
12-
{
13-
prop: {
14-
default: function() {
15-
return [];
16-
}
17-
}
18-
}
19-
```
11+
```js
12+
import {DefineMap} from "can";
2013

14+
const Example = DefineMap.extend( {
15+
prop: {
16+
default: function() {
17+
return [];
18+
}
19+
}
20+
} );
2121

22-
If the default value should be an object of some type, it should be specified as the return value of a function (the above call signature) so that all instances of this map don't point to the same object. For example, if the property `value` above had not returned an empty array but instead just specified an array using the next call signature below, all instances of that map would point to the same array (because JavaScript passes objects by reference).
22+
const ex = new Example();
23+
console.log( ex.prop.serialize() ); //-> []
24+
```
25+
@codepen
2326

24-
@return {*} The default value. This will be passed through setter and type.
27+
If the default value should be an object of some type, it should be specified as the return value of a function (the above call signature) so that all instances of this map don't point to the same object. For example, if the property `value` above had not returned an empty array but instead just specified an array using the next call signature below, all instances of that map would point to the same array (because JavaScript passes objects by reference).
28+
29+
@return {*} The default value. This will be passed through setter and type.
2530

2631
@signature `default`
2732

28-
Any value can be provided as the default value used for this property, like:
33+
Any value can be provided as the default value used for this property, like:
2934

30-
```js
31-
{
32-
prop: {
33-
default: 'foo'
34-
}
35-
}
36-
```
35+
```js
36+
import {DefineMap} from "can"
37+
38+
const Example = DefineMap.extend( {
39+
prop: {
40+
default: "foo"
41+
}
42+
} );
43+
44+
const ex = new Example();
45+
console.log( ex.prop ); //-> "foo"
3746

38-
@param {*} defaultVal The default value, which will be passed through setter and type.
47+
```
48+
@codepen
49+
50+
@param {*} defaultVal The default value, which will be passed through setter and type.
3951

4052
@body
4153

@@ -44,24 +56,28 @@ Any value can be provided as the default value used for this property, like:
4456
The following defaults `age` to `0` and `address` to an object:
4557

4658
```js
47-
import { DefineMap } from "can"
48-
// A default age of `0`:
59+
import {DefineMap} from "can";
60+
61+
4962
const Person = DefineMap.extend( {
63+
// A default age of `0`:
5064
age: {
5165
default: 0
5266
},
67+
// A default address:
5368
address: {
54-
default: function() {
69+
default() {
5570
return { city: "Chicago", state: "IL" };
5671
}
5772
}
5873
} );
5974

6075
const person = new Person();
61-
console.log(person.age); //-> 0
76+
console.log( person.age ); //-> 0
77+
console.log( person.address.serialize() ); //-> { city: "Chicago", state: "IL" }
6278
```
6379
@codepen
6480

6581
## Alternates
6682

67-
There is a third way to provide a default value, which is explained in the [can-define.types.defaultConstructor] docs page. `default` lowercased is for providing default values for a property type, while `Default` uppercased is for providing a constructor function, which will be invoked with `new` to create a default value for each instance of this map.
83+
There is a third way to provide a default value, which is explained in the [can-define.types.defaultConstructor] docs page. `default`, lowercase, is for providing default values for a property type, while `[can-define.types.defaultConstructor Default]`, uppercase, is for providing a constructor function, which will be invoked with `new` to create a default value for each instance of this map.

Diff for: docs/defaultConstructor.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@ Provides a constructor function to be used to provide a default value for a prop
55

66
@signature `Default`
77

8-
A constructor function can be provided that is called to create a default value used for this property.
9-
This constructor will be invoked with `new` for each created instance. The default
10-
value is created on demand when the property is read for the first time.
11-
12-
Specify `Default` like:
13-
14-
```js
15-
{
16-
prop: {
17-
Default: Array
18-
},
19-
person: {
20-
Default: Person
21-
}
22-
}
23-
```
8+
A constructor function can be provided that is called to create a default value used for this property.
9+
This constructor will be invoked with `new` for each created instance. The default
10+
value is created on demand when the property is read for the first time.
11+
12+
Specify `Default` like:
13+
14+
```js
15+
{
16+
prop: {
17+
Default: Array
18+
},
19+
person: {
20+
Default: Person
21+
}
22+
}
23+
```
2424

2525
@body
2626

2727
## Use
2828

2929
```js
30-
import { DefineMap } from "can";
30+
import {DefineMap} from "can";
3131

3232
const Address = DefineMap.extend( {
3333
street: { type: "string", value: "321 Longbow" },
@@ -43,7 +43,7 @@ const direction = new Direction( {
4343
to: { street: "2070 N. Stave" }
4444
} );
4545

46-
console.log(direction.from.street); //-> "321 Longbow"
47-
console.log(direction.to.street); //-> "2070 N. Stave"
46+
console.log( direction.from.street ); //-> "321 Longbow"
47+
console.log( direction.to.street ); //-> "2070 N. Stave"
4848
```
4949
@codepen

Diff for: docs/define.md

+23-28
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,35 @@ and [can-define/list/list] are documented here.
3535
message: { type: "string" }
3636
} );
3737

38-
var greeting = new Greeting("Hello");
38+
const greeting = new Greeting("Hello");
3939

4040
canReflect.onKeyValue(greeting, "message", (newValue) => {
41-
console.log(newValue); // logs "goodbye"
41+
console.log( newValue.target.message ); //-> logs "goodbye"
4242
});
4343

4444
greeting.message = "goodbye";
4545
```
4646
@codepen
4747

48-
@param {Object} prototype The prototype object of a constructor function or [class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class). The prototype
49-
object will have getter/setters defined on it that carry out the defined behavior. The prototype will also contain
50-
all of [can-event-queue/map/map]'s methods.
48+
@param {Object} prototype The prototype object of a constructor function or [class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class). The prototype object will have getter/setters defined on it that carry out the defined behavior. The prototype will also contain all of [can-event-queue/map/map]'s methods.
5149

52-
@param {Object<String,can-define.types.propDefinition>} propDefinitions An object of
53-
properties and their definitions. For example, a property (`propertyName`) has a [can-define.types.propDefinition] object with zero or more of the following behaviors:
50+
@param {Object<String,can-define.types.propDefinition>} propDefinitions An object of properties and their definitions. For example, a property (`propertyName`) has a [can-define.types.propDefinition] object with zero or more of the following behaviors:
5451

55-
```js
56-
define(Type.prototype, {
52+
```js
53+
define(Type.prototype, {
5754
propertyName: {
58-
default: function() { /* ... */ },
59-
Default: Constructor,
60-
type: function() { /* ... */ },
61-
Type: Constructor,
62-
get: function() { /* ... */ },
63-
value: function() { /* ... */ },
64-
set: function() { /* ... */ },
65-
serialize: function() { /* ... */ },
66-
identity: Boolean
55+
default: function() { /* ... */ },
56+
Default: Constructor,
57+
type: function() { /* ... */ },
58+
Type: Constructor,
59+
get: function() { /* ... */ },
60+
value: function() { /* ... */ },
61+
set: function() { /* ... */ },
62+
serialize: function() { /* ... */ },
63+
identity: Boolean
6764
}
68-
})
69-
```
70-
65+
})
66+
```
7167

7268
@body
7369

@@ -80,8 +76,7 @@ more assumptions on the type constructor. `can-define` can be used
8076
to create completely customized types.
8177

8278

83-
The following creates a
84-
`Person` constructor function that
79+
The following creates a `Person` constructor function that
8580
will be used to create `Person` instances with observable properties:
8681

8782
```js
@@ -105,13 +100,13 @@ define( Person.prototype, {
105100
// Create an instance
106101
const person = new Person( "Justin", "Meyer" );
107102

108-
console.log( person.first ) //-> "Justin"
109-
console.log( person.last ) //-> "Meyer"
110-
console.log( person.fullName ) //-> "Justin Meyer"
103+
console.log( person.first ); //-> "Justin"
104+
console.log( person.last ); //-> "Meyer"
105+
console.log( person.fullName ); //-> "Justin Meyer"
111106

112107
person.on( "fullName", function( ev, newVal, oldVal ) {
113-
console.log( newVal ) //-> "Ramiya Meyer"
114-
console.log( oldVal ) //-> "Justin Meyer"
108+
console.log( newVal ); //-> "Ramiya Meyer"
109+
console.log( oldVal ); //-> "Justin Meyer"
115110
} );
116111

117112
person.first = "Ramiya";

0 commit comments

Comments
 (0)