can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
36 lines (29 loc) • 1.07 kB
Markdown
Map.prototype.attributes.serialize serialize
can.Map.attributes.prototype 0
Serializes the observe's properties using
the [can.Map.attributes attribute plugin].
`observe.serialize([attrName])`
{String} [attrName] If passed, returns only a serialization of the named attribute.
{String} A serialization of this Observe.
You can set the serialization methods similar to the convert methods:
var Contact = can.Map.extend({
attributes : {
birthday : 'date'
},
serialize : {
date : function( val, type ){
return val.getYear() +
"-" + (val.getMonth() + 1) +
"-" + val.getDate();
}
}
},{})
var contact = new Contact();
contact.attr('birthday', new Date());
contact.serialize()
//-> { birthday: 'YYYY-MM-DD' }
You can also get and serialize an individual property by passing the attribute
name to the `serialize` function. Building on the above demo, we can serialize
the `birthday` attribute only.
contact.serialize('birthday') //-> 'YYYY-MM-DD'
can.