====
copy
====

.. js:function:: copy(options)

**domain**: client 

**language**: javascript

**class** :doc:`Item class </refs/client/item_api>`

Description
===========

Use copy to create a copy of an item. The created copy is not added to the 
:doc:`task tree </programming/task_tree>`
and will be destroyed by JavaScript garbage collection process when no longer 
needed.

All attributes of the copy object are defined as they were at the time of loading
of the task tree when application started. See 
:doc:`Workflow </programming/workflow>`

``Options`` parameter further specifies the created copy. It can have the 
following attributes:

* ``handlers`` - if the value of this attribute is ``true``, all the settings 
  to the item made in the Form Dialogs in the Application Builder and all the 
  functions and events defined in the client module of the item will also be 
  available in the copy. The default value is ``true``.
* ``filters`` - if the value of this attribute is ``true``, the filters will be 
  created for the copy, otherwise there will be no filters. The default value 
  is ``true``.
* ``details`` - if the value of this attribute is ``true``, the details will be 
  created for the copy, otherwise there will be no details. The default value 
  is ``true``.


Example
=======

The following code is used in the 
:doc:`Demo project </intro/demo_project>`
to asynchronously calculate total values of the fields, displayed at the foot of 
the **Invoice** journal table:

.. code-block:: js

    function on_filter_applied(item) {
        var copy = item.copy({handlers: false, details: false});
        copy.assign_filters(item);
        copy.open(
            {fields: ['subtotal', 'tax', 'total'], 
            funcs: {subtotal: 'sum', tax: 'sum', total: 'sum'}}, 
            function() {
                var footer = item.view_form.find('.dbtable.' + item.item_name + ' tfoot');
                copy.each_field(function(f) {
                    footer.find('div.' + f.field_name)
                        .css('text-align', 'right')
                        .css('color', 'black')
                        .text(f.display_text);
                });
            }
        );
    }

See also
========

:doc:`Task tree </programming/task_tree>`

:doc:`Workflow </programming/workflow>`