Skip to content
Educora
Intermediate18 min7 / 14

Client–server and managed forms

Control where code runs with compilation directives, learn the elements, attributes and commands of a managed form, handle events such as `ПриСозданииНаСервере`, and use server calls sparingly.

Check yourself
In this lesson you will learn
  • Choose correctly between &НаКлиенте, &НаСервере and &НаСервереБезКонтекста
  • Distinguish a form's elements, attributes and commands
  • Handle form events and build non-modal dialogs

Leyla opens a document in her browser at an office in Baku, while the database sits on a server in a data centre in another city. Every “conversation” between Leyla's computer and the server takes time. That is why in 1C every procedure must say where it runs: on the client (the user's computer) or on the server.

Compilation directives

DirectiveEnglishWhere it runsForm data
&НаКлиенте&AtClienton the clientavailable
&НаСервере&AtServeron the server, with the formavailable, but sent to the server
&НаСервереБезКонтекста&AtServerNoContexton the server, parameters onlynot available
&НаКлиентеНаСервереБезКонтекста&AtClientAtServerNoContexton both client and servernot available

On the client you can only work with the interface. Queries, the object model (Справочники.Товары.СоздатьЭлемент()), ТаблицаЗначений and reading an attribute through a reference with a dot (Товар.Цена) are server-only. Code that breaks this rule either does not compile or fails at run time.

How a managed form is built

  • Attributes (Реквизиты) — the form's data. In an object form the main attribute is Объект: Объект.Дата, Объект.Товары.
  • Elements (Элементы) — what you see: fields, tables, groups, buttons. They are bound to attributes.
  • Commands (Команды) — user actions; each command has an Action (Действие) procedure.
  • Module — the form's code: event handlers and helper procedures, each with its directive.
  1. 1
    Create the form

    Open the ПриходТовара document, on the Формы tab click Добавить, choose Форма документа (document form) and Готово. The form editor opens.

  2. 2
    Add a command

    On the Команды tab create a new command (for example Очистить), create its handler with the magnifier button of the Действие property, then drag the command onto the form's command bar.

  3. 3
    Attach an event

    Select the Товар column in the table and click the magnifier of the ПриИзменении (OnChange) event in its properties — the procedure ТоварыТоварПриИзменении appears in the module.

Form events and server calls

When a form opens, ПриСозданииНаСервере (OnCreateAtServer) runs on the server first (this is where you read parameters and prepare data), then ПриОткрытии (OnOpen) on the client. On writing, ПередЗаписью (client) and ПередЗаписьюНаСервере (server) are called. In the example below, choosing a product fetches its price from the server.

1C
&НаСервере
Процедура ПриСозданииНаСервере(Отказ, СтандартнаяОбработка)
    Если Объект.Ссылка.Пустая() Тогда
        Объект.Комментарий = "Создан из формы";
    КонецЕсли;
КонецПроцедуры

&НаКлиенте
Процедура ТоварыТоварПриИзменении(Элемент)
    ТекущиеДанные = Элементы.Товары.ТекущиеДанные;
    ТекущиеДанные.Цена = ЦенаТовара(ТекущиеДанные.Товар);
    ТекущиеДанные.Сумма = ТекущиеДанные.Количество * ТекущиеДанные.Цена;
КонецПроцедуры

&НаСервереБезКонтекста
Функция ЦенаТовара(Товар)
    Возврат Товар.Цена;  // через точку от ссылки - только на сервере
КонецФункции
ЦенаТовара is a no-context call: only one reference travels to the server, not the whole form.

Non-modal dialogs

Browsers dislike modal windows, so modern configurations use ПоказатьВопрос() (ShowQueryBox) instead of the old Вопрос(). The answer arrives in a notification handler, described by ОписаниеОповещения (NotifyDescription); the handler procedure must be Экспорт.

1C
&НаКлиенте
Процедура Очистить(Команда)
    Оповещение = Новый ОписаниеОповещения("ОчиститьЗавершение", ЭтотОбъект);
    ПоказатьВопрос(Оповещение, "Очистить табличную часть?", РежимДиалогаВопрос.ДаНет);
КонецПроцедуры

&НаКлиенте
Процедура ОчиститьЗавершение(Ответ, ДополнительныеПараметры) Экспорт
    Если Ответ = КодВозвратаДиалога.Да Тогда
        Объект.Товары.Очистить();
    КонецЕсли;
КонецПроцедуры
ЭтотОбъект (ThisObject) is the form that contains the handler. Newer platform versions also let you write the same with the words Асинх (Async) and Ждать (Await).

Key points

  • Every form procedure has a directive: &НаКлиенте, &НаСервере, &НаСервереБезКонтекста.
  • Queries, objects, ТаблицаЗначений and reading through a reference's dot are server-only.
  • Form = attributes (data) + elements (look) + commands (actions) + module.
  • ПриСозданииНаСервере runs first on the server, ПриОткрытии later on the client.
  • Minimise server calls: never call in a loop, prefer no-context calls.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
Which directive runs code on the server without sending the form data there?