node-barefoot
Advanced tools
Comparing version 0.0.9 to 0.0.10
# Changes | ||
## 0.0.10 | ||
* Bug fixed: `DataStore` was not deserializing collections properly. | ||
## 0.0.9 | ||
@@ -3,0 +6,0 @@ * `APIAdapter.Server` provides the `del` alias for `delete` now to ensure compatibility with JavaScripts reserved words. |
@@ -33,13 +33,29 @@ /** Mixin: Barefoot.DataStore.Client | ||
var serializedData = serializedStore[key] | ||
, identifier = serializedData.dataStoreModelIdentifier; | ||
, identifier = serializedData.dataStoreModelIdentifier | ||
, Model; | ||
if(!_.has(self.registeredModels, identifier)) { | ||
if(!_.has(self.registeredModelsAndCollections, identifier)) { | ||
throw new Error('Could not deserialize DataStore because ' + | ||
'a required model was not registered before!'); | ||
} else { | ||
var Model = self.registeredModels[identifier] | ||
, jsonRepresentation = serializedData.data | ||
, deserializedModel = new Model(jsonRepresentation); | ||
var metaInformation =self.registeredModelsAndCollections[identifier] | ||
, jsonRepresentation = serializedData.data; | ||
self.set(key, deserializedModel); | ||
if(_.has(metaInformation, 'collectionClass')) { | ||
var Collection = metaInformation.collectionClass | ||
, deserializedCollection = new Collection(); | ||
Model = metaInformation.modelClass; | ||
self.set(key, deserializedCollection); | ||
for(var i in jsonRepresentation) { | ||
var modelJson = jsonRepresentation[i]; | ||
deserializedCollection.add(new Model(modelJson)); | ||
} | ||
} else { | ||
Model = metaInformation; | ||
self.set(key, new Model(jsonRepresentation)); | ||
} | ||
} | ||
@@ -46,0 +62,0 @@ }); |
@@ -41,13 +41,13 @@ /** Class: Barefoot.DataStore | ||
/** Function: initialize | ||
* Ensures that an empty registeredModels instance variable is created for the | ||
* DataStore. | ||
* Ensures that an empty registeredModelsAndCollections instance variable is | ||
* created for the DataStore. | ||
*/ | ||
function initialize() { | ||
this.registeredModels = {}; | ||
this.registeredModelsAndCollections = {}; | ||
} | ||
/** Function: registerModel | ||
* Registers a specific <Barefoot.Model> or <Barefoot.Collection> | ||
* with the given identifier. This information is needed when serializing the | ||
* DataStore for transferring data to the client. | ||
* Registers a specific <Barefoot.Model> with the given identifier. This | ||
* information is needed when serializing the DataStore for transferring data to | ||
* the client. | ||
* | ||
@@ -62,20 +62,32 @@ * You will not be able to serialize a store containing models which were not | ||
* (String) identifier - A unique identifier for this model type | ||
* (Barefoot.Model, Barefoot.Collection) model - Model/Collection type | ||
* (Barefoot.Model) model - Model type | ||
*/ | ||
function registerModel(identifier, model) { | ||
this.registeredModels[identifier] = model; | ||
this.registeredModelsAndCollections[identifier] = model; | ||
} | ||
/** Function: registerCollection | ||
* An alias for <registerModel>. | ||
* Registers a specific <Barefoot.Collection> and the <Barefoot.Model>s which | ||
* are contained in it with the given identifier. This information is needed | ||
* when serializing the DataStore for transferring data to the client. | ||
* | ||
* You will not be able to serialize a store containing collections which were | ||
* not registered before! | ||
* | ||
* Important: | ||
* Do not register "instances" of a collection. Instead, register its "class". | ||
* | ||
* Parameters: | ||
* (String) identifier - A unique identifier for this model type | ||
* (Barefoot.Model, Barefoot.Collection) model - Model/Collection type | ||
* (String) identifier - A unique identifier for this collection type | ||
* (Barefoot.Collection) collection - Collection type | ||
* (Barefoot.Model) model - Model type contained in the collection | ||
*/ | ||
function registerCollection(identifier, model) { | ||
this.registerModel(identifier, model); | ||
function registerCollection(identifier, collection, model) { | ||
this.registeredModelsAndCollections[identifier] = { | ||
collectionClass: collection | ||
, modelClass: model | ||
}; | ||
} | ||
/** Function: getRegisteredModels | ||
/** Function: getRegisteredModelsAndCollections | ||
* Returns an object literal containing all currently registered models and | ||
@@ -86,9 +98,9 @@ * collections. | ||
* (Object) an object containing all registered <Barefoot.Model> and | ||
* <Barefoot.Collection> | ||
* <Barefoot.Collection>. | ||
*/ | ||
function getRegisteredModels() { | ||
return this.registeredModels; | ||
function getRegisteredModelsAndCollections() { | ||
return this.registeredModelsAndCollections; | ||
} | ||
/** Function: findRegisteredModelIdentifier | ||
/** Function: findRegisteredIdentifier | ||
* Scans the registered models and collections for the given model. When found, | ||
@@ -106,11 +118,20 @@ * the identifier of the registration gets returned. Otherwise undefined is | ||
*/ | ||
function findRegisteredModelIdentifier(model) { | ||
function findRegisteredIdentifier(modelOrCollection) { | ||
var foundKey; | ||
for(var key in this.registeredModels) { | ||
var registeredModel = this.registeredModels[key]; | ||
for(var key in this.registeredModelsAndCollections) { | ||
var registeredModelOrCollection = | ||
this.registeredModelsAndCollections[key]; | ||
if(model.constructor === registeredModel) { | ||
if(modelOrCollection.constructor === registeredModelOrCollection) { | ||
foundKey = key; | ||
break; | ||
} else { | ||
if(_.has(registeredModelOrCollection, 'collectionClass') && | ||
modelOrCollection.constructor === | ||
registeredModelOrCollection.collectionClass) { | ||
foundKey = key; | ||
break; | ||
} | ||
} | ||
@@ -128,5 +149,6 @@ } | ||
DataStore.prototype.registerCollection = registerCollection; | ||
DataStore.prototype.getRegisteredModels = getRegisteredModels; | ||
DataStore.prototype.findRegisteredModelIdentifier = | ||
findRegisteredModelIdentifier; | ||
DataStore.prototype.getRegisteredModelsAndCollections = | ||
getRegisteredModelsAndCollections; | ||
DataStore.prototype.findRegisteredIdentifier = | ||
findRegisteredIdentifier; | ||
DataStore.prototype.sync = function() {}; | ||
@@ -133,0 +155,0 @@ DataStore.prototype.fetch = function() {}; |
@@ -37,5 +37,6 @@ /** Mixin: Barefoot.DataStore.Server | ||
_.each(keys, function(key) { | ||
var model = self.get(key) | ||
, jsonRepresentation = model.toJSON() | ||
, identifier = self.findRegisteredModelIdentifier.call(self, model); | ||
var modelOrCollection = self.get(key) | ||
, jsonRepresentation = modelOrCollection.toJSON() | ||
, identifier = self.findRegisteredIdentifier.call( | ||
self, modelOrCollection); | ||
@@ -51,3 +52,3 @@ if(identifier) { | ||
throw new Error('Could not serialize DataStore because ' + | ||
'a required model was not registered before!'); | ||
'a required modelOrCollection was not registered before!'); | ||
} | ||
@@ -54,0 +55,0 @@ |
@@ -189,3 +189,3 @@ /** Mixin: Barefoot.Router.Server | ||
if(!_.isUndefined(self.dataStore) && | ||
_.keys(self.dataStore.registeredModels).length > 0) { | ||
_.keys(self.dataStore.registeredModelsAndCollections).length > 0) { | ||
var serializiedDataStore = JSON.stringify(self.dataStore.toJSON()) | ||
@@ -192,0 +192,0 @@ , javaScriptElement = |
{ | ||
"name": "node-barefoot" | ||
, "version": "0.0.9" | ||
, "version": "0.0.10" | ||
, "description": "Barefoot makes code sharing between browser and server reality. Write your application once and run it on both ends of the wire." | ||
@@ -5,0 +5,0 @@ , "keywords": [ |
@@ -20,6 +20,6 @@ describe('DataStore.Client', function() { | ||
dataStore.registerModel('model', Model); | ||
dataStore.registerCollection('collection', Collection); | ||
dataStore.registerCollection('collection', Collection, Model); | ||
dataStore.parse(objectRepresentation); | ||
dataStore.get('apple').constructor.should.be.equal(Model); | ||
@@ -26,0 +26,0 @@ dataStore.get('apple').attributes.should.be.eql(apple.attributes); |
describe('DataStore', function() { | ||
var dataStore | ||
, Model = Barefoot.Model.extend() | ||
, Collection = Barefoot.Collection.extend() | ||
, apple = new Model({ name: 'Apple' }) | ||
, vegs = new Collection([ | ||
new Model({ name: 'Tomato' }) | ||
, new Model({ name: 'Salad' }) | ||
]) | ||
, jsonRepresentation = '{"apple":{"dataStoreModelIdentifier":"model","data":{"name":"Apple"}},"vegs":{"dataStoreModelIdentifier":"collection","data":[{"name":"Tomato"},{"name":"Salad"}]}}' | ||
, objectRepresentation = JSON.parse(jsonRepresentation); | ||
, Collection = Barefoot.Collection.extend(); | ||
@@ -22,17 +15,39 @@ | ||
dataStore.registerModel(identifier, Model); | ||
dataStore.getRegisteredModels().should.be.eql(expected); | ||
dataStore.getRegisteredModelsAndCollections().should.be.eql(expected); | ||
}) | ||
}) | ||
describe('getRegisteredModels', function() { | ||
describe('registerCollection', function() { | ||
it('should register a collection with given key', function() { | ||
var identifier = 'collection' | ||
, expected = {}; | ||
expected[identifier] = { | ||
collectionClass: Collection | ||
, modelClass: Model | ||
}; | ||
dataStore = new Barefoot.DataStore(); | ||
dataStore.registerCollection(identifier, Collection, Model); | ||
dataStore.getRegisteredModelsAndCollections().should.be.eql(expected); | ||
}) | ||
}) | ||
describe('getRegisteredModelsAndCollections', function() { | ||
it('should return all currently registered models', function() { | ||
var identifier = 'model' | ||
var modelIdentifier = 'model' | ||
, collectionIdentifier = 'collection' | ||
, expected = {}; | ||
expected[identifier] = Model; | ||
expected[modelIdentifier] = Model; | ||
expected[collectionIdentifier] = { | ||
collectionClass: Collection | ||
, modelClass: Model | ||
}; | ||
dataStore = new Barefoot.DataStore(); | ||
dataStore.registerModel(identifier, Model); | ||
dataStore.getRegisteredModels().should.be.eql(expected); | ||
dataStore.registerModel(modelIdentifier, Model); | ||
dataStore.registerCollection(collectionIdentifier, Collection, Model); | ||
dataStore.getRegisteredModelsAndCollections().should.be.eql(expected); | ||
}) | ||
}) | ||
}) |
@@ -12,3 +12,3 @@ describe('DataStore.Server', function() { | ||
, objectRepresentation = JSON.parse(jsonRepresentation); | ||
describe('toJSON', function() { | ||
@@ -23,6 +23,5 @@ beforeEach(function() { | ||
dataStore.registerModel('model', Model); | ||
dataStore.registerCollection('collection', Collection); | ||
dataStore.registerCollection('collection', Collection, Model); | ||
var actualJSON = JSON.stringify(dataStore.toJSON()); | ||
actualJSON.should.be.equal(jsonRepresentation); | ||
@@ -29,0 +28,0 @@ }) |
793587
5761