DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
    • can-component
    • can-compute
    • can-connect
    • can-define
      • static
        • types
      • types
        • PropDefinition
      • behaviors
        • Type
        • Value
        • get
        • serialize
        • set
        • type
        • value
    • can-define/list/list
    • can-define/map/map
    • can-route
    • can-route-pushstate
    • can-set
    • can-stache
    • can-stache/helpers/route
    • can-stache-bindings
  • Ecosystem
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Core
  • /
  • can-define
  • /
  • type
  • / On this page
    • type

      typedef

      Converts a value set on an instance into an appropriate value.

      • source

      type(newValue, propertyName)

      Given the set value, transform it into a value appropriate to be set. type is called before set.

      age: {
          type: function(newValue, propertyName){
              return +newValue;
          }
      }
      

      Parameters

      1. newValue {*}:

        The value set on the property.

      2. propertyName {String}:

        The property name being set.

      Returns

      {*}:

      The value that should be passed to set or (if there is no set property) the value to set on the map instance.

      "typeName"

      Sets the type to a named type in types. The default typeName is "observable".

      age: {
          type: "number"
      }
      

      Parameters

      1. typeName {String}:

        A named type in types.

      {propDefinition}

      A PropDefinition that defines an inline can-define/map/map type. For example:

      address: {
          type: {
              street: "string",
              city: "string"
          }
      }
      

      [Type|propDefinition]

      Defines an inline can-define/list/list type that's an array of Type or inline propDefinition can-define/map/map instances. For example:

      people: {
          type: [Person]
      },
      addresses: {
          type: [{
              street: "string",
              city: "string"
          }]
      }
      

      Use

      The type property specifies the type of the attribute. The type can be specified as either:

      • A type function that returns the type coerced value.
      • A named type in types.
      • An object that gets converted into an inline DefineMap.
      • An array that gets converted to an inline DefineList.

      Basic Example

      The following example converts the count property to a number and the items property to an array:

      DefineMap.extend({
          count: {type: "number"},
          items: {
              type: function(newValue){
                  if(typeof newValue === "string") {
                      return newValue.split(",")
                  } else if( Array.isArray(newValue) ) {
                      return newValue;
                  }
              }
          }
      });
      

      When a user tries to set those properties like:

      map.set({count: "4", items: "1,2,3"});
      

      The number converter will be used to turn count into 4, and the items type converter function will be used to turn items into [1,2,3].

      Preventing Arrays and Objects from Automatic Conversion

      When an array value is set, it is automatically converted into a DefineList. Likewise, objects are converted into DefineMap instances. This behavior can be prevented like the following:

       locations: {type: "any"}
      

      When a user tries to set this property, the resulting value will remain an array.

      map.locations = [1, 2, 3]; // locations is an array, not a DefineList
      

      Working with the 'compute' type

      Setting type as compute allows for resolving a computed property with the .attr() method.

      MyMap = DefineMap.extend({
          value: {
              type: "compute"
          }
      });
      
      var myMap = new MyMap();
      var c = compute(5);
      
      myMap.value = c;
      myMap.value //-> 5
      
      c(6);
      myMap.value //-> 6
      
      //Be sure if setting to pass the new compute
      var c2 = compute("a");
      myMap.value = c2;
      myMap.value //-> "a"
      

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.