-
Notifications
You must be signed in to change notification settings - Fork 421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can.Map.prototype.define #819
Comments
From my limited testing the setter is not getting called when removeAttr is The event is being thrown, but no setter is being called.
|
I like the removeATTR functionality. I would also propose that we pass in the value that the attribute was at It should be noted that I do not have a use case for accepting the value of
|
+1 looks like a good, clean API there was overlap between setter and attributes, so this is clearer |
What does |
As discussed in the hangout today, this will be added to can.List as well. Adding it here to make sure it doesn't get lost. |
I see it as a clean API for defining business logic services I like it |
convert (i think what is "type" for?) and serialize should be added as well. |
Yes |
@justinbmeyer @whitecolor do you mean an equivalent for http://canjs.com/docs/can.Map.attributes.static.serialize.html, but defined for each attribute? |
yes, and as api propsal |
I like that idea. Makes sense for things like a can.List of objects that will be represented in its serialized form elsewhere by a list of Ids. For example (similar to #752 (comment)): locations: {
serialize: false
set: function(val){
},
},
locationIds: {
get: function(){
return this.attr('locations').filter(function(location){
return this.location.attr('selected');
});
},
serialize: function(locationIds){ return this.join(','); }
} |
Are you going to add here plain object/array attributes support (that won't be converted to can.Map)? |
Another feature that could be implemented in this plugin, is to allow out of the box setting deferred values to attributes // so this would wait for findAll result and assign it to "items"
app.attr('items', Item.findAll) for this in define there could be defined some property
|
You could do this within set yourself:
|
Yes, you do that like: define: {
plain: { type: "*" }
} |
Yes, I understand I meant maybe to support setting deferreds out of the box. is the plugin ready for use/test? |
this has been merged into minor |
It seems to be a wrong context inside serialize function ("this" is window object) |
I'm proposing a
can/map/define
plugin that would clean up several issues with thecan/map/setter
andcan/map/attributes
plugin. It will be a replacement for those plugins.Example
Issues to think about:
Issue this should fix:
.attr('attr', newVal)
and causes a stack overflow.Issue this might fix:
can.Map.prototype.define
@Property {Object<String,can.Map.attrDefinition>} can.Map.prototype.define
Defines a map attributes setter, type, and remove functionality and default values.
Use
A
can.Map
's define property is used to define behavior for attributes of instance of thecan.Map
. The following defines a make-model-year view model that:can.Map.prototype.attrDefinition
@typedef {{set: can.Map.defineSetter, remove: can.Map.defineRemover, value: *|can.Map.defineDefaulter, type: can.Map.defineConverter | can.Map.defineType, Type: function}} can.Map.prototype.attrDefinition
Defines a map attributes setter, type, and remove functionality and default values.
@option {can.Map.defineConverter | can.Map.defineType} type A function that converts the
value to a specified type. Example:
@option {function} Type A constructor function that takes the value as the first argument and called with new.
can.Map.defineSetter
@function can.Map.defineSetter
@Signature
setter( newVal, setValue )
@return {*} If a non undefined value is returned, the value returned is set on the map.
@Body
Use
The following shows an async setter:
can.Map.defineRemover
@function can.Map.defineRemover
Called when an attribute is removed.
@Signature
remover( currentValue )
@return {*|false} If
false
is returned, the value is not removed.@Body
Use
The following prevents removing the prop attribute if someone tries to remove the value 0:
can.Map.defineDefaulter
@function can.Map.defineDefaulter
Returns the default value for instances of this can.Map. This is called before
init
.@Signature
defaulter()
@return {*} The default value. This will be passed through setter and type.
can.Map.defineType
@function can.Map.defineType
A function that converts what's returned by the setter to some type.
The text was updated successfully, but these errors were encountered: