Skip to content

Commit 52dc2c8

Browse files
committed
Update Documentation page, improve function toJSONAttribute and Base Model URL function
1 parent 4dede23 commit 52dc2c8

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

README.md

+41-25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ I strong recommend to use the Drupal module <a href="https://drupal.org/project/
2323

2424
**Models**: Created Backbone models for Nodes, Users, Comments Entities
2525

26+
You can add extra fields to modules to use as extra information in you application, these extra fields could be mark as *noSaveAttributes*. Check the following example.
27+
28+
````
29+
var Property = Backbone.Drupal.Models.Node.extend({
30+
initialize : function(options) {
31+
// Setting the Id Attribute for Drupal model
32+
this.attributes.nid = options.property_id;
33+
this.noSaveAttributes = ['property_id'];
34+
35+
// Extended Backbone.Drupal.Models.Node to my own service for Drupal Nodes.
36+
// This Rest service return absolute URL for field pictures
37+
this.urlSource = "node_waterbed";
38+
},
39+
````
40+
2641
**Collections**: Created Backbone collection for Users, Nodes and Views
2742

2843
**REST**: Integration with Services Server type REST
@@ -61,31 +76,32 @@ I strong recommend to use the Drupal module <a href="https://drupal.org/project/
6176
var Auth = new Backbone.Drupal.Auth({crossDomain: true});
6277
// Request executed in sync mode
6378
// If status is token further ajax will use the proper token
64-
var status = Auth.login('admin', 'admin');
65-
66-
console.log(status);
67-
68-
/*
69-
Check user retrieve
70-
*/
71-
72-
var User = new Backbone.Drupal.Models.User({uid: 1});
73-
User.fetch({
74-
success: function (user) {
75-
// Check information retrived, could be used directly in a template
76-
console.log(user.attributes.mail);
77-
}
78-
});
79-
/*
80-
Check users retrive
81-
*/
82-
var Users = new Backbone.Drupal.Collections.UserIndex();
83-
Users.fetch({
84-
success: function (users) {
85-
// Check information retrived, could be used directly in a template
86-
console.log(users.models[0].attributes.uri);
87-
}
88-
});
79+
var auth_status = = Auth.login('admin', 'admin');
80+
81+
if(auth_status) {
82+
83+
// Check user retrieve
84+
85+
var User = new Backbone.Drupal.Models.User({uid: 1});
86+
User.fetch({
87+
success: function (user) {
88+
// Check information retrived, could be used directly in a template
89+
console.log(user.attributes.mail);
90+
}
91+
});
92+
93+
// Check users retrive
94+
95+
var Users = new Backbone.Drupal.Collections.UserIndex();
96+
Users.fetch({
97+
success: function (users) {
98+
// Check information retrived, could be used directly in a template
99+
console.log(users.models[0].attributes.uri);
100+
}
101+
});
102+
} else {
103+
alert('Auth Error');
104+
}
89105
});
90106
91107
</script>

backbone.drupal.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ Backbone.Drupal.Auth = (function(Backbone, $, _){
6161
//dataType : 'json',
6262
error: function(XMLHttpRequest, textStatus, errorThrown) {
6363
return false;
64-
/*console.log(JSON.stringify(XMLHttpRequest));
65-
console.log(JSON.stringify(textStatus));
66-
console.log(JSON.stringify(errorThrown));*/
6764
},
6865
success : function(data) {
6966

@@ -181,8 +178,10 @@ Backbone.Drupal.Models.Base = Backbone.Model.extend({
181178

182179
// #### toJSONAttribute: process attribute into JSON if process funciton given.
183180
toJSONAttribute: function(attributeValue, attributeName) {
184-
if (this.toJSONProcessors[attributeName]) {
185-
attributeValue = this.toJSONProcessors[attributeName].call(this, attributeValue);
181+
if (typeof this.toJSONProcessors !== 'undefined') {
182+
if (this.toJSONProcessors[attributeName]) {
183+
attributeValue = this.toJSONProcessors[attributeName].call(this, attributeValue);
184+
}
186185
}
187186
return attributeValue;
188187
},
@@ -196,9 +195,10 @@ Backbone.Drupal.Models.Base = Backbone.Model.extend({
196195
// Modified from Backbone.js to ignore collection and add ".format" extension.
197196
var restEndpoint = Backbone.Drupal.restEndpoint.root + (Backbone.Drupal.restEndpoint.root.charAt(Backbone.Drupal.restEndpoint.root.length - 1) === '/' ? '' : '/');
198197
var base = restEndpoint + this.urlSource;
198+
199199
if (this.isNew()) { return base; }
200200
// Add .json for format here.
201-
return base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.id) + Backbone.Drupal.restEndpoint.dataType;
201+
return base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.get(this.idAttribute)) + Backbone.Drupal.restEndpoint.dataType;
202202
},
203203

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

backbone.drupal.services.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,13 @@ Backbone.Drupal.Collections.NodeView = Backbone.Drupal.Collections.Base.extend({
118118
},
119119
url: function() {
120120
var restEndpoint = Backbone.Drupal.restEndpoint.root + (Backbone.Drupal.restEndpoint.root.charAt(Backbone.Drupal.restEndpoint.root.length - 1) === '/' ? '' : '/');
121-
return restEndpoint + "views/" + this.viewName + Backbone.Drupal.restEndpoint.dataType;
121+
122+
return restEndpoint + "views/" + this.viewName + Backbone.Drupal.restEndpoint.dataType + (typeof(this.filters) !== "undefined"? this.filters: '');
123+
},
124+
setFilters: function (filters) {
125+
if(filters !== '') {
126+
this.filters = filters;
127+
}
122128
}
123129
});
124130

0 commit comments

Comments
 (0)