How to handle global config without magic mode? - ractivejs

From v0.9 the "magic mode" is removed.
How should we now handle scenarios like in this sample:
Updating all the prices when global config with rates updated
http://jsfiddle.net/Inversion/7an5hdb5/
conf =
g_currency_exchange_rates:
usd:27
win.r = new Ractive
el: 'cont'
template: '#tpl'
magic:true
data:
conf:conf
items_prices_usd: [5, 10, 25]
in_uah: (v)-> v * #get 'conf.g_currency_exchange_rates.usd'
update_rate = ->
conf.g_currency_exchange_rates.usd += Math.round(Math.random()*2-1)
setInterval update_rate, 500
In the sample there is only one Ractive instance, but imagine you have several different on the page and all they use same config and should dynamically update any rate changes. And they really should be separate root instances, not nested components.
For example if you are logged in as admin — admin panel can be shown on demand, and it is like another app, but relies on the same config.

Assuming you have a reference to the instance, you can call ractive.update() after setting new data.
https://jsfiddle.net/artvhvpo/
console.clear();win=window
conf =
g_currency_exchange_rates:
usd:27
win.r = new Ractive
el: 'cont'
template: '#tpl'
magic:true
data:
conf:conf
items_prices_usd: [5, 10, 25]
in_uah: (v)-> v * #get 'conf.g_currency_exchange_rates.usd'
update_rate = ->
conf.g_currency_exchange_rates.usd += Math.round(Math.random()*2-1);win.r.update()
setInterval update_rate, 500

Related

Google Earth Engine video export error (a.element.map is not a function)

I'm trying to export a time-lapse here but got a weird error:
Error Creating or Submitting Task
a.element.map is not a function
I want to keep the visParams on my exported video by visualize() which I'm not sure is the right way to do so or not. do you have any suggestions for it?
var l8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA"),
region = ee.Geometry.Polygon(
[[[44.76385083079123, 38.28074335406828],
[44.76385083079123, 37.1334667575582],
[46.08221020579123, 37.1334667575582],
[46.08221020579123, 38.28074335406828]]], null, false),
params = {"opacity":1,"bands":["B4","B3","B2"],"min":0.07630298537191671,"max":0.3954072752450793,"gamma":1.356};
var collection = l8.filterBounds(region)
.filterMetadata('CLOUD_COVER', 'LESS_THAN', 30);
.filterDate('1999-01-01', '2020-01-01');
var l8med = collection.median();
Map.addLayer(collection, params, 'Layer');
print(collection.size());
var newimg = l8med.visualize(params);
Export.video.toDrive({
collection: newimg,
description: 'a1',
dimensions: 720,
framesPerSecond: 12,
folder: "GEE",
maxFrames: 100000,
region: region
});
You made a single image out of the collection using .median() and then tried to export that, so it can't work — there's no time series to make a video out of, after that.
You do need .visualize() but you need to do it for each image:
Export.video.toDrive({
collection: collection.map(function (image) { return image.visualize(params); }),
...

IMPORTDATA not grabbing live data from XML

I'm using Google Sheet's IMPORTDATA function to grab information from an XML file that is pulling from an API but the information I pull into the sheet isn't up to date.
How can I modify my sheet to pull in up-to-date data?
Compare the sheet: https://docs.google.com/spreadsheets/d/1W0Bt5z-Tky-tNhG_JtfE4FfjTRgQNRu_eQu2qVhQ-_E/edit?usp=sharing (LiveScores sheet)
To the XML: https://www67.myfantasyleague.com/2019/export?TYPE=liveScoring&L=64741&APIKEY=&W=14&DETAILS=1&JSON=0
Observe franchise id="0015" in both sets of data.
The sheet states <franchise id="0005" score="0.00" gameSecondsRemaining="21600" playersYetToPlay="6" playersCurrentlyPlaying="0" isHome="0">
The XML states <franchise id="0015" score="11.14" gameSecondsRemaining="20004" playersYetToPlay="4" playersCurrentlyPlaying="2"> (This data is for a football game that is currently being played as I'm writing this so the above example may not be exact, but it WON'T be score of 0.00, for example.
Any help would be amazing, thanks!
Have you tried using IMPORTXML? Google Sheets IMPORTXML Page
In IMPORTXML, you can just use the Inspect Element feature to pull the xpath.
Hope this helps. Let me know if I can help further.
Edit: Instructions To Change When Data Is Imported
In the toolbar go to the script editor
Now in the scripts, paste the code listed bellow
/**
* Go through all sheets in a spreadsheet, identify and remove all spreadsheet
* import functions, then replace them a while later. This causes a "refresh"
* of the "import" functions. For periodic refresh of these formulas, set this
* function up as a time-based trigger.
*
* Caution: Formula changes made to the spreadsheet by other scripts or users
* during the refresh period COULD BE OVERWRITTEN.
*
* From: https://stackoverflow.com/a/33875957/1677912
*/
function RefreshImports() {
var lock = LockService.getScriptLock();
if (!lock.tryLock(5000)) return; // Wait up to 5s for previous refresh to end.
// At this point, we are holding the lock.
var id = "YOUR-SHEET-ID";
var ss = SpreadsheetApp.openById(id);
var sheets = ss.getSheets();
for (var sheetNum=0; sheetNum<sheets.length; sheetNum++) {
var sheet = sheets[sheetNum];
var dataRange = sheet.getDataRange();
var formulas = dataRange.getFormulas();
var tempFormulas = [];
for (var row=0; row<formulas.length; row++) {
for (col=0; col<formulas[0].length; col++) {
// Blank all formulas containing any "import" function
// See https://regex101.com/r/bE7fJ6/2
var re = /.*[^a-z0-9]import(?:xml|data|feed|html|range)\(.*/gi;
if (formulas[row][col].search(re) !== -1 ) {
tempFormulas.push({row:row+1,
col:col+1,
formula:formulas[row][col]});
sheet.getRange(row+1, col+1).setFormula("");
}
}
}
// After a pause, replace the import functions
Utilities.sleep(5000);
for (var i=0; i<tempFormulas.length; i++) {
var cell = tempFormulas[i];
sheet.getRange( cell.row, cell.col ).setFormula(cell.formula)
}
// Done refresh; release the lock.
lock.releaseLock();
}
}
This snippet of code came from Periodically refresh IMPORTXML() spreadsheet function
Last and definitely the least, replace the "YOUR-SHEET-ID"
NOTE: I have not personally tested this code and I cannot vouch for it. I recommend making a copy and testing it there first.
Hopefully, this solves the issue of your data not being imported as often as you want. If you want to manually get "fresh" data, you can just delete/cut the import function and paste it back in.
try in A2:
=ARRAYFORMULA(IFNA(VLOOKUP(C2:C, PlayerList!A:F, {2, 6}, 0)))
and in C2:
=ARRAYFORMULA(QUERY(REGEXEXTRACT(QUERY(IMPORTDATA(
"https://www67.myfantasyleague.com/2019/export?TYPE=liveScoring&L=64741&APIKEY=&W=14&DETAILS=1&JSON=0?273"),
"where Col1 contains 'player id'", 0),
"(player id=""(\d+)).+?(score=""(\d+.\d+))"),
"select Col2,Col4"))
spreadsheet demo

How to read, change and replace data with Firebase?

I'm trying to make a gambling website simulation, and I need to have a variable that stores the user's balance. This will change when they place a bet.
The variable is called "userbalance" and it set at 500 on Firebase. The user's bet is successfully stored on firebase. Therefore, I need to restrive this bet value again, and take it away from "userbalance", therefore updating their balance.
function storewager(){
var coinref = firebase.database().ref();
var coinwager = document.getElementById("coininput").value;
var userbalance =
coinref.child("coinbet").set(coinwager);
changeuserbal();
}
function changeuserbal(coinwager, userbalance){
var balref = firebase.database().ref().child('userbalance');
var coinwager = document.getElementById("coininput").value;
var userbalchg = balref - coinwager;
window.alert(balref);
balref.child("userbalance").set(userbalchg);
}
I can't understand why it is working correctly in storewager, but not in changeuserbal.
Here's the firebase database layout:
gambling-website-simulationaddclose
coinbet: "66" <--- this is the user bet, which is changed by storewager()
userbalance: 500 <--- I'm trying to take coinbet away from this
Thank you very much for your help, as when I can do this, I should be able to progress a good amount more.
EDIT
I've now managed to send the variable value, not the name To "userbalance". However, the calculation isn't correct. I believe the issue lies with the original value of "userbalance" not being referenced correctly.
Therefore, "userbalance" is just the wager but minus.
bal - coinwager = userbalchg ---> this is uploaded to "userbalance"
0 - coinwager = userbalchg ---> This is what I think is occuring
Here's the updated piece of my code:
function changeuserbal(coinwager){
var balref = firebase.database().ref().child("userbalance");
balref.once("value")
.then(function(snapshot){
var bal = snapshot.child("userbalance").val();
var coinwager = document.getElementById("coininput").value;
var userbalchg = bal - coinwager;
balref.set(userbalchg);
});
}

Partially update a session variable in meteor?

I think that I might be missing something. I've got the entire contents of a user's product order form stored in a session variable.
var orderFormContents = {
numDoors: 4
numWheels: 4
numSeats: 5
};
Session.set("orderFormContentsSessionVar", orderFormContents);
How do I update the value of just one key in orderFormContentsSessionVar, for instance, just numDoors?
I don't want to overwrite the entire existing contents of the session var.
I would love to be able to do something like:
Session.set("orderFormContentsSessionVar.numDoors", 2);
Something equivalent to _.extend
UPDATE
Following the example of the answer below, I just wrote a function for it:
var updateSession = function(sessionVarName, updateParams){
var obj = Session.get(sessionVarName);
_.extend(obj, updateParams);
Session.set(sessionVarName, obj);
console.log("updated session name: ", sessionVarName, "new session contents: ", Session.get(sessionVarName));
};
Using like you proposed:
var obj = Session.get("orderFormContentSessionVar");
Session.set("orderFormContentsSessionVar", _.extend(obj, {numDoors: 2}));
_.extend should work just fine.
Session.set( "orderFormContentsSessionVar",
_.extend(Session.get("orderFormContentsSessionVar"), { numSeats: 10 }) )
I doubt there is any other way.

Changes to one variable propagates to another

For example I have two ArrayCollection's - firstAC and secondAC. If I do secondAC = firstAC, and than I make changes to secondAC (prehaps put a filterfunction on it) it somehow propagates to firstAC, would anyone tell me why that happens in Flex or Actionscript 3?
What can I do if I only want secondAC to get all data from firstAC but then when I make changes to secondAC it does not show in firstAC?
Thanxs a bunch for answers!
Ladislav
When you write secondAC = firstAC, you simply state that secondAC and firstAC are references to the same array collection.
What you want is to clone the first collection (as in, copy all elements one by one).
You should be able to do it with something like :
secondAC = new ArrayCollection();
secondAC.addAll(firstAC);
I have no idea of Flex or Actionscript, but looks like firstAC and secondAC point to the same array, therefore that's expected.
What you should do is just create another array, copy members, and they will be two real different entities.
Instead of secondAC = firstAC, you can try secondAC.addAll(firstAC).
In ECMAScript languages (AS1-3, JavaScript, et al.), when you use
var foo = //some value which is not a String or a Number
what you are really saying is "foo now points to the same object as that other variable." This means that in this situation, both arrays will be the same value:
var foo:Array = [ 1, 2, 3 ];
foo = bar;
bar.push( 4 );
trace( foo ); // 1, 2, 3, 4
This also works for functions:
var foo:Array = [ 1, 2, 3 ];
adder( foo );
function adder( bar:Array ):void {
bar.push( 4 );
}
trace( foo ); // 1, 2, 3, 4
and it even works with XML:
var xml:XML = <root><foo/></root>;
var bar:XML = xml;
bar.children()[ 0 ].#bar = 1;
trace( xml.toXMLString() ); // <root><foo bar="1"/></root>
This is called "passing by reference" instead of "passing by value" or "passing by copy". It means that every time that an item is referenced, each variable will point to the same object.
There are many ways to get around this, and most of them depend on your context. For arrays, my favorite is Array.concat(), which returns a literal clone of the array. This means that anything I do to the returned value will not effect the original in any way. If I'm dealing with XML, however, I will do something like: var xml2:XML = XML( xml.toXMLString() );.
In your case, I would actually recommend that you use:
var secondAC:ArrayCollection = new ArrayCollection( firstAC.source.concat() );
This has the major benefits of not only being faster (it relies on compiled code instead of Flex SDK code and it also does not first instantiate a new array and then re-populate it), but it also has the distinct benefit of being available in older versions of Flex 3's SDK -- it is entirely backwards compatible.

Resources