i just installed Coffeescript and tried a test compile but it always drop me errors for silly things, Coffeescript only compile correctly when only Coffeescript syntax is used?
because if yes then i understand the error.
concDev.js contents:
/*! ProjectName 2013-08-18 06:08:39 */
$(function() {
// Avoid `console` errors in browsers that lack a console.
(function() {
var method;
var noop = function () {};
var methods = [
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
'timeStamp', 'trace', 'warn'
];
var length = methods.length;
var console = (window.console = window.console || {});
while (length--) {
method = methods[length];
// Only stub undefined methods.
if (!console[method]) {
console[method] = noop;
}
}
}());
});
// New file
$(function() {
// Handler for .ready() called.
});
You can't use C style comments in Coffeescript.
This
/*! Project Name ...*/
should be this
# Project Name ...
More generally, if you're using the Coffeescript compiler, you need valid coffeescript syntax and can't mix and match JS and coffeescript files.
Update
You're trying to pass a JS file to a coffeescript compiler. The coffee compiler accepts a coffeescript file, and compiles it to a JS file. Your file in Coffeescript would look something like this:
#! ProjectName 2013-08-18 06:08:39 */
$ ->
# Avoid `console` errors in browsers that lack a console.
do ->
noop = -> null
methods = [
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
'timeStamp', 'trace', 'warn'
]
length = methods.length
console = window.console = window.console || {}
while length--
method = methods[length]
# Only stub undefined methods.
if !console[method]
console[method] = noop;
// New file
$ ->
#Handler for .ready() called.
If you really need to have some JS in your coffeescript file, you can embed it using backticks like this
a = `(function() x{ return 2;})()`
Related
VSC's jsx formatting looks weird.
If there's no param in useCallback it looks fine, but there's new line when there's param. And also there's new line in dependency array when useCallback method has params.
I want to change this as in the case where there are no parameters.
Any Ideas?
const onSubmitEditing = useCallback(
({nativeEvent}) => {
setSearchQuery(nativeEvent.text);
},
[setSearchQuery],
);
const clearText = useCallback(() => {
setSearchBoxText('');
}, [setSearchBoxText]);
This is my .prettierrc.js.
And I use 'Prettier - Code formatter' as default formatter.
module.exports = {
bracketSpacing: false,
jsxBracketSameLine: true,
singleQuote: true,
trailingComma: 'all',
};
I'm trying to add a custom filter to an entity in my ApiPlatform project that allows me to filter on specific property given a regex pattern.
Following the ApiPlatform documentation I came up with the following class (this is a near copy of the their example, only the where-clause is different):
<?php
namespace App\Filter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
final class RegexFilter extends AbstractContextAwareFilter
{
protected function filterProperty(
string $property,
$value,
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
string $operationName = null
) {
// otherwise filter is applied to order and page as well
if (
!$this->isPropertyEnabled($property, $resourceClass) ||
!$this->isPropertyMapped($property, $resourceClass)
) {
return;
}
$parameterName = $queryNameGenerator->generateParameterName($property); // Generate a unique parameter name to avoid collisions with other filters
$queryBuilder
->andWhere(sprintf('(o.%s ~ :%s) = true', $property, $parameterName))
->setParameter($parameterName, $value);
}
// This function is only used to hook in documentation generators (supported by Swagger and Hydra)
public function getDescription(string $resourceClass): array
{
if (!$this->properties) {
return [];
}
$description = [];
foreach ($this->properties as $property => $strategy) {
$description["regexp_$property"] = [
'property' => $property,
'type' => 'string',
'required' => false,
'swagger' => [
'description' => 'Filter using a regex. This will appear in the Swagger documentation!',
'name' => 'Custom name to use in the Swagger documentation',
'type' => 'Will appear below the name in the Swagger documentation',
],
];
}
return $description;
}
}
When I run my code this results in the following DQL:
SELECT o FROM App\Entity\Vehicle o WHERE (o.licensePlate ~ :licensePlate_p1) = true ORDER BY o.id ASC
However I cannot get the Lexer to understand the tilde ~ character:
Doctrine\ORM\Query\QueryException
[Syntax Error] line 0, col 56: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '~'
How can I make Doctrine understand the tilde?
Turns out I was pretty close, simPod's answer helped me fill the gaps.
I copied his custom DQL function and added it in my Doctrine yaml configuration
In my RegexFilter I had to slightly modify the where-clause:
->andWhere(sprintf('REGEX(o.%s, :%s) = true', $property, $parameterName))
I'm trying to cleanup 2 collections before each test. I'm using mocha --watch to rerun tests while editing the test source files. First run always executes as expected, but consecutive runs gives Topology was destroyed error from mongodb(indicated via result of http request).
I am not really sure why deleteMany deletes my inserted object in consecutive runs.
describe('myCollection1 related tests', () => {
// myCollection1 documents should refer to a valid myCollection2 document.
var foo;
const exampleObject = {name: 'TEST OBJECT', attr1: 'TO'};
beforeEach(() => {
return Promise.all([
mongo.db('mydb').collection('myCollection1').deleteMany({}), // clear collection 1
mongo.db('mydb').collection('myCollection2').deleteMany({}) // clear collection 2
.then(() => mongo.db('mydb').collection('myCollection2').insertOne(exampleObject) // and add a sample object
.then((value) => {
foo = value.ops[0]; // save this as test specific variable so I can use it in my tests.
return Promise.resolve();
})),
]);
});
it('should create a related object', (done) => {
chai.request(server)
.post('/api/v1/foos/')
.send({ related: foo._id })
.then((res) => {
res.should.have.status(200);
res.body.should.be.an('object').with.all.keys('status', 'errors', 'data');
done();
}).catch((err) => {
done(err);
});
});
});
I spotted issue with your promise structure in beforeEach. I'm not sure it is intended or not. I'm afraid it is the culprit. I'm fixing that into below:
beforeEach(() => {
return Promise.all([
mongo.db('mydb').collection('myCollection1').deleteMany({}),
mongo.db('mydb').collection('myCollection2').deleteMany({})
]) // close the promise.all here
.then(() => collections.engines().insertOne(exampleObject)) // close `then` here
.then((value) => {
foo = value.ops[0];
return Promise.resolve();
});
});
Hope it helps
I have a module that looks like this:
module.exports = AtomMarkdownLabels =
# other stuff here
file_changed_added: (file_path) =>
fs.readFile file_path, 'utf-8', #process_yaml
console.log 'file changed'
process_yaml: (err, data) =>
console.log "process_yaml is called"
I know file_changed_added is being called from some other function and I'm seeing the "file changed" output in the console, but process_yaml isn't if I change file_changed_added to
file_changed_added: (file_path) =>
fs.readFile file_path, 'utf-8', (err, data) =>
console.log "test"
console.log 'file changed'
I see both "test" and "file changed" being called properly. What could be going on?
=> has two slightly different purposes:
When defining a named function (f = => ...) or anonymous function f(x, => ...)), => simply ensures that # inside the function is that same as # in the surrounding context.
When defining a method in a class:
class C
m: => ...
=> ensures that # inside m will be the instance of C.
Both uses are creating a bound function but they're binding to different things.
You're using this structure:
obj =
func: =>
# ...
That's equivalent to this:
f = =>
# ...
obj =
func: f
because you're using a plain old object rather than a class. So what is # outside your AtomMarkdownLabels definition? # won't be anything useful and in particular, it won't be your AtomMarkdownLabels object and it won't have a process_yaml property so #process_yaml inside file_changed_added is going to be undefined or an error.
I'm not sure what specifically Atom wants you to return but a class should work, something like this:
# Use a class so that => does what you're expecting it to do
class AtomMarkdownLabels
# other stuff here
file_changed_added: (file_path) =>
fs.readFile file_path, 'utf-8', #process_yaml
console.log 'file changed'
# you may or may not need => here
process_yaml: (err, data) =>
console.log "process_yaml is called"
# Export an instance of your class
module.exports = new AtomMarkdownLabels
If you want to or must use a plain object then you could bypass # completely and do it like this:
# Just plain old functions so define them that way
process_yaml = (err, data) ->
console.log "process_yaml is called"
file_changed_added = (file_path) ->
fs.readFile file_path, 'utf-8', process_yaml
console.log 'file changed'
module.exports = AtomMarkdownLabels =
# other stuff here
file_changed_added: file_changed_added
or like this:
# Explicitly name your object rather than using #
module.exports = AtomMarkdownLabels =
# other stuff here
file_changed_added: (file_path) ->
fs.readFile file_path, 'utf-8', AtomMarkdownLabels.process_yaml
console.log 'file changed'
process_yaml: (err, data) ->
console.log "process_yaml is called"
This should solve your other CoffeeScript problem from a few days ago too.
I have a simple ajaxLink to a controller. It works without the params, but when I add a param I get an error:
Warning: Missing argument 1 for Ez_ContactsController::testyAction()
The params are required and I see them in the jquery that ZF creates. here is the JS that gets generated (/ez is my module):
$(document).ready(function() {
$('a.ajaxLink1').click(function() { $.post('/ez/contacts/testy', {"param1":"1","param2":"456"}, function(data, textStatus) { $('#testy').html(data); }, 'html');return false; });
});
Here is my ajaxLink:
<div id="testy"></div>
<?= $this->ajaxLink("Example 2",
"/ez/contacts/testy",
array('update' => '#testy',
'class' => 'someLink'),
array('param1' => '1',
'param2' => '456')); ?>
Thanks for any help
I found the answer.
You don't send the vars directly to the function, they are sent via GET or POST.
Here is how I got the vars.
$param1 = (int)$this->_request->getParam('param1');
$param2 = (int)$this->_request->getParam('param2');
Here is the entire function:
public function testyAction(){
//The request was made with JS XmlHttpRequest
$this->_helper->viewRenderer->setNoRender(); //Will not render view
$this->_helper->Layout->disableLayout(); // Will not load the layout
$param1 = (int)$this->_request->getParam('param1');
$param2 = (int)$this->_request->getParam('param2');
echo 'testy 123. params sent were: '.$param1.', '.$param2;
}