forked from canjs/canjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'map-define' into list-promise-docs
- Loading branch information
Showing
17 changed files
with
422 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@function can.Map.prototype.define.TypeConstructor Type | ||
@parent can.Map.prototype.define | ||
|
||
Provides a constructor function to be used to convert any value passed into [can.Map::attr attr] into an appropriate value | ||
|
||
|
||
@signature `constructorFunc` | ||
|
||
A constructor function can be provided that is called to convert incoming values set on this property, like: | ||
|
||
define: { | ||
prop: { | ||
Type: Person | ||
} | ||
} | ||
|
||
@body | ||
|
||
Similar to [can.Map.prototype.define.type type], this uppercase version provides a mechanism for converting incoming values to another format or type. | ||
|
||
Specifically, this constructor will be invoked any time this property is set, and any data passed into the setter will be passed as arguments for the constructor. | ||
|
||
If the call to attr passes an object that is already an instance of the constructor specified with `Type`, no conversion is done. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@function can.Map.prototype.define.ValueConstructor Value | ||
@parent can.Map.prototype.define | ||
|
||
Provides a constructor function to be used to provide a default value for a certain property of a can.Map. This constructor will be invoked with `new` each time a new instance of the map is created. | ||
|
||
@signature `constructorFunc` | ||
|
||
A constructor function can be provided that is called to create a default value used for this property, like: | ||
|
||
define: { | ||
prop: { | ||
Value: Array | ||
}, | ||
person: { | ||
Value: Person | ||
} | ||
} | ||
|
||
@body | ||
|
||
Similar to [can.Map.prototype.define.value value], this uppercase version provides a mechanism for providing a default value. If the default value is an object, providing a constructor is a good way to ensure a copy is made for each instance. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,52 @@ | ||
@function can.Map.prototype.define.get get | ||
@parent can.Map.prototype.define | ||
@parent can.Map.prototype.define | ||
|
||
Specify what happens when a certain property is read on a map. | ||
|
||
|
||
@signature `get( )` | ||
|
||
A get function defines the behavior of what happens when a value is read on a | ||
[can.Map]. It is typically used to provide properties that derive their value from other properties of the map. | ||
|
||
@return {*} Anything can be returned from a getter. | ||
|
||
@body | ||
|
||
## Use | ||
|
||
Getter methods are useful for defining virtual properties on a map. These are properties that don't actually store any value, but derive their value from some other properties on the map. | ||
|
||
Whenever a getter is provided, it is wrapped in a [can.compute], which ensures that whenever its dependent properties change, a change event will fire for this property also. | ||
|
||
@codestart | ||
var Person = can.Map.extend({ | ||
define: { | ||
fullName: { | ||
get: function () { | ||
return this.attr("first") + " " + this.attr("last"); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
var p = new Person({first: "Justin", last: "Meyer"}); | ||
|
||
p.attr("fullName"); // "Justin Meyer" | ||
@codeend | ||
|
||
Below is another example of using get to create a dependent "virtual" property. This map has a locations property, which is a can.List containing location objects. Another property called locationIds is required, which is just an array of ids from each of the locations in the real location list. The value of locationIds is tied to locations, so a getter is useful. A user could bind to 'locationIds' and its event handler would be triggered if new locations were added, causing a change in the locationIds array. | ||
|
||
var Store = can.Map.extend({ | ||
define: { | ||
locationIds: { | ||
get: function(){ | ||
var ids = []; | ||
this.attr('locations').each(function(location){ | ||
ids.push(location.id); | ||
}); | ||
return ids; | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
@function can.Map.prototype.define.serialize serialize | ||
@parent can.Map.prototype.define | ||
|
||
Called when an attribute is removed. | ||
|
||
@signature `serializer( currentValue )` | ||
|
||
@return {*|false} If `false` is returned, the value is not serialized. | ||
|
||
@body | ||
|
||
## Use | ||
|
||
[can.Map::serialize serialize] is useful for serializing a can.Map instance into a more JSON-friendly form. This can be used for many reasons, including saving a can.Model instance on the server or serializing can.route's internal can.Map for display in the hash or pushstate URL. | ||
|
||
The serialize property allows an opportunity to define how each attribute will behave when the map is serialized. This can be useful for: | ||
|
||
* serializing complex types like dates, arrays, or objects into string formats | ||
* causing certain properties to be ignored when serialize is called | ||
|
||
The following causes a locationIds property to be serialized into the comma separated ID values of the location property on this map: | ||
|
||
define: { | ||
locationIds: { | ||
serialize: function(){ | ||
var ids = []; | ||
this.attr('locations').each(function(location){ | ||
ids.push(location.id); | ||
}); | ||
return ids.join(','); | ||
} | ||
} | ||
} | ||
|
||
Setting serialize to false for any property means this property will not be part of the serialized object. For example, if the property numPages is not greater than zero, the following example won't include it in the serialized object. | ||
|
||
define: { | ||
prop: { | ||
numPages: function( num ){ | ||
if(num <= 0) { | ||
return false; | ||
} | ||
return num; | ||
} | ||
} | ||
} |
Oops, something went wrong.