osalabs / osafw-php

Business Applications Web Framework, PHP
MIT License
3 stars 3 forks source link

Best practice: JS vars default values for unset PS #10

Closed vladsavchuk closed 5 years ago

vladsavchuk commented 6 years ago

Consider the following code in onload.js:

var Config = {
  maxSmsChars: <~js[maxSmsChars]>,
  maxEmailChars: <~js[maxEmailChars]>,
  commsSmsRefreshStatusUrl: '<~/comms/sms/url>' + '/(RefreshSmsStatus)/',
  manageBookingsUrl: '<~/manage/bookings/url>'
}

$PS['js'] array only exists for edit action (existing entity with id), i.e. if it's not new. So, if I request new action the $PS['js'] is empty and the browser throw JS-syntax error.

What is the best way to deal with this?

Option 1:

var Config = {
  maxSmsChars: <~js[maxSmsChars]><~un_js unless="js[maxSmsChars]" inline>1</~un_js>,
  maxEmailChars: <~js[maxEmailChars]><~un_js2 unless="js[maxEmailChars]" inline>1</~un_js2>,
  commsSmsRefreshStatusUrl: '<~/comms/sms/url>' + '/(RefreshSmsStatus)/',
  manageBookingsUrl: '<~/manage/bookings/url>'
}

Option 2:

var Config = {
  <if_id if="id" inline>
  maxSmsChars: <~js[maxSmsChars]>,
  maxEmailChars: <~js[maxEmailChars]>,
  </if_id>
  commsSmsRefreshStatusUrl: '<~/comms/sms/url>' + '/(RefreshSmsStatus)/',
  manageBookingsUrl: '<~/manage/bookings/url>'
}

Changes in the controller are undesirable for various reasons.

We need a good standard regarding exchanging data between back-end Controller and front-end Client. Probably using some common object in PS with initialization to setup default values.

At the moment it feels strange to me of using ParsePage tags in JS-files ... These tags break syntax highlighting and create linter errors in IDE.

osalabs commented 5 years ago

with recent ParsePage updates you could use:

$Config=<~config json noescape>;

or if you prefer not to have ParsePage tags directly in code and not to break syntax highlighting:

$Config=JSON.parse('<~config json noescape>');