Skip to content

Commit fd392fc

Browse files
committed
Added support to Drupal 8 and maintain version for Drupal 7
1 parent 2f609ea commit fd392fc

File tree

6 files changed

+155
-49
lines changed

6 files changed

+155
-49
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,40 @@ Enable to have Drupal as Backend in a Domain backend.com and the Backbone/Marion
3737

3838
##### Drupal 8
3939

40-
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
40+
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 <a href="https://www.drupal.org/files/issues/core-cors-headers-1869548-26.patch">patch</a> for .htacces to enable CORS request using jQuery documented in issue # https://www.drupal.org/node/1869548#comment-9120317
4141

4242
```
43-
Header set Access-Control-Allow-Origin "*"
43+
diff --git a/.htaccess b/.htaccess
44+
index c32b182..b0bf563 100644
45+
--- a/.htaccess
46+
+++ b/.htaccess
47+
@@ -118,6 +118,10 @@ DirectoryIndex index.php index.html index.htm
48+
RewriteCond %{REQUEST_URI} !core
49+
RewriteRule ^ %1/core/%2 [L,QSA,R=301]
50+
51+
+ # Intercept OPTIONS calls
52+
+ RewriteCond %{REQUEST_METHOD} OPTIONS
53+
+ RewriteRule .* / [R=200,L]
54+
+
55+
# Pass all requests not referring directly to files in the filesystem to
56+
# index.php.
57+
RewriteCond %{REQUEST_FILENAME} !-f
58+
@@ -165,3 +169,7 @@ DirectoryIndex index.php index.html index.htm
59+
</FilesMatch>
60+
</IfModule>
61+
</IfModule>
62+
+
63+
+Header always set Access-Control-Allow-Origin "*"
64+
+Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, PATCH, DELETE"
65+
+Header always set Access-Control-Allow-Headers: Authorization
4466
```
67+
When issues https://www.drupal.org/node/1869548 and https://www.drupal.org/node/2237231 get resolved this implementation will be updated.
4568

4669
More information at http://enable-cors.org/server_apache.html
4770

4871
##### Drupal 7
4972

50-
In your Drupal Server you must setup <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS" target="_blank">HTTP Access Control</a> to enable connection, below and example.
73+
In your Drupal Server you must setup <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS" target="_blank">HTTP Access Control</a> to enable connection, below an example.
5174

5275
````
5376
Access-Control-Allow-Credentials:true
@@ -87,12 +110,14 @@ var Property = Backbone.Drupal.Models.Node.extend({
87110

88111
<ul>
89112
<li>Implement Collections for Taxonomies and Search</li>
90-
<li>Create integration with module Restws</li>
91-
<li>Create version of plugin for Drupal 8.</li>
113+
<li>Create integration with module Restws in Drupal 7</li>
114+
<li>Test Drupal 8 POST method and Views integrations.</li>
92115
</ul>
93116

94117
### Usage
95118

119+
Check **test/index.html** for Drupal 8 example and **indexd7.html** for Drupal 7 example.
120+
96121
````
97122
<!DOCTYPE html>
98123
<html lang="en">

backbone.drupal.js

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Backbone.Drupal.Auth = (function(Backbone, $, _){
2424

2525
// Set defaults. These are the only attributes allowed.
2626
function defaults() {
27-
return { crossDomain: false };
27+
return { crossDomain: false, drupal8: true };
2828
}
2929

3030
// Set attributes
@@ -53,37 +53,64 @@ Backbone.Drupal.Auth = (function(Backbone, $, _){
5353
var status = false;
5454
var restEndpoint = Backbone.Drupal.restEndpoint.root + (Backbone.Drupal.restEndpoint.root.charAt(Backbone.Drupal.restEndpoint.root.length - 1) === '/' ? '' : '/');
5555

56-
jQuery.ajax({
57-
async: false,
58-
url : restEndpoint + 'user/login' + Backbone.Drupal.restEndpoint.dataType,
59-
type : 'post',
60-
data : 'username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password),
61-
//dataType : 'json',
62-
error: function(XMLHttpRequest, textStatus, errorThrown) {
63-
return false;
64-
},
65-
success : function(data) {
66-
67-
var settings = {
68-
beforeSend: function (request) {
69-
request.setRequestHeader("X-CSRF-Token", data.token);
70-
}
71-
};
56+
if(attributes.drupal8) {
57+
// Prepare further calls to use Basic Auth againt drupal 8 and set a cookie
58+
var settings = {
59+
beforeSend: function (request) {
60+
request.setRequestHeader ('Authorization', 'Basic ' + btoa(username + ':' + password));
61+
//request.setRequestHeader ('Accept', 'application/json');
62+
request.setRequestHeader ( "Content-type", "application/x-www-form-urlencoded" );
63+
}
64+
};
7265

73-
if(attributes.crossDomain) {
74-
settings.xhrFields = {
75-
withCredentials: true
66+
if(attributes.crossDomain) {
67+
/*settings.xhrFields = {
68+
withCredentials: true
69+
};*/
70+
71+
//settings.crossDomain = true;
72+
}
73+
74+
// Define specific parametres to be used in all future request.
75+
$.ajaxSetup(settings);
76+
77+
console.log(btoa(username + ':' + password));
78+
status=true;
79+
}
80+
else if(attributes.drupal8 === false) {
81+
// Call user/login end point to get CSR token an use in following calls
82+
jQuery.ajax({
83+
async: false,
84+
url : restEndpoint + 'user/login' + Backbone.Drupal.restEndpoint.dataType,
85+
type : 'post',
86+
data : 'username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password),
87+
//dataType : 'json',
88+
error: function(XMLHttpRequest, textStatus, errorThrown) {
89+
return false;
90+
},
91+
success : function(data) {
92+
93+
var settings = {
94+
beforeSend: function (request) {
95+
request.setRequestHeader("X-CSRF-Token", data.token);
96+
}
7697
};
7798

78-
settings.crossDomain = true;
79-
}
99+
if(attributes.crossDomain) {
100+
settings.xhrFields = {
101+
withCredentials: true
102+
};
103+
104+
settings.crossDomain = true;
105+
}
80106

81-
// Define specific parametres to be used in all future request.
82-
$.ajaxSetup(settings);
107+
// Define specific parametres to be used in all future request.
108+
$.ajaxSetup(settings);
83109

84-
status=true;
85-
}
86-
});
110+
status=true;
111+
}
112+
});
113+
}
87114

88115
return status;
89116
}.bind(this),
@@ -110,7 +137,6 @@ Backbone.Drupal.Auth = (function(Backbone, $, _){
110137
return Auth;
111138
})(Backbone, jQuery, _);
112139

113-
114140
Backbone.Drupal.Models = {};
115141

116142

@@ -197,8 +223,14 @@ Backbone.Drupal.Models.Base = Backbone.Model.extend({
197223
var base = restEndpoint + this.urlSource;
198224

199225
if (this.isNew()) { return base; }
200-
// Add .json for format here.
201-
return base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.get(this.idAttribute)) + Backbone.Drupal.restEndpoint.dataType;
226+
var url_endpoint = base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.get(this.idAttribute));
227+
228+
if(Backbone.Drupal.restEndpoint.drupal8 == false) {
229+
// Add .json for format here.
230+
url_endpoint = url_endpoint + urlBackbone.Drupal.restEndpoint.dataType;
231+
}
232+
233+
return url_endpoint;
202234
},
203235

204236
// TODO: evaluate moving all of this to Views.Base

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"backbone.drupal.js",
55
"backbone.drupal.services.js"
66
],
7-
"version": "0.1.0-beta",
7+
"version": "0.2.0-alpha",
88
"homepage": "http://enzolutions.com/projects/backbone_drupal/",
99
"author": {
1010
"name": "enzo - Eduardo Garcia",
1111
"email": "[email protected]"
1212
},
13-
"description": "Backbone.drupal extension provides Drupal compatible using Services module",
13+
"description": "Backbone.drupal extension provides connection to Drupal 7/8",
1414
"keywords": [
1515
"backbone",
1616
"marionette",

test/index.html

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<!DOCTYPE html>
2+
<!DOCTYPE html>
23
<html lang="en">
34
<head>
45
<meta charset="utf-8">
@@ -14,8 +15,7 @@
1415
$(function() {
1516
// Set API Information
1617
Backbone.Drupal.restEndpoint = {
17-
root: 'http://onthisday/api',
18-
dataType: '.json'
18+
root: 'http://drupal8'
1919
};
2020
// Define auth object, set crossDomain if is necessary
2121
var Auth = new Backbone.Drupal.Auth({crossDomain: true});
@@ -36,16 +36,6 @@
3636
console.log(user.attributes.mail);
3737
}
3838
});
39-
/*
40-
Check users retrive
41-
*/
42-
var Users = new Backbone.Drupal.Collections.UserIndex();
43-
Users.fetch({
44-
success: function (users) {
45-
// Check information retrived, could be used directly in a template
46-
console.log(users.models[0].attributes.uri);
47-
}
48-
});
4939
});
5040

5141
</script>

test/indexd7.html

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE html>
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Backbone Drupal Library</title>
7+
</head>
8+
<body>
9+
<script src="./jquery.js"></script>
10+
<script src="./underscore.js"></script>
11+
<script src="./backbone.js"></script>
12+
<script src="../backbone.drupal.js"></script>
13+
<script src="../backbone.drupal.services.js"></script>
14+
<script>
15+
$(function() {
16+
// Set API Information
17+
Backbone.Drupal.restEndpoint = {
18+
root: 'http://onthisday/api',
19+
dataType: '.json'
20+
};
21+
// Define auth object, set crossDomain if is necessary
22+
var Auth = new Backbone.Drupal.Auth({crossDomain: true, drupal8: false});
23+
// Request executed in sync mode
24+
// If status is token further ajax will use the proper token
25+
var status = Auth.login('admin', 'admin');
26+
27+
console.log(status);
28+
29+
/*
30+
Check user retrieve
31+
*/
32+
33+
var User = new Backbone.Drupal.Models.User({uid: 1});
34+
User.fetch({
35+
success: function (user) {
36+
// Check information retrived, could be used directly in a template
37+
console.log(user.attributes.mail);
38+
}
39+
});
40+
/*
41+
Check users retrive
42+
*/
43+
var Users = new Backbone.Drupal.Collections.UserIndex();
44+
Users.fetch({
45+
success: function (users) {
46+
// Check information retrived, could be used directly in a template
47+
console.log(users.models[0].attributes.uri);
48+
}
49+
});
50+
});
51+
52+
</script>
53+
54+
</body>
55+
</html>

test/jquery.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9656,13 +9656,17 @@ if ( xhrSupported ) {
96569656
// To keep consistent with other XHR implementations, cast the value
96579657
// to string and ignore `undefined`.
96589658
if ( headers[ i ] !== undefined ) {
9659+
//console.log(i);
9660+
//console.log(headers[ i ]);
96599661
xhr.setRequestHeader( i, headers[ i ] + "" );
96609662
}
96619663
}
96629664

96639665
// Do send the request
96649666
// This may raise an exception which is actually
96659667
// handled in jQuery.ajax (so no try/catch here)
9668+
console.log(options);
9669+
console.log(options.hasContent && options.data);
96669670
xhr.send( ( options.hasContent && options.data ) || null );
96679671

96689672
// Listener

0 commit comments

Comments
 (0)