DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
        • Type
        • Value
        • type
        • attribute definition
        • get
        • remove
        • serialize
        • set
        • value
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-map-define

  • npm package badge
  • Star
  • Edit on GitHub

Defines the type, initial value, get, set, remove, and serialize behavior for attributes of a Map.

Object

Options

  • define {Object}:

    Exports object with helper methods internal to can-map-define. The export of can-map-define is not used directly, instead the module is imported and all can-map's can have their properties defined like:

    var CanMap = require("can-map");
    require("can-map-define");
    
    var Person = CanMap.extend({
        define: {
            fullName: function(){
                return this.attr("first") + " "+ this.attr("last")
            }
        }
    })
    

Use

The define plugin allows you to completely control the behavior of attributes on a Map. To use it, you specify an define object that is a mapping of properties to attribute definitions. The following example specifies a Paginate Map:

var Paginate = Map.extend({
  define: {
    count: {
      type: "number",
      value: Infinity,
      // Keeps count above 0.
      set: function(newCount){
        return newCount < 0 ? 0 : newCount;
      }
    },
    offset: {
      type: "number",
      value: 0,
      // Keeps offset between 0 and count
      set: function(newOffset){
        var count = this.attr("count");
        return newOffset < 0 ?
          0 :
          Math.min(newOffset, !isNaN( count - 1) ?
            count - 1 :
            Infinity);
      }
    },
    limit: {
      type: "number",
      value: 5
    },
    page: {
      // Setting page changes the offset
      set: function(newVal){
        this.attr('offset', (parseInt(newVal) - 1) *
                             this.attr('limit'));
      },
      // The page value is derived from offset and limit.
      get: function (newVal) {
        return Math.floor(this.attr('offset') /
                          this.attr('limit')) + 1;
      }
    }
  }
});

Default behaviors

The define plugin not only allows you to define individual attribute behaviors on a Map, but you can also define default behaviors that would apply to any unspecified attribute. This is particularly helpful for when you need a particular behavior to apply to every attribute on a Map but won't be certain of what every attribute will be.

The following example is a Map that is tied to route where only specified attributes that are serialized will be updated in the location hash:

var State = Map.extend({
  define: {
    foo: {
      serialize: true
    },
    '*': {
      serialize: false
    }
  }
});

var state = new State();

// tie State map to the route
route.map(state);
route.ready();

state.attr('foo', 'bar');
state.attr('bar', 'baz');

window.location.hash; // -> #!foo=bar

Overview

This plugin is a replacement for the now deprecated attributes and setter plugins. It intends to provide a single place to define the behavior of all the properties of a Map.

Here is the cliffnotes version of this plugin. To define...

  • The default value for a property - use value
  • That default value as a constructor function - use Value
  • What value is returned when a property is read - use get
  • Behavior when a property is set - use set
  • How a property is serialized when serialize is called on it - use serialize
  • Behavior when a property is removed - use remove
  • A custom converter method or a pre-defined standard converter called whenever a property is set - use type
  • That custom converter method as a constructor function - use Type

Demo

The following shows picking cars by make / model / year:

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

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news