Permalink
Browse files

Only do get when needed

Do nested property lookup
  • Loading branch information...
1 parent 8fb40d1 commit 3c56be0b764de4f827a50d4e5182fef81b555dd2 @kriszyp kriszyp committed Feb 25, 2010
Showing with 24 additions and 26 deletions.
  1. +24 −26 lib/facet.js
View
@@ -63,7 +63,14 @@ function FacetedStore(store, facetClass){
for(var i in facetClass){
constructor[i] = facetClass[i];
}
- var needsOldVersion = true; // TODO: compute based on schema (if there are any readonly or blocked properties)
+ var needsOldVersion;
+ var properties = constructor.properties;
+ for(var i in properties){
+ var propDef = properties[i];
+ if(propDef.readonly || propDef.blocked){
+ needsOldVersion = true;
+ }
+ }
constructor.id = store.id;
constructor.query= function(query, options){
if(typeof facetClass.query !== "function"){
@@ -92,8 +99,10 @@ function FacetedStore(store, facetClass){
}
if(id.indexOf('.') > -1 && (id.indexOf('?') == -1 || id.indexOf('.') < id.indexOf('?'))){
var parts = id.split('.');
- var object = this.get(parts[0]);
- var value = object && (object.get ? object.get(parts[1]) : object[parts[1]]);
+ var value = this.get(parts[0]);
+ for(var i = 1; i < parts.length; i++){
+ value = value && (value.get ? value.get(parts[i]) : value[parts[i]]);
+ }
return value;
}
if(id === '' || id.match(/\?|\[/)){
@@ -126,15 +135,12 @@ function FacetedStore(store, facetClass){
}
return this.wrap(facetClass.get(id), this.transaction);
};
- function create(instance){
+ function create(instance, options){
instance = constructor.wrap({}, constructor.transaction, instance, NEW);
if(typeof instance.initialize === "function"){
instance.initialize.apply(instance, arguments);
}
- if(constructor.transaction){
- (constructor.transaction.newInstances = constructor.transaction.newInstances || []).push(instance);
- }
- return when(instance.save({overwrite:false}), function(){
+ return when(instance.save(options || {overwrite:false}), function(){
return instance;
});
};
@@ -192,8 +198,8 @@ function FacetedStore(store, facetClass){
}
};
- constructor.post = function(props, metadata){
- if(!metadata.id){
+ constructor.post = function(props, directives){
+ if(!directives.id){
// create a new object
return this.put(props, {overwrite: false});
}
@@ -202,17 +208,13 @@ function FacetedStore(store, facetClass){
// TODO: Do this: if(props instanceof RPC){ // where the media handler creates RPC objects
if("method" in props && "id" in props && "params" in props){
// looks like JSON-RPC
- return rpcInvoke(this.get(metadata.id), props);
+ return rpcInvoke(this.get(directives.id), props);
}
// doing an incremental update
- return this.copyProperties(props, metadata);
+ return this.copyProperties(props, directives);
}
};
- constructor.isNew= function(instance){
- return this.transaction.newInstances && this.transaction.newInstances.indexOf(instance) > -1;
- };
-
constructor.__proto__ = httpHandlerPrototype;
// TODO: handle immutable proto
@@ -342,8 +344,8 @@ var SchemaControlled = function(facetSchema, sourceClass){
enumerable: false
},
save: {
- value: function(metadata){
- metadata = metadata || {};
+ value: function(directives){
+ directives = directives || {};
if(facetPrototype.save){
facetPrototype.save.call(this, source);
}
@@ -360,16 +362,13 @@ var SchemaControlled = function(facetSchema, sourceClass){
}
}
mustBeValid(validation);
- if(transaction){
- var newIndex = transaction.newInstances && transaction.newInstances.indexOf(instance);
- }
try{
if(typeof facetSchema.put === "function"){
- var id = facetSchema.put(source, metadata);
+ var id = facetSchema.put(source, directives);
}
else{
if(facetSchema.__noSuchMethod__){
- var id = facetSchema.__noSuchMethod__("put", [source, metadata]);
+ var id = facetSchema.__noSuchMethod__("put", [source, directives]);
}
else{
throw new MethodNotAllowedError("put is not allowed");
@@ -385,9 +384,8 @@ var SchemaControlled = function(facetSchema, sourceClass){
});
}
finally{
- if(newIndex > -1){
- transaction.generatedId = source.getId ? source.getId() : source.id;
- transaction.newInstances.splice(newIndex, 1);
+ if(typeof id == "string" || typeof id == "number"){
+ transaction.generatedId = id;
}
}
function transfer(value){

0 comments on commit 3c56be0

Please sign in to comment.