"Failed to compute" error because of unexpected operation inside #if - ractivejs

Here is the problem (Ractive.js v0.7.3): http://jsfiddle.net/Inversion/hLym2xcy/
r = new Ractive
el: 'cont'
template: '''
{{#task.options}}
{{#if pros}}
inside if
{{#each filter(.pros)}}<li class='{{set_class(.)}}'>+{{.v}}</li>{{/each}}
{{/if}}
{{/task.options}}
'''
data:
task:
options: [{pros:[{v:1}]}]
set_class: (o)-> o.v
filter: (arr)-> console.log(arr);arr || []
setTimeout(
-> r.set('task.options.0.pros', undefined)
2000
)
On r.set operation "inside if" is not printed out as expected, but #each tries to work with an undefined array which is not expected.
I cannot guarantee presence of an pros array and that's why I used {{#if pros}} to do not use it, but seems like it doesn't work.
console.log(arr) is added to see if filter is actually called, and it is.
Also when I try to return [] from the filter it still calls set_class which is unexpected because there is nothing to iterate.
Is there a way to overcome these unexpected problems?
Thanks!

Seems like it is a known problem and will be fixed in v0.8

Related

XQuery: declare a function returning nothing

declare variable $testseq as item()* := ();
declare function local:insertseq($target as item()*, $position as xs:integer?, $inserts as item()*)
as item()* (:might be great if we have a keyword to represent nothing:)
{
fn:insert-before($target, 1, $inserts) (:change the global sequence:)
() (:simulate returning nothing, empty sequence:)
};
element test
{
attribute haha {"&"},
local:insertseq($testseq, 1, ('a', 'b')),
$testseq
}
I need to collect something into a global sequence while the script running. At the end of the script I release the sequence. The function insertseq must return nothing. It is possible with XQuery? Or are there other tricks to do it?
Error from BaseX:
$ basex test.xqy
Stopped at /Users/jack/Documents/SHK/XSD2OWL/Workspace/xqy/test.xqy, 7/4:
[XPTY0004] Item expected, sequence found: ("a", "b").
The answer on the title of your original question would actually be:
declare function local:f() as empty-sequence() {
()
};
As you probably want to solve a specific problem, you could think about creating a new question with another title and a corresponding problem description (including a tiny example with the expected input and output).
In functional languages, such as XQuery, variables cannot be reassigned once they have been defined (see Referential Transparency). As a consequence, you’ll need to use recursive functions to repeatedly add values to a sequence. fn:fold-left can be used as well: it feels challenging when being used for the first time, but once you understand what it does, you don’t want to miss is.

Jade Template in Meteor - Conditional Comparison Syntax Error

Trying to compare two template variables is resulting in an error. This seems consistent with jade syntax. What's wrong here?
Code:
if name == current_project.name
| Current
else
a.nav-project-selection Set as Current
Error:
While building the application:
client/templates/account.jade: Jade syntax error: Expected identifier, number, string, boolean, or null
{{#if name == current_project.na...
^
SOLVED
Since Jade compiles down to Spacebars which doesn't support comparison, you have to do it via a helper function. The following worked for me:
client.js
UI.registerHelper('equals', function(a, b) {
return (a === b);
});
template
if equals name current_project.name
| Current
else
a.nav-project-selection Set as Current
An easier way is to use the Meteor-handlebar-helpers package.
meteor add raix:handlebar-helpers
Check the readme file to get an overview of the helpers. In your case:
if $eq name current_project.name
| Current

user defined function for xquery if else condition

My code is working fine if I don't use function.
declare function local:samlefamily(
$pers as xs:string?) as xs:string?
{
some stuff
};
if (exists(/*/FamilyRec/Child/Link[#Ref = "IN0099"]))
then
for $family in doc("family.xml") /*/FamilyRec
where $family/Child/Link[#Ref = "IN0099"]
return if (local:samlefamily(data($family/HusbFath/Link/#Ref))) then local:samlefamily(data($family/HusbFath/Link/#Ref)) else "na1"
else "na2"
But problem occurs when I tried to define function
declare function local:giveParent($pers as xs:string) as xs:string
{if (exists(/*/FamilyRec/Child/Link[#Ref = $pers]))
then
for $family in doc("gedcom.xml") /*/FamilyRec
where $family/Child/Link[#Ref = $pers]
return if (local:samlefamily(data($family/HusbFath/Link/#Ref))) then local:samlefamily(data($family/HusbFath/Link/#Ref)) else "na1"
else "na2"
};
I am getting error:
Description: Unexpected token "< eof >" in path expression
It is not occurring for passing parameter($pers). Even I use static ("IN0099") parameter to it, same error I am getting.
Can anyone please help? Can't I use If ... else in user defined function? Advanced thanks for your support
You need to have at least one non-function statement in the xquery file, i.e. add anything after the last semicolon
Otherwise your code looks fine

Handlebars doesn't render boolean variables when false

Handlebars.js has some weird behavior. It renders a boolean with a value of true as the string "true", but a value of false as "".
var booleanTestTrue = true;
var booleanTestFalse = false;
Template:
True: {{booleanTestTrue}}
False: {{booleanTestFalse}}
Renders to:
True: true
False: (empty string)
Is there any way to fix this problem? Or do I have to write a helper?
You can use a simple block helper and implement #if like the following:
{{#if isTrue}}
true
{{else}}
false
{{/if}}
If you want to print a string, you should pass a string.
false.toString();
Otherwise, yeah you would need a helper (unless you use the #if|#unless helpers to output a string).
On a side note, if you wanted to print these values for debugging purpose, use the {{log booleanTestFalse}}.
I was surprised by this, and ended up writing a simple helper for it:
Handlebars.registerHelper( 'toString', function returnToString( x ){
return ( x === void 0 ) ? 'undefined' : x.toString();
} );
You would then invoke it as follows:
True: {{toString booleanTestTrue}}
False: {{toString booleanTestFalse}}
In most scenarios you could get away with simply return x.toString(). The extra checking avoids attempting to call toString on undefined values.
I used this, similar to Barney's answer but supports null too.
Handlebars.registerHelper('toString', function (v) {
return '' + v;
});
You would then invoke it as follows:
True: {{toString booleanTestTrue}}
False: {{toString booleanTestFalse}}
However if v is an object with a pretty toString method you need to do more coding.

xquery - expected return found let

I get this error from Saxon,
Engine name: Saxon-PE XQuery 9.2.1.2
Severity: fatal
Description: XQuery syntax error in #... (:return :) let $#: expected "return", found "let"
Start location: 776:0
on this function
declare function local:set-internet-type($req1 as element(ns0:req), $cate as element()) as xs:string {
if(count( for $itm in $req/*:cust/*:inter/*:itm
where $789/*:product/*:030/*:specs/*:name/text()= data($11/INTERNET)
and $22/*:action/text()="CHANGE"
return $33)>0) then
(
for $44 in $55
where $tt/*:name/text()= data($t/INTERNET)
and $u/*:action/text()="CHANGE"
(:return <fake/>:)
let $z:= $a/*:product/*:c/*:e[1]
return concat($x,'>',$y)
) else ("")
};
I am new with xquery and I spent a lot on this error without getting a solution. Vars were masked intentionally but from the error message seems something related to the function flow.
Any help is appreciated.
Thanks in advance
Alessandro
Saxon only declares to have "partial support of XQuery 1.1". Therefore, I guess, it supports the old FLWOR format in which you cannot use let after where. Just try to swap these clauses:
for $44 in $55
let $z:= $a/*:product/*:c/*:e[1]
where $tt/*:name/text()= data($t/INTERNET)
and $u/*:action/text()="CHANGE"
return concat($x,'>',$y)

Resources