knockout-sync
Advanced tools
Comparing version 0.1.10 to 0.1.11
{ | ||
"name": "knockout-sync", | ||
"version": "0.1.10", | ||
"version": "0.1.11", | ||
"description": "A small backend using knockout mapping to sync entities with a RESTful Backend", | ||
@@ -5,0 +5,0 @@ "main": "Gruntfile.js", |
@@ -51,6 +51,6 @@ define(['./Exception', './EntityModel', 'lodash', 'knockout', 'knockout-mapping'], function(Exception, EntityModel, _, ko, koMapping) { | ||
/** | ||
* Syncs all Entities that are in the response as objects | ||
* Syncs all Entities that are in the response as objects with the entities in memory | ||
* | ||
* only Entities defined in mapping data will be synced | ||
* After that the objects can be retrieved with the find* functions | ||
* it removes / adds or updates entities in memory | ||
*/ | ||
@@ -62,2 +62,30 @@ this.mapResponse = function(response) { | ||
/** | ||
* Syncs only the entities in the response with the entities in memory | ||
* | ||
* it does not remove entities in memory | ||
*/ | ||
this.mergeResponse = function(response) { | ||
var mappingMeta = that.getKnockoutMappingMetadata(); | ||
_.each(mappingMeta, function(mapping, collectionKey) { | ||
if (response[collectionKey] && response[collectionKey].length) { | ||
var mappedArray = that.entities[collectionKey]; | ||
var entityMapping = that.getEntityMapping(mapping.entityMeta); | ||
_.each(response[collectionKey], function(unmappedEntity) { | ||
if (mappedArray.mappedIndexOf(unmappedEntity) !== -1) { // existing entity to update | ||
var entity = mappedArray.mappedGet(unmappedEntity); | ||
// we could call update on entity here (if avaible) | ||
koMapping.fromJS(unmappedEntity, entityMapping, entity); | ||
} else { // new entity to create | ||
mappedArray.mappedCreate(unmappedEntity); | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
/** | ||
* Finds a Entity by id | ||
@@ -178,3 +206,5 @@ * | ||
return that.hydrate(entityMeta, options.data); | ||
} | ||
}, | ||
entityMeta: entityMeta | ||
}; | ||
@@ -181,0 +211,0 @@ |
@@ -0,1 +1,2 @@ | ||
/* jshint expr:true */ | ||
var chai = require('chai'), | ||
@@ -184,2 +185,43 @@ expect = chai.expect; | ||
it("can refresh an entity already fetchecd", function () { | ||
var response = { | ||
"users": [{ | ||
"id": 1, | ||
"name": "Alice", | ||
"email": "alice@ps-webforge.com" | ||
}, { | ||
"id": 2, | ||
"name": "Bob", | ||
"email": "bob@ps-webforge.com" | ||
}, { | ||
"id": 5, | ||
"name": "Rachel", | ||
"email": "rachel@ps-webforge.com" | ||
}] | ||
}; | ||
// this puts 3 items into the entityManager | ||
em.mapResponse(response); | ||
// we want to refresh bob | ||
var additionalResponse = { | ||
"users": [ | ||
{ | ||
"id": 2, | ||
"name": "Bob B.", | ||
"email": "bob@ps-webforge.com" | ||
} | ||
] | ||
}; | ||
em.mergeResponse(additionalResponse); | ||
var bob = em.find('ACME.Blog.Entities.User', 2); | ||
expect(bob).to.exist; | ||
expect(bob.name(),'name from bob should have changed').to.be.eql('Bob B.'); | ||
expect(em.find('ACME.Blog.Entities.User', 1), 'other users should not be detached').to.exist; | ||
expect(em.find('ACME.Blog.Entities.User', 5), 'other uses should nto be detached').to.exist; | ||
}); | ||
it("can attach an object by key", function() { | ||
@@ -186,0 +228,0 @@ var ross = new UserModel({name: 'Ross', email: 'ross@ps-webforge.net', id: 7}); |
52760
1511