How to draw a link from an existing port in JointJS? - jointjs

Im trying to figure out how to draw a link from an existing port in an element to another element in JointJS.
For example using the following code won't work as I want to specify the source as a specific port in the element not the element itself:
var link = new joint.shapes.devs.Link({
source: {
id: elementFrom.Id,
port: "out"
},
target: {
id: elementTo.id,
port: "in"
}
});
this.graph.addCell(link);
I there a way to achieve this?

I found out that this can be done by providing the id of the port in the source port propery. Like this:
//add a port
var port = {
id: "my_port_id"
};
var sourceElement = this.findElement(my_element_id);
sourceElement.addPort(port);
sourceElement.portProp("my_port_id", "attrs/circle/fill", "#f2f2f2");
sourceElement.portProp("my_port_id", "attrs/label/position/name", "outsideOriented");
sourceElement.portProp("my_port_id", "attrs/text/text", "some port label");
//add link
var link = new joint.dia.Link({
source: {
id: sourceElement.Id,
port: "my_port_id"
},
target: {
id: targetElement.id
}
});
this.graph.addCell(link);

Related

Graphql query is always returning null

I am trying to get course data using graphql, but server is always returning null as a response This is my code in file server.js :
var express=require('express');
const { graphqlHTTP } = require('express-graphql')
var {buildSchema}=require('graphql');
//graphql schema
var schema=buildSchema(`
type Query {
course(id: Int!): Course
courses(topic:String!):[Course]
}
type Course {
id: Int
title: String
author: String
description:String
topic:String
url: String
}
`);
var coursesData = [
{
id: 1,
title: 'The Complete Node.js Developer Course',
author: 'Andrew Mead, Rob Percival',
description: 'Learn Node.js by building real-world applications with Node, Express, MongoDB, Mocha, and more!',
topic: 'Node.js',
url: 'https://codingthesmartway.com/courses/nodejs/'
},
{
id: 2,
title: 'Node.js, Express & MongoDB Dev to Deployment',
author: 'Brad Traversy',
description: 'Learn by example building & deploying real-world Node.js applications from absolute scratch',
topic: 'Node.js',
url: 'https://codingthesmartway.com/courses/nodejs-express-mongodb/'
},
{
id: 3,
title: 'JavaScript: Understanding The Weird Parts',
author: 'Anthony Alicea',
description: 'An advanced JavaScript course for everyone! Scope, closures, prototypes, this, build your own framework, and more.',
topic: 'JavaScript',
url: 'https://codingthesmartway.com/courses/understand-javascript/'
}
]
//root resolver
var root= {
course:getCourse,
courses:getCourses
};
var getCourse= function (args){
var id=args.id;
console.log("delila")
return coursesData.filter(course=>{
return course.id==id
})[0]
}
var getCourses = function(args){
if(args.topic){
var topic=args.topic;
return coursesData.filter(course=>
course.topic===topic
);
}
else return coursesData
}
//create an experess server and graphql endpoint
var app=express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue:root,
graphiql:true
}));
app.listen(4000,()=>console.log("delila express graphql server running on localhost 4000"))
When I go to localhost:4000/graphql to get data I am using
query getSingleCourse($courseID: Int !){
course(id:$courseID){
title
author
description
url
topic
}
}
{
"courseID": 3
}
But I am constantly getting result null. Look at image
Anyone idea why is happening this? Server should return course with id 3 but obviously there is something that I am missing
You should define function expression first and then use them. That's the reason.
Function expressions in JavaScript are not hoisted, unlike function declarations. You can't use function expressions before you create them:
See Function expression
E.g.
//...
var getCourse = function (args) {
var id = args.id;
console.log('delila');
return coursesData.filter((course) => {
return course.id == id;
})[0];
};
var getCourses = function (args) {
if (args.topic) {
var topic = args.topic;
return coursesData.filter((course) => course.topic === topic);
} else return coursesData;
};
//root resolver
var root = {
course: getCourse,
courses: getCourses,
};
//...

Get the latest email from an email address using imap-simple in node

I want to get the latest email from the email address using imap. I was using searchCriteria 1:2 but I am getting oldest 2 emails, I also used 'NEW' and 'RECENT' but that is also not working. How can I fetch latest emails of my inbox. Here is my code:
var imaps = require('imap-simple');
var config = {
imap: {
user: 'xxxx#gmail.com',
password: 'xxxxx',
host: 'imap.gmail.com',
port: 993,
tls: true,
authTimeout: 3000
}
};
imaps.connect(config).then(function (connection) {
return connection.openBox('INBOX').then(function () {
var searchCriteria = [
'UNSEEN'
];
var fetchOptions =
{ bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)'], struct: true }
return connection.search(searchCriteria, fetchOptions).then(function (messages) {
messages.forEach(function (message) {
console.log(message.parts[0].body);
});
});
});
});

Ember Data breaks when fetching JSON from Node + Express REST server

I am trying to fetch JSON data from REST server built with Node.js and Express and then use it as a model in my Ember#Route.
The data I am trying to fetch:
var books = [
{ id: 98, author: 'Stanisław Lem', title: 'Solaris' },
{ id: 99, author: 'Andrzej Sapkowski', title: 'Wiedźmin' }
];
The model I use:
App.Book = DS.Model.extend({
id: DS.attr('number'),
author: DS.attr('string'),
title: DS.attr('string')
});
I set up the RESTAdapter this way:
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:8080'
});
Mapping:
App.Router.map(function () {
this.resource("books");
});
My Route looks like this:
App.BooksRoute = Ember.Route.extend({
model: function () {
return this.store.find('book');
}
});
I am aware that ember-data follows certain convention when it comes to JSON files.
My server provides JSONs this way:
app.get('/books', function (request, response) {
console.log('In GET function ');
response.json({'books': books})
});
Then, after entering
http://localhost:8080/books
I get
{"books":[{"id":98,"author":"Stanisław Lem","title":"Solaris"},{"id":99,"author":"Andrzej Sapkowski","title":"Wiedźmin"}]}
but when I enter
http://localhost:8080/#/books
ember-data throws long error list that begins with:
"Error while processing route: books" "invalid 'in' operand record._attributes"
"ember$data$lib$system$model$attributes$$getValue#http://localhost:8080/static/ember-data.js:8176:1
ember$data$lib$system$model$attributes$$attr/<#http://localhost:8080/static/ember-data.js:8202:26
computedPropertySet#http://localhost:8080/static/ember.prod.js:11882:15
computedPropertySetWithSuspend#http://localhost:8080/static/ember.prod.js:11842:9
makeCtor/Class#http://localhost:8080/static/ember.prod.js:33887:17
...
and now I don't know what is wrong and how to fix this.
Looks like the mistake I made was in declaring model. ID attribute shouldn't be declared here, correct model looks like this:
App.Book = DS.Model.extend({
author: DS.attr('string'),
title: DS.attr('string')
});

Retrieve roster in node-xmpp

I'm having trouble understanding how to retrieve an XMPP roster (and eventually the presence state of each contact) in node-xmpp (GTalk account).
My example code can login and connect, but I'm a bit lost as to what to send and listen for:
var xmpp = require('node-xmpp')
jid = 'example#gmail.com'
password = 'xxxxxxxxxxxxxx'
// Establish a connection
var conn = new xmpp.Client({
jid: jid,
password: password,
host: 'talk.google.com',
port: 5222
})
conn.on('online', function() {
console.log('ONLINE')
var roster = new xmpp.Element('iq', {
type: 'get',
from: jid,
id: new Date().getTime()
}).c('query', { xmlns: 'jabber:iq:roster' })
conn.send(roster) // Now what?
})
conn.on('error', function(e) {
console.log(e)
})
Looks like the structure of my roster query was wrong, this works correctly:
conn.on('online', function() {
console.log('ONLINE')
var roster = new xmpp.Element('iq', {
id: 'roster_0',
type: 'get'
}).c('query', {
xmlns: 'jabber:iq:roster'
})
conn.send(roster)
})

mongoJS parsing JSON output

I'm playing with nodeJS and mongoJS. I've successfully queried the DB and returned a set of data:
var databaseUrl = "redirect"; // "username:password#example.com/mydb"
var collections = ["things", "reports"]
var db = require("mongojs").connect(databaseUrl, collections);
db.things.find({url: "google.com"}, function(err, things) {
if( err || !things) {
console.log("URL not Found");
}
else things.forEach( function(RedirectURL) {
console.log(RedirectURL);
} );
});
This returns this:
{ _id: 4fb2c304f2dc05fe12e8c428,
url: 'google.com',
redirect:
[ { url: 'www.goolge.com', weight: 10 },
{ url: 'www.google.co.uk', weight: 20 } ] }
What I need to do now is select certain elements of the array such as redirect.weight or redirect.url.
Does mongoJS allow me to do this easily or is there a better way?
Any pointers greatly appreciated as ever!
Ric
MongoJS implements the mongo API. Calls to document contents use the dot notation.
So in the example above,
var weight = RedirectURL.redirect.0.weight
// equal to 10, since it is the first item of the redirect array.
The same principle can be used with the find commands. So if you wish to find documents with weight = 10, use the following command
db.things.find({redirect.weight: 10})

Resources