attributes
can/map/attributesDeprecated 2.1
The attributes plugin (and the setter plugin) have been deprecated in favor of the new define plugin, which provides the same functionality. It will still be maintained up to 3.0 and potentially after. Projects using converters or serializers should consider switching to define's type and define's serialize.
Use
can.Map.attributes is a plugin that helps convert and normalize data being set on an Map and allows you to specify the way complex types get serialized. The attributes plugin is most helpful when used with can.Model (because the serialization aids in sending data to a server), but you can use it with any Map you plan to make instances from.
There are three important static properties to give the class you want to use attributes with:
attributes
lists the properties that will be normalized and the types those properties should be.convert
lists how to convert and normalize arbitrary values to the types this class uses.serialize
lists serialization algorithms for the types this class uses.Together, the functions in convert and serialize make up the type definitions for the class. The attributes plugin comes with three useful predefined types:
'date'
,'number'
, and'boolean'
.Here is a quick example of an Map-based class using the attributes plugin to convert and normalize its data, and then to serialize the instance:
Demo
When a user enters a new date in the format of YYYY-MM-DD, the control listens for changes in the input box and updates the Map using the
attr
method which then converts the string into a JavaScript date object.Additionally, the control also listens for changes on the Map and updates the age in the page for the new birthdate of the contact.
Reference types
Types listed in
attributes
can also be a functions, such as themodel
ormodels
methods of a can.Model. When data of this kind of type is set, this function is used to convert the raw data into an instance of the Model.This example builds on the previous one to demonstrate these reference types.
The Attributes plugin provides functionality for converting data attributes from raw types and serializing complex types for the server.
Below is an example code of an Map providing serialization and conversion for dates and numbers.
When
Contact
is initialized, theweight
attribute is set and converted to anumber
using the converter we provided. Next thebirthday
attribute is set using theattr
method and gets converted as well. Lastly,serialize
is invoked converting the new attributes to raw types for the server.Converter functions
Another common case is to create converter functions (
function(value, oldValue) {}
) that return a converted value:Associations
The attribute plugin also allows setting up data associations between Maps or Models. This means that nested data structures can be automatically converted into their Map or Model (using
Model.models
) representations by passing them as the attribute. If the value to convert is an array it will be converted into itscan.Map.List
orcan.Model.List
(usingcan.Model.models
) representation:Assuming that
Zelda.findOne({ id: 'link' })
will return something like:The converted data will contain a list or Levels and a sword Model:
Demo
Below is a demo that showcases associations between 2 different models to show the tasks for each contact and how much time they have left to complete the task(s) using converters.