Data Binding in Livecode

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Hans-Helmut
Posts: 57
Joined: Sat Jan 14, 2017 6:44 pm

Data Binding in Livecode

Post by Hans-Helmut » Sat Feb 04, 2017 8:49 pm

Still being a beginner to Livecode, I have lots of questions. In another thread, I was asking for the possibility of developing a "Livecode Excel Light". I am not actually striving to develop another Excel, but to me, it was and still is an exercise to think about what could possibly all be done using Livecode. My intention is to develop applications with a very rich user interface primarily focused on Windows platforms (the actual market) and Android (not leaving out iOS, Mac, and Linux).

I am still exploring the possibilities. I very much like Livecode. It deserves some industrial strength well-known applications that speak for this language. I believe that Livecode should have the power and potential to do the same or better than any Javascript application (with HTML5, CSS3) or any native Window application written in whatever language - with the exception of system specific and low-level functions.

Ultimately, is must create a very rich and pleasant user experience.

And thanks all for those very kind replies and your knowledge. Currently, I am still only good in asking questions ).


Actual data binding question


One new question is how to best accomplish what is called "data binding". This means that, for example, in a bi-directional binding any change in the data source would reflect in a user-level object, a field or button or list, and any changes in the field would reflect in the data source.

Let us assume the data source is SQLite and a field formatted as table field is representing the data.

The user would enter data into the field, and the change would automatically reflect in changing the associated column value or values in the database. This seems to be easy to do programmatically. But what happens the other way around? From outside, someone would change the database field, and now - usually using a database trigger - this change on the database level will automatically reflect in the user level field.

Now, let us assume there are all kinds of data, including sound, images, videos, text lists, etc. Leaving data in the data source untouched, just retrieving it, on the user interface data would be displayed according to Livecode controls available or created (LC objects, now widgets).

But already we might use an intermediary function to create different data views through grouping and sorting and filtering before presenting. We could even store the original data as different views as properties of the card, or in global variables, or intermediary text files... or create them at runtime. (This does not mean that a new query would to possibly better to achieve the same result, but I am trying to explain for demonstration purpose only.)

Then, on the presentation layer, we may want to present dates and numbers formatted in user- or culture-specific ways using localization functions: number formatting, color coding, date- and time formatting, etc.

Livecode keeps track of many events which would trigger updating the database whenever any change appears on the user level.


Yet, I do not know how to make a Livecode stack aware of changes that appear in a data source such as SQLite (or MySQL or whatever).


Would there be some kind of a function frequently checking for any changes that might have occurred? Or how can Livecode be notified from outside itself (or from an outside database, or even from an outside text file)? Could that be a service running separately checking for changes all the time? Could there be another second LC instance doing this?

And what would be best practice to build a framework around that would support more quickly building the components of such a system and would just require faceless stack files (script-only stacks) and keep all the logic completely distinct from the presentation layer?

I see a scenaratio such as:

======================================================
DATASOURCE <-> VIEWS <-> LOCALIZATION <-> PRESENTATION
======================================================

PRESENTATION ./. LOGIC ./. DATA ----> kept separate from each-other as much as possible.

One database query can result in many views, and a selected view would be formatted to represent localized data.

Let us take a simple field as an example. I would user custom properties to define the way how this field displays data, for example, decimal numbers with thousand separators. Even the decimal point or comma would be displayed according to such local setting. This means that such field would turn into a number field. Another field would be a date field and so on.

The field could have the same name as the column in the associated database source. Therefore, it would automatically know from where to fetch data. Another property would define which table it is associated with.

Through some trigger (messages caught) mechanism, the field would present the data of currently selected value of a column (field) in the database. The user would be able to move through all the records, or he would simply use another query. The field "knows" from where to take data and how to present it. If the user changes data in the field, also automatically the value in the associated record in the database will be updated (or deleted or created).

But then again, how would the field know about changes in the database and automatically reflect the latest value?

---

Maybe then also a mechanism could be established whereby specific field types would automatically be created when the user selects a database field. If defined to contain numerical values, the Livecode field would be numerical as well using local formatting and be bound to the database field.

Or would there be an intermediary array where we bind the field to the array and the array to the database?

And/or is there a possibility to define a field which would also have the same name (binding it to the same database field as well) but would present another type of formatting, or a calculated value based on the field value where the calculation is dynamically updating when the original value is updating? Since two fields with the same name are not a good solution, the database name may be better reflected as a custom property of the field. The id on the other side would not survive when changing the stack, when rebuilding user interface items dynamically, etc. Or there would have to be a mapping table mapping whatever current ids are present with the names and other specifications of a control.

---

I hope my questions are not too far fetched... ). I am thinking aloud without wishing to be annoying doing that. :?

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Data Binding in Livecode

Post by AxWald » Mon Feb 06, 2017 9:54 pm

Hi,
very short, it has been a long day - I'm just in the progress to code a quite similar thingie:
A program that heavily relies on database data is used "on the road". In case 3G/4G fails it must still provide useful data. To accomplish this the most relevant data are replicated from the remote MySQL to a local SQLite. The program regularly tests connectivity and, if there's none, switches to the local db. It also renews the the "cached" data in the background (well, more or less ...) in regular intervals, as long as there's IN. I'll post about it in the "databases" section as soon as the first part is ready to be shown in public ;-)
Hans-Helmut wrote:But then again, how would the field know about changes in the database and automatically reflect the latest value?
Nothing really easy coming to mind. :\
My usual strategy is to only show the smallest possible amounts of data, and to query often.

More later, too tired. Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Data Binding in Livecode

Post by Mikey » Tue Feb 07, 2017 3:04 pm

first post got to be much too long right at the point where the data binding explanation started (really?), but yes, to both of you, all of what you are doing is done in any number of apps that have been in production for years.

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Data Binding in Livecode

Post by MaxV » Wed Feb 08, 2017 2:51 pm

In mobile world there is the Google Cloud Messaging and Apple equivalent. They are already dedicate to advice your app that some data is changed and to reload them.
On the desktop there is the standard MySQL style: every n seconds connect, fetch just the data of the visible part of the screen, update the screen.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Hans-Helmut
Posts: 57
Joined: Sat Jan 14, 2017 6:44 pm

Re: Data Binding in Livecode

Post by Hans-Helmut » Thu Feb 09, 2017 1:42 pm

I would like to thank everyone who replied. To get really deeply acquainted with Livecode is something that is not done in a day.

To simplify the question. Even if I understand how to create and maintain a database using LiveCode - and even I had some success with PHP and MySQL -, I can not yet understand an automatized way to update entries in either the local field representing a database field, or vice versa, updating a database field which would reflect in the presentation layer.

Of course, I can run a timer that will check each second if there was any change in the database. But since Livecode is not multi-threaded, this would alway kind of block the normal flow of working with an application. Or not? If the overhead doing this is small then it might be done. I have not been able yet to test such scenario. And mainly, my question is to ask for BEST PRACTICE and not doing the same mistakes other may have done in all the years before.

Maybe I am still missing some fundamental understanding.

The way I understand "data binding" means a little bit more than updating a field acting upon either events on the user level, or events of the database and updating the representation of data in such field (or whatever control).

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Data Binding in Livecode

Post by Mikey » Thu Feb 09, 2017 3:19 pm

Are you connecting to a remote server, or to a local db? If you're using SQlite, then I assume you're connecting to a local db, and the app you are writing is the only user at any given time. If that is the case, then there is no syncing to do. You simply pull the record you want when you want it, display the data in the relevant controls, then update the db when you want.

If you're connecting to a remote db then you have to get your arms around all of the issues related to connecting to a remote DB.

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Data Binding in Livecode

Post by MaxV » Fri Feb 10, 2017 9:26 am

The commercial versions of livecode have a tsNet library: http://livecode.wikia.com/wiki/TsNet
unfortunately is totally undocumented, so I can't help you more on tsNet
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Data Binding in Livecode

Post by FourthWorld » Fri Feb 10, 2017 5:36 pm

MaxV wrote:The commercial versions of livecode have a tsNet library: http://livecode.wikia.com/wiki/TsNet
unfortunately is totally undocumented, so I can't help you more on tsNet
How would tsNet help with data binding?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Hans-Helmut
Posts: 57
Joined: Sat Jan 14, 2017 6:44 pm

Re: Data Binding in Livecode

Post by Hans-Helmut » Fri Feb 10, 2017 9:19 pm

Richard: Unfortunately, tsNet I have not been exploring yet. It is a matter of license and time. For this test period, I am still using the non-commercial version of Livecode. But probably will soon buy an Indy license.

Partially, I still think that I have not expressed myself well enough regarding data binding. It is a concept used in almost all commercial applications. Let us assume the simplest form of data: A simple field for one value from a column in a database table - let it be SQLite.

What is data binding?

On the user level, and in development mode, the control (the "field" in LC terms) would be selected and automatically a lookup table would pop up with the names of the connected data source (in this case SQLite). The user would select a column of the data source, and that is all the user has to do. Automatically the field would display one record of within the selected column of the selected table of SQLite.

Now, the field would update the selected record also automatically when there was any change. NO SCRIPT NEEDED. It would also provide a method to delete the value, not just the visible text in the field but also the value in the column of the database. And creating a new value would create a new value in the corresponding column as well.

I am thinking of a general connector that sits between the data source and such field. The user may decide to use data from different databases, from text files, from JSON, from arrays loaded, or whatever data source there may be, locally or remotely.)

So, data binding here means to allow connecting a field with any available data source without having to script the basic create, update, delete and presentation method.

And referencing such bound field through scripting would automatically reference the record of the underlying data source (record of a column of SQLite, for example).

This makes it much easier to build commercial applications. It may not be appropriate for each and every situation, but in general, any Livecode control such as fields, lists, DG, images in groups, etc. could also be a control with such data binding capability.

Probably, this might be something for widgets being bound controls rather than unbound controls as we have them now.

If someone ever used Microsoft Access or Apples Filemaker, the concept becomes clear immediately. But data binding is used in most other languages to connect the user interface to underlying data sources (abstracted as classes and data sources).

If there were the possibility to use Livecode Builder creating such controls allowing to enter and display data then probably they could be turned into "bound controls".I have not been able to really understand Livecode Builder at all yet (except the very basic concept).

And - as said in previous replies to this thread - data binding can also take with it the possibility of filtering and sorting as well as displaying data the way it should be displayed depending on context (for example language, culture, etc.). And this would JUST BE SETTING A PROPERTY rather than writing lots of scripts around it.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Data Binding in Livecode

Post by FourthWorld » Fri Feb 10, 2017 10:31 pm

No worries about tsNet. It has some useful features for specialized apps, but I can't conceive of anything you'd want to do regarding binding that would depend on it. If your binding needs to work with remote data, you'll probably want to use a REST API over HTTP for that, and LC's built-in support for HTTP is more than sufficient.

As for data binding, I'm familiar with it. It's been rather a hobby of mine over the years to study various implementations. Lately I've been staying up late reading Apple's Core Data docs.

Any good binding system will, as you suggest, be property-driven - point-and-click. But since LC doesn't have a data binding system built-in (for reasons probably too broad to get into; people use data in such a wide range of ways in LC), any such system will be implemented in script.

I have one in progress, but it's not ready for use.

There may be others available in our community, perhaps well suited for remote DBs even.

Have you seen Trevor's SQL Yoga?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

gagsoft
Posts: 168
Joined: Sat Jun 29, 2013 7:56 pm

Re: Data Binding in Livecode

Post by gagsoft » Thu Sep 14, 2017 11:22 am

Am am. just testing the waters for now but does SQLYoga MySQL and Windows SQLServer?

Peter G

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Data Binding in Livecode

Post by MaxV » Thu Sep 14, 2017 1:18 pm

Data binding = custom properties :?:
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Data Binding in Livecode

Post by FourthWorld » Thu Sep 14, 2017 9:49 pm

Data binding = a controller or similar mechanism to bind UI objects used for display and input to a data store.

https://en.wikipedia.org/wiki/Data_binding
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”