Category: How do I do X?
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

Is it possible to use 1 query object bound to different controls with different results?

Issue

As a developer, I need to bind different text boxes to a single query, with only the filter parameter changing. However, I am struggling to do so without creating multiple query objects, which would be cumbersome with many text boxes. Currently, all the text boxes are displaying the same data, but I want them to dynamically display data based on the query assigned to them.

Resolution

The problem is how to bind different text boxes to one query but with different filter parameters. One solution is to create a single query object and trigger it from each widget that needs it. Another solution is to store the response of the query in a centralized store and then for each text box, assign it to a particular property in the store. This way, when a button is clicked, the query runs and its response is stored in the appropriate property in the store. The text box is then bound to the value of the property in the store, thus dynamically assigning the query to the text box. Here is an example code snippet:

const store = { 
txt1: "",
txt2: "",
txt3: ""
};

export default {
txt1Bind: () => {
return JSON.stringify(store.txt1, null, 4);
},
txt2Bind: () => {
return JSON.stringify(store.txt2, null, 4);
},
txt3Bind: () => {
return JSON.stringify(store.txt3, null, 4);
},
runQuery: (genreName, textBoxName) => {
const query = {
"find": "movies",
"filter": { "genres.name": "{{this.params.genreName}}" },
"sort": { "_id": 1 },
"limit": 10
};
Query1.run({genreName: genreName}, {
onSuccess: (response) => {
store[textBoxName] = response[0];
}
});
},
button1_onClick: () => {
runQuery("Thriller", "txt1");
},
button2_onClick: () => {
runQuery("Comedy", "txt2");
},
button3_onClick: () => {
runQuery("Action", "txt3");
}
}

In this example, the runQuery() function takes in the filter parameter and the name of the text box that needs to be updated. Inside the function, the query is run with the filter parameter, and upon success, the response value is stored in the appropriate property in the store object. The text box binding functions then return the value of the appropriate property in the store. The button click events trigger the runQuery() function with the appropriate filter parameter and text box name.