Swift3.1 - NSApplication.terminate or NSApplication.shared().terminate - swift3

Creating a StatusBarMenuItem using a older style, what works fine. However because of a warning, I did want to change my code conform Swift 3.1, from
menu.addItem(NSMenuItem(title: "Quit terminate", action: Selector(("terminate:")), keyEquivalent: "q" ))
to
...
menu.addItem(NSMenuItem(title: "Quit quitAction", action: #selector(quitAction), keyEquivalent: "Q"))
...
func quitAction() {
NSApplication.shared().terminate(self) // works of course
// NSApplication.terminate(self) // doesn't works of course but works in a selector
}
Then I found out that they did change the Selector(("terminate:)) part not only to #selector but also the terminate part into
menu.addItem(NSMenuItem(title: "Quit NSAppl.terminate", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q" ))
But the following is also working ( mind the '.shared()' part )
menu.addItem(NSMenuItem(title: "Quit NSAppl.shared.terminate", action: #selector(NSApplication.shared().terminate(_:)), keyEquivalent: "q" ))
But when I want to remove the .shared() part from the quitAction function then I get a compiler error. My question is; "Why can I leave the shared() part out using a selector but not in a line of code"? What is the rationale about this? Thank you.
Actually, the question should be more like; "Why can I insert the shared() part using a selector"?
added image

A selector with syntax Type.method sends the method to an instance of the Type specified in the target parameter.
I suppose in macOS a selector without specified target is sent to FirstResponder ignoring the Type. The method terminate: is exposed to FirstResponder
In code you have to send the method to an instance so shared() is required.

Related

SHIFT-F6 as menu shortcut

In my current code I have the code
MenuItem runFileItem = new MenuItem(
(MenuItem mi) => pm.runFile(),
"_Run file",
"activate", true, accelGroup, 'r',
ModifierType.CONTROL_MASK | ModifierType.SHIFT_MASK,
AccelFlags.VISIBLE);
which works as expected - I can execute the handler pm.runFile() by pressing CTRL+SHIFT+r. Reason why I did it this way is because I could not figure out how to implement the preferred SHIFT+F6 that I wanted to use for this purpose. It would be good if I could put GDK_F6 GDK Keysym (from the gdk.Keysyms module), but could not find a way to use it. I've also asked the same question on GtkD forum (https://forum.gtkd.org/groups/GtkD/thread/2976/) without luck.
So the question is how to modify the above code to make SHIFT+F6 combination be handled properly as originally planned?
UPDATE:
Based on Adam's answer, the following code works:
MenuItem runFileItem = new MenuItem("_Run file", (MenuItem mi) => pm.runFile(), "activate");
runFileItem.addAccelerator("activate", accelGroup, GdkKeysyms.GDK_F6, ModifierType.SHIFT_MASK, AccelFlags.VISIBLE);
This constructor you're calling: https://api.gtkd.org/gtk.MenuItem.MenuItem.this.3.html
Forwards some of its arguments to this function on the base class:
https://api.gtkd.org/gtk.Widget.Widget.addAccelerator.html
Notice the ctor takes char but that other function takes uint so it should be able to take more values.
So I'd suggest trying:
auto mi = new MenuItem(label, action);
mi.addAccelerator(
"activate", accelGroup, GDK_F6,
ModifierType.SHIFT_MASK,
AccelFlags.VISIBLE
);
and see if it works.

I m inserting my data uthrough page item using request process it gives an error fetch more then one row please give me a solution

var a = $v('P1995_LUMBER');
if ((a = '1')) {
apex.submit({
request: "CREATE",
set: {
LUMBER: "P1995_LUMBER",
LST_NME: "P1995_LST_NME",
FST_NME: "P1995_FST_NME",
},
});
} else if (a != '1') {
apex.submit({
request: "Update",
set: {
LUMBER: "P1995_LUMBER",
LST_NME: "P1995_LST_NME",
FST_NME: "P1995_FST_NME",
},
});
} else {
alert("bang bang");
}
Couple of things:
JavaScript's equality check is either == or === (more details here). (a = '1') assign '1' to the variable.
It seems like you're not using the apex.submit process correctly. Typically, you would set the item's value
e.g.:
apex.page.submit({
request: "SAVE",
set: {
"P1_DEPTNO": 10,
"P1_EMPNO": 5433
}
} );
Although, by looking at your JavaScript code, I would say you don't even need to use JavaScript.
Whenever you submit a page, all items on it are automatically sent to the server-side. You can then reference them using bind variables. You could then simply have two process, one for the Create and one for the Update, each having the corresponding insert/update statement using the different items on your page.
Usually what you will see is a page with two buttons for Create/Edit. They will have a server-side condition so that only the correct one is displayed.
Try creating a Form type page (form with report) using the wizard, and you'll see how everything is done.
Without seeing the page and the code you're using it's hard to tell what your issue really is, more details would be required.
That code does not have any sql in it so it is impossible to diagnose why you are encountering a TOO_MANY_ROWS exception. Run the page in debug mode and check the debug data - it should show you what statement is throwing the exception. If you need more help, post a proper reproducible case, not a single snipped of code without any context.

Baqend onUpdate Handler

Will doing partialupdate() cause code in a data class' onUpdate Handler to run?
I have this setup in the data class:
exports.onUpdate = function(db, obj) {
DB.log.info(obj.ShiftID);
db.Shifts.load(obj.ShiftID)
.then((Shift) => {
DB.log.info(Shift);
if (Shift.User == db.User.me) {
Shift.User = null;
Shift.status = 0;
return Shift.update();
}
})
};
(yes, role 2 for node has permissions to query and update the Shifts data class)
But I am getting zero logs when I make a partialupdate(). Do I need to do a real update query...load the object, modify the data, update()?
Also it seems that this code causes the partialupdate() to not run at all, but when I delete the handler, it starts working again.
Yes, that is currently an unimplemented feature since a partial update can't execute an onUpdate handler since there is no object which can be passed to the update handler.
On the other hand, a partial update can't be executed directly since that will result in a security issue (since your onUpdate handler can contain validation code etc.)
So we currently reject any partial update on a class which has an onUpdate handler because there doesn't exist a way how we can actually validate the partial update against your onUpdate code.
We have planned that you can define an extra onPartial handler where you can take some extra steps before the partialUpdate is executed. But that handler will only get the partial update and not the object itself.
I'm pretty sure that partialupdate() will not cause the onUpdate Handler to run.
When I put the log line in and edit the records using website data manager it does log as expected. Not a big deal, I can just rewrite the query to be a full update.
BUT having any code in there does break partialupdate() which is not good.
Here is the code I'm using that works as long as there is nothing in the onUpdateHandler:
requestShift(shiftID) {
db.ready().then((db) => {
db.Applicants.find()
.where({
"shiftID": { "$in": [shiftID] },
})
.singleResult((applicants) => {
return applicants.partialUpdate()
.add("applicants", db.User.me.id)
.add("photos", this.props.UserData.photo)
.execute()
})
Alert.alert(
'Confirmation',
'Shift has been requested.',
)
this.props.navigation.dispatch(goToFindShifts)
})
}

Exact field input mongoose for nodejs

I've recently been attempting to pull my hair out getting stuck on what I would call a trivial issue.
The way mongoose in nodejs handles the specific field inputs. I have a specific issue where mongoose is not doing the same as mongodb is doing.
The issue is the following, if I use a "-" symbol in my field name, mongoose seems to run some weird operation on it, instead of accepting it as part of a string.
I've tried running several regex commands, some / escapes, however it should literally just take the input, as I know the specific data I'm looking for.
The code causing the issue is the following:
datapoints.find({type: "charging-type", device: device._id})
.exec(function(err, objects){
if(!objects){
log("Can't find objects");
}
});
Going straight into mongo shell and typing:
db.datapoints.count({type: "charging-type", device: device._id})
taking out the type makes everything work, changing the type to for example: shuntvoltage, current, ... all work perfectly.
The problem thus occurs with the usage of this - symbol.
What would be the way to enter this inputstring with a special character as an exact string?
Any help would be much appreciated.
Edit as per request; I don't get any error, I get objects==undefined (or !objects), schema is below.
var datapointSchema = mongoose.Schema({
type: { type: String, lowercase: true},
value: { type: Number},
timestamp: { type: Number},
device: { type: ObjectId, ref: "devices"}
});
module.exports = mongoose.model('datapoints', datapointSchema)
Manually updated the field in mongodb from charging-status to chargingstatus, as no useful answer was produced.
It's a workaround and should be considered unresolved.
Final code ended up looking as such:
var cursor = calcpoints.find({device: device._id}).where('type').equals('chargingstatus').sort({timestamp: 1}).cursor();

Does webstorm have some shortcut for console.log or console.info?

Just tired of typing console.log again and again, and do not find a way like Sysout + Control + Space in Eclipse will create System.out.println().
There's a predefined Postfix template that allows you to type .log after a JavaScript expression or string and hit Tab to transform it to console.log().
You can also create a Live template (see Preferences | Editor | Live templates) that would expand into a code snippet once you type the selected abbreviation and hit Tab.
Update: there's now also a plugin that allows you to add console.log with a shortcut: https://plugins.jetbrains.com/plugin/10986-console-log
Yes it does,
<anything>.log and press Tab key. This will result in console.log(<anything>);
ie,
<anything>.log + Tab => console.log(<anything>);
eg1: variable
let my_var = 'Hello, World!';
my_var.log + Tab => console.log(my_var);
eg2: string
'hello'.log + Tab => console.log('hello');
eg3: string and variable
'hello', my_var.log + Tab => console.log('hello', my_var);
[UPDATE 2020]
Typing log + Enter autocompletes to console.log()
I made my own template that seems to work.
It may be useful for somebody.
Abbreviation: ll
Template text:
console.log('$NAME$ ', $VALUE$);
$END$
Variables: (just select the given field values by clicking drop down box)
NAME - jsDefineParameter()
VALUE - jsSuggestVariableName
I'm including what I find to be the most efficient, which I added via live templates -> javascript -> applicable to "Everything". Hopefully someone finds it useful.
console.log('L$LINE$ $MYSTRING$ ===', $MYVAR$);$END$
What it does:
When I type cl and press tab, it creates the log and the first thing you type fills both MYSTRING and MYVAR variables. If you tab again, it selects MYVAR where you can rewrite/delete as desired. The third time you hit tab will take you to the end of the line at $END.
This snippet also prints the line number like L123 but you can easily remove that if it isn't helpful because obviously most browsers show line number anyway.
You also have to set the variables' behaviour as seen in the image below:
Edit variables setup
use Macros!
https://www.jetbrains.com/help/webstorm/using-macros-in-the-editor.html
I recorded a macro that takes the name my cursor is on and create
console.log("#### name = ", name);
on the next line.
and assigned a keyboard shortcut to it :)
super easy, and couldn't get Live Template to get the same result with 1 action.
to create a new macro: Edit -> Macros -> Start Macro Recording. then record your next moves and create the desired result.
this is mine:
This is my solution, it somewhat mimics a turbo-console approach and gives you certain freedoms to build on it.
Step 1: Go to Editor > General > Postfix Completion;
Step 2: Click on JavaScript, click the + button, select JavaScript and TypeScript;
Step 3: In the Key input, type a alias for your command, I choose 'cl' for mine;
Step 4: In the 'Minimum language level' select your desired preference, I choose ECMAScript 6+;
Step 5: In the bellow text area, add your logic, for me it is console.log('$EXPR$', $EXPR$, '$END$');
Step 6: Customize however you like.
So what does all of this do?
Lets consider the following:
const willNeedToBeLogged = 'some value you want to check';
All you need to do for a console long is type
willNeedToBeLogged.cl + press (Tab, Enter or Spance)
And you will get this
console.log('willNeedToBeLogged', willNeedToBeLogged, '');
With your cursor being on the $END$ variable, where you could write, a line, or anything you like.
Have fun!
I made a custom template. This can help you.
Abbreviation: clog
Template code:
console.log("\n\n--------------------------------");
console.log($END$);
console.log("--------------------------------\n\n");
Simplest live template text:
console.log($END$);
Maybe it is a recent addition but you can write log and hit tab and console.log() will appear with the caret in between the braces.
The answer from Ekaterina Prigara (https://stackoverflow.com/a/32975087/5653914) was very useful to me but if you want to log a string like "Test" this method is quicker.
Try a logit plugin. It provides the next logging pattern by default:
const data = 'data';
console.log('-> data', data);
You can configure it.
Try Dot Log (vscode extension), It can automatically transfer aaa.log to console.log('aaa', aaa )