diff --git a/README.md b/README.md index 92e676f..24e417d 100644 --- a/README.md +++ b/README.md @@ -37,17 +37,40 @@ Enable to have Drupal as Backend in a Domain backend.com and the Backbone/Marion ##### Drupal 8 -Because the mode https://www.drupal.org/project/cors doesn't have a version for Drupal 8 yet I recommend use Apache 2 and enable CORS using .htaccess using the following command +Because the mode https://www.drupal.org/project/cors doesn't have a version for Drupal 8 yet and Drupal Core still doesn't have a solution for that I did a patch for .htacces to enable CORS request using jQuery documented in issue # https://www.drupal.org/node/1869548#comment-9120317 ``` -Header set Access-Control-Allow-Origin "*" +diff --git a/.htaccess b/.htaccess +index c32b182..b0bf563 100644 +--- a/.htaccess ++++ b/.htaccess +@@ -118,6 +118,10 @@ DirectoryIndex index.php index.html index.htm + RewriteCond %{REQUEST_URI} !core + RewriteRule ^ %1/core/%2 [L,QSA,R=301] + ++ # Intercept OPTIONS calls ++ RewriteCond %{REQUEST_METHOD} OPTIONS ++ RewriteRule .* / [R=200,L] ++ + # Pass all requests not referring directly to files in the filesystem to + # index.php. + RewriteCond %{REQUEST_FILENAME} !-f +@@ -165,3 +169,7 @@ DirectoryIndex index.php index.html index.htm + + + ++ ++Header always set Access-Control-Allow-Origin "*" ++Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, PATCH, DELETE" ++Header always set Access-Control-Allow-Headers: Authorization ``` +When issues https://www.drupal.org/node/1869548 and https://www.drupal.org/node/2237231 get resolved this implementation will be updated. More information at http://enable-cors.org/server_apache.html ##### Drupal 7 -In your Drupal Server you must setup HTTP Access Control to enable connection, below and example. +In your Drupal Server you must setup HTTP Access Control to enable connection, below an example. ```` Access-Control-Allow-Credentials:true @@ -87,12 +110,14 @@ var Property = Backbone.Drupal.Models.Node.extend({ ### Usage +Check **test/index.html** for Drupal 8 example and **indexd7.html** for Drupal 7 example. + ```` diff --git a/backbone.drupal.js b/backbone.drupal.js index 10fbdab..129625d 100644 --- a/backbone.drupal.js +++ b/backbone.drupal.js @@ -24,7 +24,7 @@ Backbone.Drupal.Auth = (function(Backbone, $, _){ // Set defaults. These are the only attributes allowed. function defaults() { - return { crossDomain: false }; + return { crossDomain: false, drupal8: true }; } // Set attributes @@ -53,37 +53,64 @@ Backbone.Drupal.Auth = (function(Backbone, $, _){ var status = false; var restEndpoint = Backbone.Drupal.restEndpoint.root + (Backbone.Drupal.restEndpoint.root.charAt(Backbone.Drupal.restEndpoint.root.length - 1) === '/' ? '' : '/'); - jQuery.ajax({ - async: false, - url : restEndpoint + 'user/login' + Backbone.Drupal.restEndpoint.dataType, - type : 'post', - data : 'username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password), - //dataType : 'json', - error: function(XMLHttpRequest, textStatus, errorThrown) { - return false; - }, - success : function(data) { - - var settings = { - beforeSend: function (request) { - request.setRequestHeader("X-CSRF-Token", data.token); - } - }; + if(attributes.drupal8) { + // Prepare further calls to use Basic Auth againt drupal 8 and set a cookie + var settings = { + beforeSend: function (request) { + request.setRequestHeader ('Authorization', 'Basic ' + btoa(username + ':' + password)); + //request.setRequestHeader ('Accept', 'application/json'); + request.setRequestHeader ( "Content-type", "application/x-www-form-urlencoded" ); + } + }; - if(attributes.crossDomain) { - settings.xhrFields = { - withCredentials: true + if(attributes.crossDomain) { + /*settings.xhrFields = { + withCredentials: true + };*/ + + //settings.crossDomain = true; + } + + // Define specific parametres to be used in all future request. + $.ajaxSetup(settings); + + console.log(btoa(username + ':' + password)); + status=true; + } + else if(attributes.drupal8 === false) { + // Call user/login end point to get CSR token an use in following calls + jQuery.ajax({ + async: false, + url : restEndpoint + 'user/login' + Backbone.Drupal.restEndpoint.dataType, + type : 'post', + data : 'username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password), + //dataType : 'json', + error: function(XMLHttpRequest, textStatus, errorThrown) { + return false; + }, + success : function(data) { + + var settings = { + beforeSend: function (request) { + request.setRequestHeader("X-CSRF-Token", data.token); + } }; - settings.crossDomain = true; - } + if(attributes.crossDomain) { + settings.xhrFields = { + withCredentials: true + }; + + settings.crossDomain = true; + } - // Define specific parametres to be used in all future request. - $.ajaxSetup(settings); + // Define specific parametres to be used in all future request. + $.ajaxSetup(settings); - status=true; - } - }); + status=true; + } + }); + } return status; }.bind(this), @@ -110,7 +137,6 @@ Backbone.Drupal.Auth = (function(Backbone, $, _){ return Auth; })(Backbone, jQuery, _); - Backbone.Drupal.Models = {}; @@ -197,8 +223,14 @@ Backbone.Drupal.Models.Base = Backbone.Model.extend({ var base = restEndpoint + this.urlSource; if (this.isNew()) { return base; } - // Add .json for format here. - return base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.get(this.idAttribute)) + Backbone.Drupal.restEndpoint.dataType; + var url_endpoint = base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.get(this.idAttribute)); + + if(Backbone.Drupal.restEndpoint.drupal8 == false) { + // Add .json for format here. + url_endpoint = url_endpoint + urlBackbone.Drupal.restEndpoint.dataType; + } + + return url_endpoint; }, // TODO: evaluate moving all of this to Views.Base diff --git a/bower.json b/bower.json index 9149ee8..13f9bcc 100644 --- a/bower.json +++ b/bower.json @@ -4,13 +4,13 @@ "backbone.drupal.js", "backbone.drupal.services.js" ], - "version": "0.1.0-beta", + "version": "0.2.0-alpha", "homepage": "http://enzolutions.com/projects/backbone_drupal/", "author": { "name": "enzo - Eduardo Garcia", "email": "enzo@enzolutions.com" }, - "description": "Backbone.drupal extension provides Drupal compatible using Services module", + "description": "Backbone.drupal extension provides connection to Drupal 7/8", "keywords": [ "backbone", "marionette", diff --git a/test/index.html b/test/index.html index 8431303..b73eec7 100644 --- a/test/index.html +++ b/test/index.html @@ -1,4 +1,5 @@ + @@ -14,8 +15,7 @@ $(function() { // Set API Information Backbone.Drupal.restEndpoint = { - root: 'http://onthisday/api', - dataType: '.json' + root: 'http://drupal8' }; // Define auth object, set crossDomain if is necessary var Auth = new Backbone.Drupal.Auth({crossDomain: true}); @@ -36,16 +36,6 @@ console.log(user.attributes.mail); } }); - /* - Check users retrive - */ - var Users = new Backbone.Drupal.Collections.UserIndex(); - Users.fetch({ - success: function (users) { - // Check information retrived, could be used directly in a template - console.log(users.models[0].attributes.uri); - } - }); }); diff --git a/test/indexd7.html b/test/indexd7.html new file mode 100644 index 0000000..ab7c9b7 --- /dev/null +++ b/test/indexd7.html @@ -0,0 +1,55 @@ + + + + + + Backbone Drupal Library + + + + + + + + + + + diff --git a/test/jquery.js b/test/jquery.js index 3c88fa8..f75fb08 100644 --- a/test/jquery.js +++ b/test/jquery.js @@ -9656,6 +9656,8 @@ if ( xhrSupported ) { // To keep consistent with other XHR implementations, cast the value // to string and ignore `undefined`. if ( headers[ i ] !== undefined ) { + //console.log(i); + //console.log(headers[ i ]); xhr.setRequestHeader( i, headers[ i ] + "" ); } } @@ -9663,6 +9665,8 @@ if ( xhrSupported ) { // Do send the request // This may raise an exception which is actually // handled in jQuery.ajax (so no try/catch here) + console.log(options); + console.log(options.hasContent && options.data); xhr.send( ( options.hasContent && options.data ) || null ); // Listener