From 79b511614ac979ed588a8a1380f2e318cfcb4117 Mon Sep 17 00:00:00 2001 From: Dirk Grappendorf Date: Mon, 22 Jun 2015 15:31:52 +0200 Subject: [PATCH] feat: Upgrade to Polymer 1.0 --- Gruntfile.js | 69 ++++++--- README.md | 2 +- bower.json | 11 +- grapp-rest-resource.html | 241 +++++++++++++++++------------- index.html | 18 +-- package.json | 29 ++-- src/grapp-rest-resource.coffee | 131 +++++++++------- src/grapp-rest-resource.html | 9 +- test/basic.html | 8 +- example.json => test/example.json | 0 test/headers.html | 12 +- 11 files changed, 300 insertions(+), 230 deletions(-) rename example.json => test/example.json (100%) diff --git a/Gruntfile.js b/Gruntfile.js index 72e2866..4331e6b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,13 +1,5 @@ module.exports = function(grunt) { - var mapDotDotUrlToLib = function(req, res, next) { - var url_parts = req.url.split('/'); - if (url_parts.length > 2 && ['lib', 'test'].indexOf(url_parts[1]) == -1) { - req.url = '/lib' + req.url; - } - return next(); - }; - grunt.initConfig({ pkg: grunt.file.readJSON('bower.json'), @@ -44,7 +36,10 @@ module.exports = function(grunt) { }, coffeelint: { - src: ['src/*.coffee'] + src: ['src/*.coffee'], + options: { + max_line_length: { level: 'ignore' } + } }, coffee: { @@ -60,7 +55,7 @@ module.exports = function(grunt) { htmlbuild: { dist: { src: 'src/*.html', - dest: './', + dest: 'build/', options: { scripts: { dist: ['build/*.js'] @@ -75,19 +70,21 @@ module.exports = function(grunt) { } }, - connect: { - server: { - options: { - port: 8080, - base: '.', - middleware: function(connect, options, middlewares) { - middlewares.unshift(mapDotDotUrlToLib); - return middlewares; - } - } + replace: { + dist: { + src: 'build/*.html', + dest: './', + replacements: [{ + from: /href="..\/lib\/([^"]+)"/g, + to: 'href="../$1"' + }] } }, + connect: { + server: {} + }, + watch: { stylesheets: { files: 'src/*.less', @@ -127,6 +124,12 @@ module.exports = function(grunt) { changelog: { options: { } + }, + + shell: { + test: { + command: 'xvfb-run -a ./bin/grunt wct-test' + } } }); @@ -140,9 +143,33 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-conventional-changelog'); grunt.loadNpmTasks('grunt-html-build'); + grunt.loadNpmTasks('grunt-shell'); + grunt.loadNpmTasks('grunt-text-replace'); grunt.registerTask('build', 'Compile all assets and create the distribution files', - ['less', 'coffeelint', 'coffee', 'htmlbuild']); + ['less', 'coffeelint', 'coffee', 'htmlbuild', 'replace']); + + grunt.registerTask('wct-test', function() { + var + done = this.async(), + wct = require('web-component-tester'), + options = { + remote: false, + persistent: false, + root: '.', + plugins: { + local: { + browsers: ['chrome'] + } + } + }; + wct.test(options, function() { + done(); + process.exit(0); + }); + }); + + grunt.registerTask('test', 'Test the web application', ['shell:test']); grunt.task.renameTask('bump', 'bumpversion'); diff --git a/README.md b/README.md index 68b38f0..9eb19a7 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ grapp-rest-resource A web component that encapsulates REST API calls. -Attributes +Properties ---------- * **url** diff --git a/bower.json b/bower.json index d283875..786228e 100644 --- a/bower.json +++ b/bower.json @@ -20,7 +20,6 @@ "keywords": [ "web-components", "polymer", - "coyoho", "grappendorf" ], "authors": [ @@ -32,12 +31,12 @@ "url": "git://github.com/grappendorf/grapp-rest-resource.git" }, "dependencies": { - "polymer": "Polymer/polymer#~0.5.1", - "core-ajax": "Polymer/core-ajax#~0.5.1" + "polymer": "Polymer/polymer#~1.0.0", + "iron-ajax": "PolymerElements/iron-ajax#~1.0.0" }, "devDependencies": { - "coffee-script": "latest", - "jquery": "latest", - "web-component-tester": "Polymer/web-component-tester#2.2.6" + "web-component-tester": "Polymer/web-component-tester#3.1.7", + "jquery": "2.1.4", + "coffee-script": "1.9.3" } } diff --git a/grapp-rest-resource.html b/grapp-rest-resource.html index 6b85d7a..c1d5c3e 100644 --- a/grapp-rest-resource.html +++ b/grapp-rest-resource.html @@ -9,14 +9,13 @@ --> - + - + @@ -51,156 +50,188 @@ return url; }; - Polymer('grapp-rest-resource', { - created: function() { + Polymer({ + is: 'grapp-rest-resource', + properties: { + url: { + type: String + }, + params: { + type: String + }, + resource: { + type: Object + }, + indexUrl: { + type: String + }, + showUrl: { + type: String + }, + newUrl: { + type: String + }, + createUrl: { + type: String + }, + updateUrl: { + type: String + }, + destroyUrl: { + type: String + }, + memberUrl: { + type: String + }, + headers: { + type: Object, + value: {} + }, + token: { + type: String + } + }, + ready: function() { var self; - this.headers = {}; self = this; return this.resource = { index: function(success, error) { - return self.$.xhr.request({ + return self.$.request.send({ url: prepareUrl(self.indexUrl || self.url, self.params), - headers: self.prepareHeaders(), - callback: function(data, response) { - var json; - json = (data.trim() !== '' ? JSON.parse(data) : {}); - switch (response.status) { - case 200: - return typeof success === "function" ? success(json) : void 0; - case 401: - return self.fire('grapp-authentication-error'); - default: - return typeof error === "function" ? error(json) : void 0; - } + headers: self.prepareHeaders() + }).then(function(request) { + var json; + json = (request.response.trim() !== '' ? JSON.parse(request.response) : {}); + switch (request.xhr.status) { + case 200: + return typeof success === "function" ? success(json) : void 0; + case 401: + return self.fire('grapp-authentication-error'); + default: + return typeof error === "function" ? error(json) : void 0; } }); }, show: function(id, success, error) { - return self.$.xhr.request({ + return self.$.request.send({ url: prepareUrl(self.showUrl || self.url, self.params, id), - headers: self.prepareHeaders(), - callback: function(data, response) { - var json; - json = (data.trim() !== '' ? JSON.parse(data) : {}); - switch (response.status) { - case 200: - return typeof success === "function" ? success(json) : void 0; - case 401: - return self.fire('grapp-authentication-error'); - default: - return typeof error === "function" ? error(json) : void 0; - } + headers: self.prepareHeaders() + }).then(function(request) { + var json; + json = (request.response.trim() !== '' ? JSON.parse(request.response) : {}); + switch (request.xhr.status) { + case 200: + return typeof success === "function" ? success(json) : void 0; + case 401: + return self.fire('grapp-authentication-error'); + default: + return typeof error === "function" ? error(json) : void 0; } }); }, "new": function(success, error) { - return self.$.xhr.request({ + return self.$.request.send({ url: prepareUrl(self.newUrl || self.url, self.params, null, 'new'), - headers: self.prepareHeaders(), - callback: function(data, response) { - var json; - json = (data.trim() !== '' ? JSON.parse(data) : {}); - switch (response.status) { - case 200: - return typeof success === "function" ? success(json) : void 0; - case 401: - return self.fire('grapp-authentication-error'); - default: - return typeof error === "function" ? error(json) : void 0; - } + headers: self.prepareHeaders() + }).then(function(request) { + var json; + json = (request.response.trim() !== '' ? JSON.parse(request.response) : {}); + switch (request.xhr.status) { + case 200: + return typeof success === "function" ? success(json) : void 0; + case 401: + return self.fire('grapp-authentication-error'); + default: + return typeof error === "function" ? error(json) : void 0; } }); }, create: function(data, success, error) { - return self.$.xhr.request({ + return self.$.request.send({ method: 'POST', url: prepareUrl(self.createUrl || self.url, self.params), headers: self.prepareHeaders(), - body: JSON.stringify(data), - callback: function(data, response) { - var json; - json = (data.trim() !== '' ? JSON.parse(data) : {}); - switch (response.status) { - case 200: - case 201: - case 204: - return typeof success === "function" ? success(json) : void 0; - case 401: - return self.fire('grapp-authentication-error'); - default: - return typeof error === "function" ? error(json) : void 0; - } + body: JSON.stringify(data) + }).then(function(request) { + var json; + json = (request.response.trim() !== '' ? JSON.parse(request.response) : {}); + switch (request.xhr.status) { + case 200: + case 201: + case 204: + return typeof success === "function" ? success(json) : void 0; + case 401: + return self.fire('grapp-authentication-error'); + default: + return typeof error === "function" ? error(json) : void 0; } }); }, update: function(id, data, success, error) { - return self.$.xhr.request({ + return self.$.request.send({ method: 'PUT', url: prepareUrl(self.updateUrl || self.url, self.params, id), headers: self.prepareHeaders(), - body: JSON.stringify(data), - callback: function(data, response) { - var json; - json = (data.trim() !== '' ? JSON.parse(data) : {}); - switch (response.status) { - case 200: - case 201: - case 204: - return typeof success === "function" ? success(json) : void 0; - case 401: - return self.fire('grapp-authentication-error'); - default: - return typeof error === "function" ? error(json) : void 0; - } + body: JSON.stringify(data) + }).then(function(request) { + var json; + json = (request.response.trim() !== '' ? JSON.parse(request.response) : {}); + switch (request.xhr.status) { + case 200: + case 201: + case 204: + return typeof success === "function" ? success(json) : void 0; + case 401: + return self.fire('grapp-authentication-error'); + default: + return typeof error === "function" ? error(json) : void 0; } }); }, destroy: function(id, success, error) { - return self.$.xhr.request({ + return self.$.request.send({ method: 'DELETE', url: prepareUrl(self.destroyUrl || self.url, self.params, id), - headers: self.prepareHeaders(), - callback: function(data, response) { - var json; - json = (data.trim() !== '' ? JSON.parse(data) : {}); - switch (response.status) { - case 200: - return typeof success === "function" ? success(json) : void 0; - case 401: - return self.fire('grapp-authentication-error'); - default: - return typeof error === "function" ? error(json) : void 0; - } + headers: self.prepareHeaders() + }).then(function(request) { + var json; + json = (request.response.trim() !== '' ? JSON.parse(request.response) : {}); + switch (request.xhr.status) { + case 200: + return typeof success === "function" ? success(json) : void 0; + case 401: + return self.fire('grapp-authentication-error'); + default: + return typeof error === "function" ? error(json) : void 0; } }); }, memberAction: function(id, action, success, error) { - return self.$.xhr.request({ + return self.$.request.send({ method: 'PUT', url: prepareUrl(self.memberUrl || self.url, self.params, id, action), - headers: self.prepareHeaders(), - callback: function(data, response) { - switch (response.status) { - case 200: - return typeof success === "function" ? success() : void 0; - case 401: - return self.fire('grapp-authentication-error'); - default: - return typeof error === "function" ? error() : void 0; - } + headers: self.prepareHeaders() + }).then(function(request) { + switch (request.xhr.status) { + case 200: + return typeof success === "function" ? success() : void 0; + case 401: + return self.fire('grapp-authentication-error'); + default: + return typeof error === "function" ? error() : void 0; } }); } }; }, prepareHeaders: function() { - var h, key, val, _ref; + var h, key, ref, val; h = { 'Accept': 'application/json' }; - _ref = this.headers; - for (key in _ref) { - val = _ref[key]; + ref = this.headers; + for (key in ref) { + val = ref[key]; h[key] = val; } if (this.token) { @@ -213,4 +244,4 @@ }).call(this); - + diff --git a/index.html b/index.html index 90d4d5c..d6d1a84 100644 --- a/index.html +++ b/index.html @@ -4,9 +4,8 @@ grapp-rest-resource web component - - - + + @@ -28,21 +27,22 @@

A web component that encapsulates REST API calls

Demo

- +