Hide certain enum values from users - javascript

I am trying to create a dropdown-list where you can select current employees from. However, in order to not invalidate old data, the names of old employees should also be valid entrances for the field. I am new to alpacajs, however i was thinking about doing something like this:
.alpaca({
"data": "Coffee2",
"options": {
"label": "Ice cream",
"optionLabels": ["", "Chocolate", "Coffee"] //Here i would want e.g. that Vanilla was hidden from the user.
},
"schema": {
"required": true,
"enum": ["Vanilla", "Chocolate", "Coffee"]
}
});
This solution however would then make it so you would not see the former employees names on previous data but only " ". I have also thought about doing it with observables, however i cannot get it to work.

You can use de element "disallow": ['property'], to remove the elements from the list

Related

Build json objects based on existing json-object-templates in nodejs

I would like to generate some requests based on an exisitng json-file:
It looks like this:
[
{ "SearchIndex": "Wine",
"Title": "Wine",
"MinimumPrice": 5000,
"MaximumPrice": 1000000,
"KeyWords": "Red Wine, White Wine, Bordeaux"
},
{
"SearchIndex": "Wireless",
"Title": "Wireless",
"MinimumPrice": 100,
"MaximumPrice": 1000000,
"KeyWords": "Wifi, Cisco, Router"
}
]
My problem is that I would like to generate a huge amount of requests based on only a few request-template by modifying this:
I want to generate new Minimum/MaximumPrices based on the given range
by incrementing by 500. f.e.: new Object = {...,'MinimumPrice':
5000, 'MaximumPrice': 5500,...}
I want to do the same thing at the same time with "Keywords" meaning:
Extracting each Keyword and then re-writing the values: If I got 3
Keywords, 3-times more Query-Objects would result.
Both modifications should work together/in a row.
Could you please give me an implementation guideline - I ask not for the code - I want to do it myself; but I am stuck...
The result could either be in an array or written into another json-file.
Thank you!
Hucho

AngularJS JSON Check Object

I am currently using Angular to read in a json file and output it onto a table.
Because some of the objects are a little different, I want to make a check to see if job.text exists.
[
{
"job": {
"href": "www.google.com",
"text": "Google"
},
"api": "Some Text Here"
},
{
"job": "Yahoo",
"api": "More text here"
}
]
If job.text exists, then display job.text
else display job
Here is my html from angular but only displaying objects with job.text, otherwise it returns nothing.Is there a simple way to write a check statement to make sure I can display both types of objects?
<td><a ng-href="{{item.job.href}}" target="_blank">{{item.job.text}}</a></td>
Use a ternary:
{{item.job.text ? item.job.text : item.job}}
You should try and normalize your data struct a bit - seems odd that job may contain an object or a simple text field.

Need an algorithm to convert string to JSON with javascript

I'm calling an api for the history of an ID which returns a string object that looks like this:
09304790130000--09304790090000
09304790130000--09304790120000
09304790090000--09304790010000
09304790120000--09304790020000
09304790120000--09304790030000
09304790120000--09304790110000
09304790110000--09304790050000
09304790010000--042322003
09304790020000--042322002
09304790030000--042322001
09304790050000--042322004
I could do so much more with it if I could figure out how to use JavaScript to convert it to JSON so it would look like this:
{
"name": "09304790130000",
"children": [{
"name": "09304790090000",
"children": [{
"name": "09304790010000",
"children": [{
"name": "04 2322-003"
}]
}]
}, {
"name": "09304790120000",
"children": [{
"name": "09304790020000",
"children": [{
"name": "04 2322-002"
}]
}, {
"name": "09304790030000",
"children": [{
"name": "04 2322-001"
}]
}, {
"name": "09304790110000",
"children": [{
"name": "09304790050000",
"children": [{
"name": "04 2322-004"
}]
}]
}]
}]
}
Is there an algorithm I can use that can construct the object I need regardless of how complicated the "tree" becomes?
EDIT for clarity:
The "--" in the string represents the relationship of the ID's. The left ID is the parent of the ID right of the dashes. So the ID that I feed the api, "09304790130000" has two children, each could have more children until they reach the current 9-digit ID.
What you have here is an input that is in a custom format. What you need to handle it is a regular expression. (Although your format might be simple enough that a full on regex function is not required so much as splitting on separators?) You need to do is break the input string up and loop over the components and put those into your desired data structure (which sounds like it would be some kind of tree). The high level pseudo-code would be something like:
Take line of input.
Break on "--".
Create root node from the left side if the tree is empty, otherwise just find the existing node.
Add child from right side to the parent.
Getting it into the JSON format you want may require also writing a function that iterates over the tree and writes a string in that format... although if you are using existing libraries and data types this probably already exists.
EDIT: To expand on the last bit, to get the format you want would mean a Pre-order traversal of the tree. At each step you just add the formatting and name to the JSON String. One of these libraries should have the capabilities you need, although obviously you can write a tree data structure and traversal function yourself if you need to.

Binding and accessing nested elements using Rivets.js and Backbone deep model

We are using an open source FormBuilder client side component and extending it to fit our requirements. Formbuilder is written using Backbone Deep model with nested data and for binding, it use Rivets.js.
Here Formbuilder is on GitHub: https://github.com/dobtco/formbuilder and here backbone deep model at GitHub: https://github.com/powmedia/backbone-deep-model
Now we are using nested elements in view, which are nested in structure as in following JSON:
{
"fields": [{
"label": "Untitled",
"field_type": "checkboxes",
"required": true,
"field_options": {
"options": [{
"label": "test",
"checked": false
}, {
"label": "",
"checked": false
}]
},
"rules_data": {
"rules": [{
"ruleId": "rule6",
"criterias": [{
"condition": "if",
"responseTo": "",
"userOption": ""
}],
"branchTo": [{
"branch": "test"
}, {
"branch": ""
}, {
"branch": ""
}]
}]
},
"cid": "c2"
}]
}
Here there is array of rules, then rules have at every index have more data with in which one is branchTo, now branchTo is also an indexed array. In Rivets.js we can bind something using Rivets.js . or : operator. In case of properties, we can use : but we are unable to access elements inside nested indexed array.
So is it possible to access and bind elements in Rivets while using nexted indexed elements? If yes, then how can we do so? Or is there better and simpler way to accomplish same goal? I am beginner in Backbone as well as Rivets, and I am not sure if this is the right way.
If I understand rivetsjs correctly the : is just an example of an adapter you could have ^ as an adapter separator if you wish. This means you can also have both and nest adapters. having the : search the first level and then the ^ to search 1 level deeper.
You can also build a more adaptive adapter that can get objects deeper. Example in the following stackoverflow answer. You can also see some other methods of getting deeper nested objects here:
How to bind deeper than one level with rivets.js
Hope this solves your problems

Couchdb view to selecting products using another key

I' ve a big list of products (they are ink cartidges and toner) stored in a couchdb, for every document i've got several fields, and one particular field called "models" that is a multidimensional array like this:
"models": {
"Brother": {
"HL": [
"5200",
"5240",
"5240 DN",
"5240 DNLT",
"5240 L",
"5250 DN",
"5250 DNHY",
"5250 DNLT",
"5270 DN",
"5270 DN 2 LT",
"5270 DNLT",
"5280 DW",
"5280 DWLT"
]
},
"": {
"MFC": [
"8460 DN",
"8460 N",
"8860 DN",
"8860 N",
"8870 DW"
],
"DCP": [
"8060",
"8065 DN"
]
},
"Lenovo": {
"": [
"LJ 3500",
"LJ 3550",
"M 7750 N"
]
}
},
I've to do several things with this data, starting with fixing a little problem that i've got while they was written, if you look at the example that i've posted, the second and the third brother serie has an empty array string instead of the "brand" value that should be "Brother"... i've many records that miss the "brand" key for certain series, and this should be set to the previous serie brand, i.e. the "MFC" serie should has the top level key set to "Brother" that is the key of the previous serie.
I can do this using a view in couchdb?
After doing that i need to obtain a list of unique models using another view with every product associated to them.. in other words i've to select all of my products using the last level of the multidimensional array "models" as key (including also brand and serie values as secondary result for futher sorting and filtering) and all the products that contain that certain model as value.
An output like this:
key: "Brother, HL, 5200" - value: "id_product1, id_product2, idproduct3, etc."
Before i start to reading all documentation present on the earth can someone explain me if this thin is at least doable?
Thanks in advance...

Categories

Resources