question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Idea for improving schema field "watch"

See original GitHub issue

Currently the “watch” functionality is rather limited when used from within schema options. (Using the on(‘watch’) from JavaScript offers much more flexibility) and the field watching is always readonly.

In the develop/2.x branch I introduced a callback list for including JavaScript function callbacks in schema options. (Originally created for 3rd-party plugins that uses callback functions as options)

The same callback list functionality could be used for “watch” fields. So instead of setting a “template” parameter, you set a “watchfunc” So if the “template” parameter matches a callback function in the callback list it will use the callback function as “template”. The callback function receives the values of the watched field (similar to templates) but can manipulate the data much more freely.

Test case:

  • Field1 is a textinput for entering firstname
  • Field2 is a radio input with 2 options (male and female) and is watching for changes in Field1

When Field1 loses focus, the watch event is triggered in Field2. this passes the value of Field1 to the “watchfunc”, which then does an API lookup at https://api.genderize.io, and sets the Field2 value based on the result. And as the field is not readonly, the user can change it manually too.

The changes needed for this to work, is very minimal since the callback list code is already present in develop/2.x

Current code in editors/string.js

    // Compile and store the template
    if(this.schema.template) {
      this.template = this.jsoneditor.compileTemplate(this.schema.template, this.template_engine);
      this.refreshValue();
    }
    else {
      this.refreshValue();
    }

After patching code in editors/string.js

    // Compile and store the template
    if(this.schema.template) {
      var callback = this.expandCallbacks('watch', {template: this.schema.template});
      if (typeof callback.template === 'function') this.template = callback.template;
      else this.template = this.jsoneditor.compileTemplate(this.schema.template, this.template_engine);
      this.refreshValue();
    }
    else {
      this.refreshValue();
    }

And a callback watch function would be defined like this (callback is named “hello”)

window.JSONEditor.defaults.callbacks.watch = {
  "hello": function(jseditor,e) {
    var watched = e.colors;
    return watched ? 'The color is ' + watched : '';
  }
};

@schmunk42 @germanbisurgi @lukzas @mirannda What do you think of this idea?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
pmk65commented, Aug 14, 2019

If there’s no objections to this change, I will implement it in the develop/2.x branch There’s only 2 editors with watch/template support. string.js (plus all that extends string.js) and hidden.js so it’s a really minor change.

IMO the use of template engines (like Mustache, Swig, Lodash etc.) is totally overkill. No one seems to really use it and just sticks to the default/build-in template engine. The only one I could get to work/figure out, was Handlebars. See demo here

(I have asked for people to post examples using the template engines, but zero response.)

0reactions
pmk65commented, Aug 14, 2019

Functionality added to develop/2.x branch

Read more comments on GitHub >

github_iconTop Results From Across the Web

10 Rules for a Better SQL Schema - Sisense
After countless hours of reading and writing queries, we've seen almost everything. Here are our top 10 rules for creating pain-free schemas.
Read more >
Schema Suggestions in MongoDB Atlas: Years of Best ...
Schema suggestions empower you to make choices that optimize for MongoDB's document data model, whether you're just starting out or already have ...
Read more >
What are Play Schemas? The Answer and 150+ Fun Schema ...
Learn about the history of play schemas, why they are important, how they align with the Montessori philosophy and get fun activity ideas....
Read more >
5 Database Design Schema Example: Critical Practices ...
This blog talks about the Database Schemas & their types, Database Design Schema Example followed by the best practices to attain high ...
Read more >
26 Transformation Schema Activities (+Definition)
Some ideas include: Planting a herb garden; Planting simple seeds such as cress and watching them grow over a period of days; Weeding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found