Client and server
Client and server code, compile directives, server calls and what can pass between client and server.
9 questions
JuniorTheoryVery commonWhere does code marked &НаКлиенте run, and where does &НаСервере code run?
Where does code marked &НаКлиенте run, and where does &НаСервере code run?
&НаКлиенте code runs in the user's application, the thin or web client, and handles the form and user actions. &НаСервере code runs on the 1C server, where the database is reachable; each call to it from the client is a server call.
Common mistakes
- ✗Reading database data in a
&НаКлиентеmethod: the client has no database access, so a server method must do it - ✗Treating a directive as a hint: it decides where the method is compiled and where it runs
Follow-up questions
- →What happens when a client method calls a server method of the same form?
- →Why can a
&НаКлиентеmethod not create aЗапрос?
JuniorTheoryVery commonHow does the directive &НаСервере differ from &НаСервереБезКонтекста?
How does the directive &НаСервере differ from &НаСервереБезКонтекста?
A &НаСервере method gets the form's context: form data travel to the server and back, so it can read and change Объект and form attributes. A &НаСервереБезКонтекста method gets only its parameters: a lighter call that cannot see the form.
Common mistakes
- ✗Using
&НаСервереfor a method that needs no form data: the whole form travels for nothing - ✗Reading
Объектin a method without context: the method cannot see it and does not compile
Follow-up questions
- →Why does a no-context method that reads
Объектfail to compile? - →How do you give a no-context method the form data it needs?
MiddleTheoryVery commonWhich values can and cannot be passed between the client and the server?
Which values can and cannot be passed between the client and the server?
Primitive values, references and the collections Массив, Структура, Соответствие and СписокЗначений, fixed ones too, can travel. ТаблицаЗначений, Запрос and objects such as СправочникОбъект cannot: they exist only on the server.
Common mistakes
- ✗Returning a value table from a server function to the client: copy it into an array of structures or a form attribute
- ✗Sending an object to the client to change it there: change it on the server and send back only the result
Follow-up questions
- →How does a form attribute of type
ТаблицаЗначенийreach the client if the type cannot travel? - →Why must a structure passed to the client hold only values that can travel too?
MiddleTheoryVery commonHow do you reduce the number of server calls made by a managed form?
How do you reduce the number of server calls made by a managed form?
Do all the server work of one user action in one call, not several, and never call the server in a loop. Prefer &НаСервереБезКонтекста with only the needed values. Cache rarely changing data in a common module with reuse of return values.
Common mistakes
- ✗Calling a server function for each row in a client loop: send the rows once and process them on the server
- ✗Passing the whole form with
&НаСервереwhen two values are needed: a call without context carries only its parameters
Follow-up questions
- →How do you measure how many server calls one user action makes?
- →Why does a call to a module with «Вызов сервера» still count as a server call?
JuniorDebuggingCommonWhat is wrong with the ОстатокНаСервере function?
What is wrong with the ОстатокНаСервере function?
Without context the function cannot see Объект: pass the form attribute as a parameter
Common mistakes
- ✗Using
Объектinside&НаСервереБезКонтекста: the form data are not there, so the needed value goes in as a parameter - ✗Expecting a reference to be unable to reach the server: references, numbers and strings all pass as parameters
Follow-up questions
- →How does
&НаСервереdiffer here from&НаСервереБезКонтекстаin the data sent to the server? - →When is
&НаСервереthe better choice for such a function?
JuniorTheoryCommonHow many server calls does this procedure make if both methods are server ones?
How many server calls does this procedure make if both methods are server ones?
4
Open full question →Common mistakes
- ✗Counting server calls by the number of server methods: every call in the loop is a server call of its own
- ✗Expecting the platform to merge the calls of one client procedure: each one goes to the server separately
Follow-up questions
- →How do you rewrite the procedure so it makes one server call?
- →Why does a server call inside a loop break the 1C standard?
JuniorTheoryCommonHow do you pass a value table from the server to the client?
How do you pass a value table from the server to the client?
Copy it into a form attribute or an array of structures: the table itself cannot travel
Common mistakes
- ✗Returning a
ТаблицаЗначенийfrom a server function to the client: it does not travel, a form attribute or an array of structures is needed - ✗Keeping the table in a form module variable for the client: client and server variables are separate
Follow-up questions
- →Which other values cannot be passed from the server to the client?
- →What does a form attribute of the «Таблица значений» type become on the client?
MiddleTheoryCommonHow do you call an object module method from a managed form?
How do you call an object module method from a managed form?
The form keeps the object as form data in Объект, not as the object itself. In a &НаСервере method, get it with РеквизитФормыВЗначение("Объект"), call the object module's export method, then put it back with ЗначениеВРеквизитФормы.
Common mistakes
- ✗Calling an object module method on
Объектdirectly:Объектis form data, not the object - ✗Forgetting
ЗначениеВРеквизитФормыafter the call: the form keeps showing the old data
Follow-up questions
- →Why must the object module's method be marked
Экспорт? - →What happens to the form's unsaved changes when
Объектis turned into an object?
SeniorTheoryCommonWhat does a server call with context cost on a form with large tables?
What does a server call with context cost on a form with large tables?
A &НаСервере call sends the form's data to the server and the changes back, so a form with thousands of table rows ships them on every button press. Calls get slow and load the network; use calls without context with only the needed values.
Common mistakes
- ✗Calling
&НаСервереfrom a button on a form with big tables just to read one value: the whole form travels - ✗Thinking the server reads the form from the database: the form data come from the client on each call
Follow-up questions
- →How do you find out which form attributes travel on a context call?
- →When is a call with context still the right choice?