knockout-sync
Advanced tools
Comparing version 0.1.2 to 0.1.4
{ | ||
"name": "knockout-sync", | ||
"version": "0.1.2", | ||
"version": "0.1.4", | ||
"description": "A small backend using knockout mapping to sync entities with a RESTful Backend", | ||
@@ -5,0 +5,0 @@ "main": "Gruntfile.js", |
define(['knockout-mapping', './EntityModel', 'Amplify'], function(koMapping, EntityModel, amplify) { | ||
/** | ||
Events: | ||
knockout-sync.entity-saved | ||
triggered when an entity with an existing id was saved again | ||
args: [entity, entityMeta] | ||
knockout-sync.entity-created | ||
triggered when an new entity without an id was saved. the entity has the new id applied | ||
args: [entity, entityMeta] | ||
*/ | ||
return function Backend(driver, entityModel) { | ||
@@ -30,5 +43,18 @@ var that = this; | ||
that.driver.dispatch(method, url, that.serializeEntity(entity), function(error, result) { | ||
if (!error && !entity.id() && result.id) { // get persisted id | ||
if (error) throw error; | ||
var data; | ||
if (result.id) { | ||
data = result; | ||
} else if (result[entityMeta.singular] && result[entityMeta.singular].id) { | ||
data = result[entityMeta.singular]; | ||
} else { | ||
throw "driver did returned an valid result set for a saved entity"; | ||
} | ||
if (!entity.id()) { // set persisted id | ||
entity.id(result.id); | ||
amplify.publish('new-entity', entity, entityMeta); | ||
amplify.publish('knockout-sync.entity-created', entity, entityMeta); | ||
} else { | ||
amplify.publish('knockout-sync.entity-saved', entity, entityMeta); | ||
} | ||
@@ -35,0 +61,0 @@ }); |
@@ -22,2 +22,3 @@ var chai = require('chai'); | ||
}; | ||
var amplify = requirejs('Amplify'); | ||
@@ -64,4 +65,13 @@ describe('Yield Deploy Backend', function() { | ||
var published = false; | ||
amplify.subscribe('knockout-sync.entity-created', function(eventEntity, eventEntityMeta) { | ||
published = true; | ||
expect(eventEntity).to.be.equal(user); | ||
expect(eventEntity.id(), 'event user.id').to.be.equal(7); | ||
expect(eventEntityMeta.fqn).to.be.eql(user.fqn); | ||
}); | ||
backend.save(user); | ||
expect(dispatched, 'dispatch is called').to.be.true; | ||
expect(published, 'publishe is called').to.be.true; | ||
@@ -85,4 +95,12 @@ expect(user.id(), 'user.id').to.be.equal(7); | ||
var published = false; | ||
amplify.subscribe('knockout-sync.entity-saved', function(eventEntity, eventEntityMeta) { | ||
published = true; | ||
expect(eventEntity).to.be.eql(user); | ||
expect(eventEntityMeta.fqn).to.be.eql(user.fqn); | ||
}); | ||
backend.save(user); | ||
expect(dispatched, 'dispatch is called').to.be.true; | ||
expect(published, 'published is called').to.be.true; | ||
expect(user.id(), 'user.id').to.be.equal(7); | ||
@@ -89,0 +107,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35608
1029