From c599f50579b3c9569bc396c0eedc3218a02ae9f8 Mon Sep 17 00:00:00 2001 From: Raphael Cazenave-Leveque Date: Tue, 17 Feb 2015 19:24:25 +0100 Subject: [PATCH 1/2] experimental --- bower.json | 3 +-- package.json | 3 ++- public/js/main.js | 55 ++++++++++++++++++++++++++++------------------- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/bower.json b/bower.json index 19078c7..24e755d 100644 --- a/bower.json +++ b/bower.json @@ -16,7 +16,6 @@ ], "dependencies": { "requirejs": "~2.1.16", - "rdf-rest-js": "~0.9.1", - "jsonld": "~0.3.20" + "rdfrestjs": "0.9.3" } } diff --git a/package.json b/package.json index 94c1d2b..bec9ccb 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "dependencies": { "express": "~4.11.2", "mongoose": "~3.8.22", - "body-parser": "~1.11.0" + "body-parser": "~1.11.0", + "rdfrest": "0.9.3" }, "repository": { "type": "git", diff --git a/public/js/main.js b/public/js/main.js index e20f079..33caa89 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -1,32 +1,43 @@ /*eslint-env amd, browser*/ -/*eslint no-console:0*/ require.config({ paths: { - jsonld: "../components/jsonld/js/jsonld" + rdfrestjs: "../components/rdfrestjs/browserified/rdfrest-bundle" } }); -define(["jsonld"], function(jsonld) { +define(["rdfrestjs"], function(rdfrestjs) { "use strict"; - var doc = { - "http://schema.org/name": "Manu Sporny", - "http://schema.org/url": {"@id": "http://manu.sporny.org/"}, - "http://schema.org/image": {"@id": "http://manu.sporny.org/images/manu.png"} - }; - var context = { - "name": "http://schema.org/name", - "homepage": {"@id": "http://schema.org/url", "@type": "@id"}, - "image": {"@id": "http://schema.org/image", "@type": "@id"} - }; - - // compact a document according to a particular context - // see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form - jsonld.compact(doc, context, function(err, compacted) { - var para = document.createElement("p"); - var node = document.createTextNode(JSON.stringify(compacted, null, 2)); - para.appendChild(node); - document.body.appendChild(para); + var getCore = rdfrestjs.coreFactory.getCore; + var iri = rdfrestjs.rdfNode.iri; + var namespace = rdfrestjs.rdfNode.namespace; + var nt = rdfrestjs.serializerNTriples.nt; + + var me = iri("http://champin.net"); + var ns = namespace("http://ex.co/vocab#"); + var ressource = getCore(me); + + ressource.getState().then(function(g) { + + // 'cause we cannot use console.log as a function in a browser + //noinspection Eslint + return nt(g, console.info.bind(console)); + + }).then(function() { + + // then we edit the graph + return ressource.edit(function(g) { + return g.addTriple(me, ns("type"), ns("Person")); + }); + }).then(function() { + return ressource.edit(function(g2) { + return g2.addTriple(me, ns("label"), "Pierre-Antoine Champin"); + }); + }).then(function(g) { + //noinspection Eslint + console.log("----\n"); + //noinspection Eslint + return nt(g, console.info.bind(console)); }); -}); \ No newline at end of file +}); From 131364ce4fccaf644b0ce75e334a20631f1b52b7 Mon Sep 17 00:00:00 2001 From: Raphael Cazenave-Leveque Date: Wed, 18 Feb 2015 15:15:52 +0100 Subject: [PATCH 2/2] cors issue --- package.json | 3 ++- public/js/main.js | 3 ++- public/js/ng-user-controller.js | 2 +- server.js | 10 ++++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index bec9ccb..e01148a 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,10 @@ "name": "Hydra-RDF-REST-bugtracker", "main": "server.js", "dependencies": { + "body-parser": "~1.11.0", + "cors": "^2.5.3", "express": "~4.11.2", "mongoose": "~3.8.22", - "body-parser": "~1.11.0", "rdfrest": "0.9.3" }, "repository": { diff --git a/public/js/main.js b/public/js/main.js index 33caa89..4a937c5 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -16,7 +16,8 @@ define(["rdfrestjs"], function(rdfrestjs) { var me = iri("http://champin.net"); var ns = namespace("http://ex.co/vocab#"); - var ressource = getCore(me); + + var ressource = getCore("http://localhost:8080/api/user/54da8d9d0f7c387641739640"); ressource.getState().then(function(g) { diff --git a/public/js/ng-user-controller.js b/public/js/ng-user-controller.js index 3a58176..deb615b 100644 --- a/public/js/ng-user-controller.js +++ b/public/js/ng-user-controller.js @@ -15,7 +15,7 @@ function UserController($scope) { console.log($scope.newPredicate); console.log($scope.newObject); - var ressource = coreFactory.getCore("http://localhost:8080/api/user/54d357ea2f6af8e974000001"); + var ressource = coreFactory.getCore("http://localhost:8080/api/user/54da8d9d0f7c387641739640"); ressource.edit(function(graph) { return graph.addTriple(rdfNode.iri($scope.newSubject), rdfNode.iri($scope.newPredicate), $scope.newObject) .then(function() { diff --git a/server.js b/server.js index 2f1c7ce..3eb7075 100644 --- a/server.js +++ b/server.js @@ -7,6 +7,7 @@ var path = require("path"); var bodyParser = require("body-parser"); var fs = require("fs"); var userToJsonLD = require("./util/userToJsonld").toJsonLD; +var cors = require('cors'); // MODEL var User = require("./app/models/user"); @@ -23,6 +24,13 @@ var urlServer = "http://vps.schrodingerscat.ovh"; var urlAPI = "/api"; var urlUser = "/user"; +// CORS +var corsOptions = { + origin: 'http://localhost:63342', + credentials: 'true' +}; +app.use(cors(corsOptions)); + // MONGODB CONFIG var mongoDatabaseName = "bugtracker"; @@ -104,6 +112,8 @@ router.route(urlUser + "/:userId") } else { user = userToJsonLD(user, urlServer, urlAPI, urlUser); + res.set('Access-Control-Allow-Credentials', 'true'); + res.set("Content-Type", "application/ld+json"); res.json(user); }