Skip to content

Commit 46a6d34

Browse files
committed
Updated documentation
1 parent 7d0783b commit 46a6d34

4 files changed

+86
-77
lines changed

README.md

+85-76
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
33
**Table of Contents**
44

5+
- [Usage](#usage)
56
- [Bower install](#bower-install)
67
- [Bower command](#bower-command)
78
- [Include as dependencie](#include-as-dependencie)
@@ -11,25 +12,98 @@
1112
- [Auth](#auth)
1213
- [Drupal 7](#drupal-7)
1314
- [ToDo](#todo)
14-
- [Usage](#usage)
1515

1616
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1717

18-
Backbone.Drupal is a standalone plugin to connect Marionette JS and Backbone JS applications with Drupal 7/8
18+
Backbone.Drupal is a standalone plugin to connect Marionette JS and Backbone JS applications with Drupal 7/8 to execute [RESTful](http://en.wikipedia.org/wiki/Representational_state_transfer) States to elaborate operation GET, POST, DELETE etc.
1919

2020
Originally Inspired in JS files from Drupal 7 Module <a href="https://drupal.org/project/backbone" target="_blank">https://drupal.org/project/backbone</a>
2121

22-
### Bower install
22+
# Usage
23+
24+
Check **test/index.html** for Drupal 8 example and **indexd7.html** for Drupal 7 example.
25+
26+
Before to test in Drupal 8 be sure the REST Resource Content for methods Get, Post, Update , Delete and Patch has json as format and Basic Auth as Authentication method.
27+
28+
You can the contributed module [Rest UI](https://www.drupal.org/project/restui) I recommend use the git version until Drupal 8 get his first official release.
29+
30+
Your configuration must look similar to following image.
31+
32+
![REST UI Content](https://raw.githubusercontent.com/enzolutions/backbone.drupal/master/images/backbone.drupal_drupal8_restui_content_resource.png "REST UI Content")
33+
34+
## Code Sample
35+
````
36+
<!DOCTYPE html>
37+
<html lang="en">
38+
<head>
39+
<meta charset="utf-8">
40+
<title>Backbone Drupal Library</title>
41+
</head>
42+
<body>
43+
<script src="./jquery.js"></script>
44+
<script src="./underscore.js"></script>
45+
<script src="./backbone.js"></script>
46+
<script src="./backbone.drupal.js"></script>
47+
<script src="./backbone.drupal.services.js"></script>
48+
<script>
49+
$(function() {
50+
// Set API Information
51+
Backbone.Drupal.restEndpoint = {
52+
root: 'http://onthisday/api',
53+
dataType: '.json'
54+
};
55+
// Define auth object, set crossDomain if is necessary
56+
var Auth = new Backbone.Drupal.Auth({crossDomain: true});
57+
// Request executed in sync mode
58+
// If status is token further ajax will use the proper token
59+
var auth_status = = Auth.login('admin', 'admin');
60+
61+
if(auth_status) {
62+
63+
// Check user retrieve
64+
65+
var User = new Backbone.Drupal.Models.User({uid: 1});
66+
User.fetch({
67+
success: function (user) {
68+
// Check information retrived, could be used directly in a template
69+
console.log(user.attributes.mail);
70+
}
71+
});
72+
73+
// Check users retrive
74+
75+
var Users = new Backbone.Drupal.Collections.UserIndex();
76+
Users.fetch({
77+
success: function (users) {
78+
// Check information retrived, could be used directly in a template
79+
console.log(users.models[0].attributes.uri);
80+
}
81+
});
82+
} else {
83+
alert('Auth Error');
84+
}
85+
});
86+
87+
</script>
88+
89+
</body>
90+
</html>
91+
92+
````
93+
94+
Note: This plugin could be used with <a href="requirejs.org" target="_blank">RequireJS</a>.
95+
96+
# Bower install
2397

2498
If you are using <a target="_blank" href="http://bower.io/">Bower</a> you can download Backbone.drupal following this instructions
2599

26-
#### Bower command
100+
## Bower command
27101

28102
```
29103
$ bower install backbone.drupal
30104
```
31105

32-
#### Include as dependencie
106+
## Include as dependencie
33107

34108
If your application has a list of bower dependencies, you can include as dependency as shown below.
35109

@@ -46,13 +120,13 @@ Check the releases section to verify the latest version.
46120
47121
```
48122

49-
### Features
123+
# Features
50124

51-
####Cross-origin
125+
##Cross-origin
52126

53127
Enable to have Drupal as Backend in a Domain backend.com and the Backbone/Marionette App in other domain frontend.com.
54128

55-
##### Drupal 8
129+
### Drupal 8
56130

57131
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
58132

@@ -85,7 +159,7 @@ When issues https://www.drupal.org/node/1869548 and https://www.drupal.org/node/
85159

86160
More information at http://enable-cors.org/server_apache.html
87161

88-
###### Auth
162+
#### Auth
89163

90164
Drupal 8 doesn't implement yet the end point /user/login to enable remote login, this method in Drupal allow validate the credentials and request a CRS Token to be used in further request.
91165

@@ -95,7 +169,7 @@ When function Auth is called the credentials are not validated, they are just en
95169

96170
Remember the credentials travel to your backend server **encoded** NOT **encrypted** for tha reason you **MUST** use a SSL certificate in your backend site to protect your users.
97171

98-
##### Drupal 7
172+
### Drupal 7
99173

100174
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.
101175

@@ -133,75 +207,10 @@ var Property = Backbone.Drupal.Models.Node.extend({
133207

134208
**REST**: Integration with Services Server type REST
135209

136-
### ToDo
210+
# ToDo
137211

138212
<ul>
139213
<li>Implement Collections for Taxonomies and Search</li>
140214
<li>Create integration with module Restws in Drupal 7</li>
141215
<li>Test Drupal 8 POST method and Views integrations.</li>
142216
</ul>
143-
144-
### Usage
145-
146-
Check **test/index.html** for Drupal 8 example and **indexd7.html** for Drupal 7 example.
147-
148-
````
149-
<!DOCTYPE html>
150-
<html lang="en">
151-
<head>
152-
<meta charset="utf-8">
153-
<title>Backbone Drupal Library</title>
154-
</head>
155-
<body>
156-
<script src="./jquery.js"></script>
157-
<script src="./underscore.js"></script>
158-
<script src="./backbone.js"></script>
159-
<script src="./backbone.drupal.js"></script>
160-
<script src="./backbone.drupal.services.js"></script>
161-
<script>
162-
$(function() {
163-
// Set API Information
164-
Backbone.Drupal.restEndpoint = {
165-
root: 'http://onthisday/api',
166-
dataType: '.json'
167-
};
168-
// Define auth object, set crossDomain if is necessary
169-
var Auth = new Backbone.Drupal.Auth({crossDomain: true});
170-
// Request executed in sync mode
171-
// If status is token further ajax will use the proper token
172-
var auth_status = = Auth.login('admin', 'admin');
173-
174-
if(auth_status) {
175-
176-
// Check user retrieve
177-
178-
var User = new Backbone.Drupal.Models.User({uid: 1});
179-
User.fetch({
180-
success: function (user) {
181-
// Check information retrived, could be used directly in a template
182-
console.log(user.attributes.mail);
183-
}
184-
});
185-
186-
// Check users retrive
187-
188-
var Users = new Backbone.Drupal.Collections.UserIndex();
189-
Users.fetch({
190-
success: function (users) {
191-
// Check information retrived, could be used directly in a template
192-
console.log(users.models[0].attributes.uri);
193-
}
194-
});
195-
} else {
196-
alert('Auth Error');
197-
}
198-
});
199-
200-
</script>
201-
202-
</body>
203-
</html>
204-
205-
````
206-
207-
Note: This plugin could be used with <a href="requirejs.org" target="_blank">RequireJS</a>.

backbone.drupal.js

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ Backbone.Drupal.Auth = (function(Backbone, $, _){
7474
// Define specific parametres to be used in all future request.
7575
$.ajaxSetup(settings);
7676

77-
console.log(btoa(username + ':' + password));
7877
status=true;
7978
}
8079
else if(attributes.drupal8 === false) {
Loading

test/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
User.fetch({
3434
success: function (user) {
3535
// Check information retrived, could be used directly in a template
36+
console.log(user.attributes);
3637
console.log(user.attributes.mail[0].value);
3738
}
3839
});

0 commit comments

Comments
 (0)