- 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
| Directive | English | Where it runs | Form data |
|---|---|---|---|
&НаКлиенте | &AtClient | on the client | available |
&НаСервере | &AtServer | on the server, with the form | available, but sent to the server |
&НаСервереБезКонтекста | &AtServerNoContext | on the server, parameters only | not available |
&НаКлиентеНаСервереБезКонтекста | &AtClientAtServerNoContext | on both client and server | not 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.
- 1Create the form
Open the
ПриходТовараdocument, on theФормыtab clickДобавить, chooseФорма документа(document form) andГотово. The form editor opens. - 2Add 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. - 3Attach 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.
&НаСервере
Процедура ПриСозданииНаСервере(Отказ, СтандартнаяОбработка)
Если Объект.Ссылка.Пустая() Тогда
Объект.Комментарий = "Создан из формы";
КонецЕсли;
КонецПроцедуры
&НаКлиенте
Процедура ТоварыТоварПриИзменении(Элемент)
ТекущиеДанные = Элементы.Товары.ТекущиеДанные;
ТекущиеДанные.Цена = ЦенаТовара(ТекущиеДанные.Товар);
ТекущиеДанные.Сумма = ТекущиеДанные.Количество * ТекущиеДанные.Цена;
КонецПроцедуры
&НаСервереБезКонтекста
Функция ЦенаТовара(Товар)
Возврат Товар.Цена; // через точку от ссылки - только на сервере
КонецФункцииЦенаТовара 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 Экспорт.
&НаКлиенте
Процедура Очистить(Команда)
Оповещение = Новый ОписаниеОповещения("ОчиститьЗавершение", ЭтотОбъект);
ПоказатьВопрос(Оповещение, "Очистить табличную часть?", РежимДиалогаВопрос.ДаНет);
КонецПроцедуры
&НаКлиенте
Процедура ОчиститьЗавершение(Ответ, ДополнительныеПараметры) Экспорт
Если Ответ = КодВозвратаДиалога.Да Тогда
Объект.Товары.Очистить();
КонецЕсли;
КонецПроцедурыЭтотОбъект (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.