DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
    • can-component
    • can-compute
    • can-connect
      • behaviors
        • ./base/
        • ./cache-requests/
        • ./can/map/
        • ./can/ref/
        • ./constructor/callbacks-once/
        • ./constructor/
          • crud methods
            • destroy
            • get
            • getList
            • save
          • crud callbacks
            • createdInstance
            • destroyedInstance
            • updatedInstance
            • updatedList
          • hydrators
            • hydrateInstance
            • instance
            • list
            • hydrateList
          • serializers
            • serializeInstance
            • serializeList
          • helpers
            • cidStore
            • isNew
        • ./constructor/store/
        • ./data/callbacks/
        • ./data/callbacks-cache/
        • ./data/combine-requests/
        • ./data/localstorage-cache/
        • ./data/memory-cache/
        • ./data/parse/
        • ./data/url/
        • ./data/worker/
        • ./fall-through-cache/
        • ./real-time/
      • modules
        • ./can/base-map/
        • ./can/model/
        • ./can/super-map/
        • ./can/tag/
        • ./helpers/weak-reference-map
      • data types
        • DataInterface
        • Instance
        • InstanceInterface
        • List
        • ListData
    • can-define
    • 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-connect
  • /
  • ./constructor/
  • /
  • save
  • / On this page
    • save

      function

      Creates or updates an instance using the underlying data interface.

      • source

      connection.save( instance )

      Checks if the instance has an id or not. If it has an id, the instance will be updated; otherwise, it will be created.

      To create an instance, the instance is added to the [can-connect/constructor.cidStore], and its serialized data is passed to createData. If createData's promise resolves to anything other than undefined, createdInstance is called.

      To update an instance, its serialized data is passed to updateData. If updateData's promise resolves to anything other than undefined, updatedInstance is called.

      Parameters

      1. instance {Instance}:

        The instance to create or save.

      Returns

      {Instance}:

      resolves to the same instance that was passed to save.

      Use

      To use save, create a connection, then an instance, and call .save() with it.

      // Create a connection:
      var todoConnection = connect([
        require('can-connect/constructor/constructor'),
        require('can-connect/data/url/url')
      ],{
        url: "/todos"
      })
      
      // Create an instance:
      var todo = {name: "do dishes"};
      
      // Call .save():
      todoConnection.save(todo)
      

      This will POST to /todos with the todo data. The server response data might look something like:

      {
       id: 5,
       ownerId: 9
      }
      

      This data will be passed to createdInstance which will default to adding those properties to todo, resulting in todo looking like:

      {
       name: "do dishes",
       id: 5,
       ownerId: 9
      }
      

      To update the todo, change a property and call .save() again:

      // Change a property:
      todo.name = "Do dishes!!";
      
      // Call .save()
      todoConnection.save(todo)
      

      This will PUT to /todos with the todo data. The server response data should look something like:

      {
       name: "Do dishes!!",
       id: 5,
       ownerId: 9
      }
      

      This data will be passed to updatedInstance which will default to setting all of todos properties to look like the response data.

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