Skip to content

Commit ca83e78

Browse files
committed
Enabled support to backform
1 parent 46a6d34 commit ca83e78

File tree

5 files changed

+78
-18
lines changed

5 files changed

+78
-18
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Backbone.Drupal is a standalone plugin to connect Marionette JS and Backbone JS
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+
Backbone.Drupal has compatibility with [Backform](http://amiliaapp.github.io/backform/index.html) Drupal 8 return an object for each property if you enable this compatibility attibuttes will be flatten to an array with value property. But this feature could be used for other implementations.
23+
24+
2225
# Usage
2326

2427
Check **test/index.html** for Drupal 8 example and **indexd7.html** for Drupal 7 example.
@@ -52,6 +55,10 @@ Your configuration must look similar to following image.
5255
root: 'http://onthisday/api',
5356
dataType: '.json'
5457
};
58+
59+
// Set Backform Compatibility (amiliaapp.github.io/backform/index.html)
60+
//Backbone.Drupal.backform = true;
61+
5562
// Define auth object, set crossDomain if is necessary
5663
var Auth = new Backbone.Drupal.Auth({crossDomain: true});
5764
// Request executed in sync mode

backbone.drupal.services.js

+65-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// ## Backbone Drupal Models
77

88
// ### Backbone.Drupal.Node
9-
//
109
// Model for nodes.
1110
Backbone.Drupal.Models.Node = Backbone.Drupal.Models.Base.extend({
1211
urlSource: "node",
@@ -19,9 +18,26 @@ Backbone.Drupal.Models.Node = Backbone.Drupal.Models.Base.extend({
1918
},
2019

2120
toJSON: function() {
22-
return {
23-
node: Backbone.Drupal.Models.Base.prototype.toJSON.call(this)
24-
};
21+
if(Backbone.Drupal.backform) {
22+
return Backbone.Drupal.Models.Base.prototype.toJSON.call(this);
23+
}
24+
else {
25+
return {
26+
node: Backbone.Drupal.Models.Base.prototype.toJSON.call(this)
27+
};
28+
}
29+
},
30+
parse: function (data) {
31+
if(Backbone.Drupal.backform) {
32+
var attributes = [];
33+
_.each(data, function(element, index, list) {
34+
attributes[index] = element[0].value;
35+
});
36+
return attributes;
37+
}
38+
else {
39+
return data;
40+
}
2541
},
2642

2743
// Processor for Boolean values, needed due to way Services treats "false".
@@ -56,11 +72,27 @@ Backbone.Drupal.Models.Comment = Backbone.Drupal.Models.Base.extend({
5672
// Override toJSON function to nest all attributes in a { comment: ... } key
5773
// to make this work with the Services module implementation of comment PUSH/PUT.
5874
toJSON: function() {
59-
var data = {
60-
comment: Backbone.Drupal.Models.Base.prototype.toJSON.call(this)
61-
};
62-
return data;
63-
}
75+
if(Backbone.Drupal.backform) {
76+
return Backbone.Drupal.Models.Base.prototype.toJSON.call(this);
77+
}
78+
else {
79+
return {
80+
comment: Backbone.Drupal.Models.Base.prototype.toJSON.call(this)
81+
};
82+
}
83+
},
84+
parse: function (data) {
85+
if(Backbone.Drupal.backform) {
86+
var attributes = [];
87+
_.each(data, function(element, index, list) {
88+
attributes[index] = element[0].value;
89+
});
90+
return attributes;
91+
}
92+
else {
93+
return data;
94+
}
95+
},
6496
});
6597

6698
// ### Backbone.Drupal.File
@@ -71,13 +103,30 @@ Backbone.Drupal.Models.File = Backbone.Drupal.Models.Base.extend({
71103
idAttribute: "fid",
72104

73105
// Override toJSON function to nest all attributes in a { file: ... } key
74-
// to make this work with the Services module implementation of file PUSH/PUT.
106+
// to make this work with the Services module implementation of file PUSH/PUT.turn data;
75107
toJSON: function() {
76-
var data = {
77-
file: Backbone.Drupal.Models.Base.prototype.toJSON.call(this)
78-
};
79-
return data;
80-
}
108+
if(Backbone.Drupal.backform) {
109+
return Backbone.Drupal.Models.Base.prototype.toJSON.call(this);
110+
}
111+
else {
112+
return {
113+
file: Backbone.Drupal.Models.Base.prototype.toJSON.call(this)
114+
};
115+
}
116+
},
117+
parse: function (data) {
118+
if(Backbone.Drupal.backform) {
119+
var attributes = [];
120+
_.each(data, function(element, index, list) {
121+
attributes[index] = element[0].value;
122+
});
123+
return attributes;
124+
}
125+
else {
126+
return data;
127+
}
128+
},
129+
81130
});
82131

83132

@@ -92,7 +141,7 @@ Backbone.Drupal.Collections.NodeIndex = Backbone.Drupal.Collections.Base.extend(
92141
model: Backbone.Drupal.Models.Node,
93142
url: function() {
94143
var restEndpoint = Backbone.Drupal.restEndpoint.root + (Backbone.Drupal.restEndpoint.root.charAt(Backbone.Drupal.restEndpoint.root.length - 1) === '/' ? '' : '/');
95-
return restEndpoint + "node" + Backbone.Drupal.restEndpoint.dataType;
144+
return restEndpoint + "node" . Backbone.Drupal.restEndpoint.dataType;
96145
}
97146
});
98147

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.0-alpha",
7+
"version": "0.2.0-beta",
88
"homepage": "http://enzolutions.com/projects/backbone_drupal/",
99
"author": {
1010
"name": "enzo - Eduardo Garcia",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "backbone.drupal",
33
"description": "Allows backbone models to work with Drupal via REST.",
4-
"version": "0.1.0",
4+
"version": "0.2.0-beta",
55
"homepage": "https://github.com/enzolutions/backbone.drupal",
66
"author": {
77
"name": "enzo - Eduardo Garcia",

test/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
Backbone.Drupal.restEndpoint = {
1818
root: 'http://drupal8'
1919
};
20+
21+
// Set Backform Compatibility (amiliaapp.github.io/backform/index.html)
22+
//Backbone.Drupal.backform = true;
23+
2024
// Define auth object, set crossDomain if is necessary
2125
var Auth = new Backbone.Drupal.Auth({crossDomain: true});
2226
// Request executed in sync mode

0 commit comments

Comments
 (0)