Skip to content

Commit

Permalink
select.js implementation supports .attr() to modify entities
Browse files Browse the repository at this point in the history
  • Loading branch information
lmatteis committed Mar 2, 2012
1 parent 50b1d05 commit b6f3752
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
62 changes: 36 additions & 26 deletions war/WEB-INF/modules/select.js
Expand Up @@ -15,7 +15,6 @@ select.fn = {
this.query = false;
this.options = false;
this.fetchOptions = FetchOptions.Builder.withDefaults();
this.result = [];

this.filterOperators = {
'<' : 'LESS_THAN',
Expand All @@ -42,24 +41,8 @@ select.fn = {
*/
add: function(record) {
var entity = new Entity(this.kind);
for(var i in record) {
var value = record[i];
// google's datastore doesn't like native arrays.
// it needs a Collection for properties with
// multiple values
if(this.isArray(value)) {
value = java.util.Arrays.asList(value);
} else if(this.isObject(value)) {
value = JSON.stringify(value);
}

/*
if(value instanceof java.lang.String || typeof value === "string") {
value = new Text(value);
}
*/

entity.setProperty(i, value);
for(var key in record) {
this.setProperty(entity, key, record[key]);
}
this.datastore.put(entity);
},
Expand All @@ -73,7 +56,17 @@ select.fn = {
* @param {Object} record An object containing values for a new record in the collection.
* @returns {Object} The current `select` object
*/
//attr: function(record) {
attr: function(record) {
var result = this.getResult();
for(var i=0; i<result.length; i++) {
var entity = result[i];
for(var key in record) {
this.setProperty(entity, key, record[key]);
}
// XXX should do bulk update
this.datastore.put(entity);
}
},

/**
* Limit the next database find query.
Expand Down Expand Up @@ -152,10 +145,10 @@ select.fn = {
* @returns {Object} The current `select` object
*/
each: function(fn) {
this.result = this.getResult();
var result = this.getResult();

for(var i=0; i<this.result.length; i++) {
var ent = this.result[i];
for(var i=0; i<result.length; i++) {
var ent = result[i];
fn.call(this.toJS(ent), ent.getKey().getId());
}
},
Expand All @@ -167,9 +160,10 @@ select.fn = {
*
*/
del: function() {
this.result = this.getResult();
for(var i=0; i<this.result.length; i++) {
var ent = this.result[i];
var result = this.getResult();
for(var i=0; i<result.length; i++) {
var ent = result[i];
// XXX should do bulk delete
this.datastore["delete"](ent.getKey());
}
},
Expand Down Expand Up @@ -249,6 +243,22 @@ select.fn = {

return ret;
},
setProperty: function(entity, key, value) {
// google's datastore doesn't like native arrays.
// it needs a Collection for properties with
// multiple values
if(this.isArray(value)) {
value = java.util.Arrays.asList(value);
} else if(this.isObject(value)) {
value = JSON.stringify(value);
}
/*
if(value instanceof java.lang.String || typeof value === "string") {
value = new Text(value);
}
*/
entity.setProperty(key, value);
},
isArray: function( obj ) {
return toString.call(obj) === "[object Array]";
},
Expand Down
9 changes: 9 additions & 0 deletions war/main.js
Expand Up @@ -100,6 +100,15 @@ apejs.urls = {
});
}
},
"/edit/([0-9]+)" : {
get: function(request, response, matches) {
var id = parseInt(matches[1], 10);
select("person")
.find()
.attr({name: "Fuck"});
response.sendRedirect("/");
}
},
"/delete/([0-9]+)" : {
get: function(request, response, matches) {
var id = parseInt(matches[1], 10);
Expand Down
3 changes: 2 additions & 1 deletion war/skins/person.html
@@ -1,3 +1,4 @@
<a href='/person/{{id}}'>{{name}}</a>
({{age}}) - {{gender}}
(<a href='/delete/{{id}}'>delete</a>)<br>
(<a href='/delete/{{id}}'>delete</a> -
<a href='/edit/{{id}}'>edit</a>)<br>

0 comments on commit b6f3752

Please sign in to comment.