Skip to content

Commit c17fce5

Browse files
committed
Rename option drupap8 to version, overwrite model save method to PATCH is version is Drupal 8, Updated documentation and samples
1 parent 091e1e9 commit c17fce5

6 files changed

+51
-15
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Check **test/index.html** for Drupal 8 example and **indexd7.html** for Drupal
2929

3030
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.
3131

32-
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.
32+
You can do this with 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.
3333

3434
Your configuration must look similar to following image.
3535

@@ -53,8 +53,9 @@ Your configuration must look similar to following image.
5353
$(function() {
5454
// Set API Information
5555
Backbone.Drupal.restEndpoint = {
56-
root: 'http://onthisday/api',
57-
dataType: '.json'
56+
root: 'http://example.com', // Set Drupal Backend Server URL
57+
version: 8
58+
//dataType: '.json', // Only required in Drupal 7
5859
};
5960
6061
// Define auth object, set crossDomain if is necessary

backbone.drupal.js

+4-4
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, drupal8: true };
27+
return { crossDomain: false, version: 7, dataType: '' };
2828
}
2929

3030
// Set attributes
@@ -53,7 +53,7 @@ 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-
if(attributes.drupal8) {
56+
if(attributes.version == 8) {
5757
// Prepare further calls to use Basic Auth againt drupal 8 and set a cookie
5858
var settings = {
5959
beforeSend: function (request) {
@@ -76,7 +76,7 @@ Backbone.Drupal.Auth = (function(Backbone, $, _){
7676

7777
status=true;
7878
}
79-
else if(attributes.drupal8 === false) {
79+
else if(attributes.version != 8) {
8080
// Call user/login end point to get CSR token an use in following calls
8181
jQuery.ajax({
8282
async: false,
@@ -224,7 +224,7 @@ Backbone.Drupal.Models.Base = Backbone.Model.extend({
224224
if (this.isNew()) { return base; }
225225
var url_endpoint = base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.get(this.idAttribute));
226226

227-
if(Backbone.Drupal.restEndpoint.drupal8 == false) {
227+
if(Backbone.Drupal.restEndpoint.version != 8) {
228228
// Add .json for format here.
229229
url_endpoint = url_endpoint + urlBackbone.Drupal.restEndpoint.dataType;
230230
}

backbone.drupal.services.js

+33-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
Backbone.Drupal.Models.Node = Backbone.Drupal.Models.Base.extend({
1111
urlSource: "node",
1212
idAttribute: "nid",
13-
1413
initialize: function(opts) {
1514
Backbone.Drupal.Models.Base.prototype.initialize.call(this, opts);
1615
// Set up common boolean fields for correct JSON format for services
1716
this.setToJSONProcessor('promote', this.toJSONBoolean);
1817
},
19-
18+
save: function(attrs, options) {
19+
if(Backbone.Drupal.restEndpoint.version == 8) {
20+
// If backend is Drupal 8 change save method to PATCH instead of PUT
21+
options.patch = true;
22+
}
23+
// Proxy the call to the original save function
24+
Backbone.Model.prototype.save.call(this, attrs, options);
25+
},
2026
toJSON: function() {
2127
if(this.backform) {
2228
return Backbone.Drupal.Models.Base.prototype.toJSON.call(this);
@@ -59,7 +65,15 @@ Backbone.Drupal.Models.Node = Backbone.Drupal.Models.Base.extend({
5965
// * TODO: Add support for login and logout methods.
6066
Backbone.Drupal.Models.User = Backbone.Drupal.Models.Base.extend({
6167
urlSource: "user",
62-
idAttribute: "uid"
68+
idAttribute: "uid",
69+
save: function(attrs, options) {
70+
if(Backbone.Drupal.restEndpoint.version == 8) {
71+
// If backend is Drupal 8 change save method to PATCH instead of PUT
72+
options.patch = true;
73+
}
74+
// Proxy the call to the original save function
75+
Backbone.Model.prototype.save.call(this, attrs, options);
76+
},
6377
});
6478

6579
// ### Backbone.Drupal.Comment
@@ -68,7 +82,14 @@ Backbone.Drupal.Models.User = Backbone.Drupal.Models.Base.extend({
6882
Backbone.Drupal.Models.Comment = Backbone.Drupal.Models.Base.extend({
6983
urlSource: "comment",
7084
idAttribute: "cid",
71-
85+
save: function(attrs, options) {
86+
if(Backbone.Drupal.restEndpoint.version == 8) {
87+
// If backend is Drupal 8 change save method to PATCH instead of PUT
88+
options.patch = true;
89+
}
90+
// Proxy the call to the original save function
91+
Backbone.Model.prototype.save.call(this, attrs, options);
92+
},
7293
// Override toJSON function to nest all attributes in a { comment: ... } key
7394
// to make this work with the Services module implementation of comment PUSH/PUT.
7495
toJSON: function() {
@@ -101,7 +122,14 @@ Backbone.Drupal.Models.Comment = Backbone.Drupal.Models.Base.extend({
101122
Backbone.Drupal.Models.File = Backbone.Drupal.Models.Base.extend({
102123
urlSource: "file",
103124
idAttribute: "fid",
104-
125+
save: function(attrs, options) {
126+
if(Backbone.Drupal.restEndpoint.version == 8) {
127+
// If backend is Drupal 8 change save method to PATCH instead of PUT
128+
options.patch = true;
129+
}
130+
// Proxy the call to the original save function
131+
Backbone.Model.prototype.save.call(this, attrs, options);
132+
},
105133
// Override toJSON function to nest all attributes in a { file: ... } key
106134
// to make this work with the Services module implementation of file PUSH/PUT.turn data;
107135
toJSON: function() {

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"backbone.drupal.js",
55
"backbone.drupal.services.js"
66
],
7-
"version": "0.2.1-beta",
7+
"version": "0.2.2-alpha",
88
"homepage": "http://enzolutions.com/projects/backbone_drupal/",
99
"author": {
1010
"name": "enzo - Eduardo Garcia",

test/index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
$(function() {
1616
// Set API Information
1717
Backbone.Drupal.restEndpoint = {
18-
root: 'http://drupal8'
18+
root: 'http://drupal8',
19+
version: 8
1920
};
2021

2122
// Set Backform Compatibility (amiliaapp.github.io/backform/index.html)

test/indexd7.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
dataType: '.json'
2020
};
2121
// Define auth object, set crossDomain if is necessary
22-
var Auth = new Backbone.Drupal.Auth({crossDomain: true, drupal8: false});
22+
var Auth = new Backbone.Drupal.Auth(
23+
{
24+
crossDomain: true,
25+
version: 7
26+
dataType: '.json',
27+
}
28+
);
2329
// Request executed in sync mode
2430
// If status is token further ajax will use the proper token
2531
var status = Auth.login('admin', 'admin');

0 commit comments

Comments
 (0)