knockout-sync
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "knockout-sync", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A small backend using knockout mapping to sync entities with a RESTful Backend", | ||
@@ -5,0 +5,0 @@ "main": "Gruntfile.js", |
@@ -1,2 +0,2 @@ | ||
define(['knockout-mapping', './EntityModel'], function(koMapping, EntityModel) { | ||
define(['knockout-mapping', './EntityModel', 'Amplify'], function(koMapping, EntityModel, amplify) { | ||
@@ -21,3 +21,3 @@ return function Backend(driver, entityModel) { | ||
if (entity.id && entity.id() > 0) { | ||
if (entity.id() > 0) { | ||
method = 'put'; | ||
@@ -30,5 +30,6 @@ url = '/api/'+entityMeta.singular+'/'+entity.id(); | ||
that.driver.dispatch(method, url, that.serialize(entity), function(result) { | ||
if (!entity.id() && result.id) { // get persisted id | ||
that.driver.dispatch(method, url, that.serializeEntity(entity), function(error, result) { | ||
if (!error && !entity.id() && result.id) { // get persisted id | ||
entity.id(result.id); | ||
amplify.publish('new-entity', entity, entityMeta); | ||
} | ||
@@ -38,3 +39,14 @@ }); | ||
this.serialize = function(entity) { | ||
/** | ||
* Queries a collection of all entities returned by backend | ||
*/ | ||
this.cget = function(entityFQN, callback) { | ||
var entityMeta = that.model.getEntityMeta(entityFQN); | ||
that.driver.dispatch('GET', '/api/'+entityMeta.plural, undefined, function(error, result) { | ||
callback(undefined, result); | ||
}); | ||
}; | ||
this.serializeEntity = function(entity) { | ||
if (typeof(entity.serialize) === 'function') { | ||
@@ -41,0 +53,0 @@ return entity.serialize(); |
@@ -1,2 +0,2 @@ | ||
define(['./Exception', './EntityModel', 'lodash', 'knockout', 'knockout-mapping'], function(Exception, EntityModel, _, ko, koMapping) { | ||
define(['./Exception', './EntityModel', 'lodash', 'knockout', 'knockout-mapping', 'Amplify'], function(Exception, EntityModel, _, ko, koMapping, amplify) { | ||
@@ -31,8 +31,17 @@ /** | ||
// initialize empty collections | ||
_.each(that.model.getEntities(), function(entityMeta) { | ||
that.meta[entityMeta.fqn] = entityMeta; | ||
that.entities[entityMeta.plural] = ko.observableArray([]); | ||
}); | ||
this.init = function() { | ||
var firstFakeResponse = {}; | ||
// initialize empty collections | ||
// the ko.observableArray([]) has not the mapped* functions (like mappedRemove), when it is not mapped yet, so we do the first mapping by hand with empty collections | ||
_.each(that.model.getEntities(), function(entityMeta) { | ||
that.meta[entityMeta.fqn] = entityMeta; | ||
firstFakeResponse[entityMeta.plural] = []; | ||
}); | ||
this.mapResponse(firstFakeResponse); | ||
}; | ||
/** | ||
@@ -211,3 +220,13 @@ * Syncs all Entities that are in the response as objects | ||
}; | ||
this.attach = function(entity) { | ||
var entityMeta = that.getEntityMeta(entity.fqn); | ||
var mappedArray = that.entities[entityMeta.plural]; | ||
mappedArray.mappedRemove(entity); | ||
mappedArray.push(entity); | ||
}; | ||
this.init(); | ||
}; | ||
}); |
@@ -20,5 +20,4 @@ define(['require', 'lodash'], function(require, _) { | ||
that.meta = {}; | ||
_.each(jsonModel.entities, function(entityMeta) { | ||
_.each(model.entities, function(entityMeta) { | ||
entityMeta.name = entityMeta.name.replace(/\\/g, '.'); | ||
@@ -25,0 +24,0 @@ |
@@ -60,3 +60,3 @@ var chai = require('chai'); | ||
callback(data); | ||
callback(undefined, data); | ||
}; | ||
@@ -81,3 +81,3 @@ | ||
callback(data); | ||
callback(undefined, data); | ||
}; | ||
@@ -89,2 +89,26 @@ | ||
}); | ||
it("queries a collection of entities", function() { | ||
var dispatched = false; | ||
var user1 = new UserModel({name: 'Ross', email: 'ross@ps-webforge.net', id: 7}); | ||
var user2 = new UserModel({name: 'Rachel', email: 'rachel@ps-webforge.net', id: 8}); | ||
var result = { 'users': [user1.serialize, user2.serialize()] }; | ||
driver.dispatch = function(method, url, data, callback) { | ||
dispatched = true; | ||
expect(method, 'method').to.be.equal('GET'); | ||
expect(url, 'url').to.be.equal('/api/users'); | ||
expect(data, 'data').to.be.eql(undefined); | ||
callback(undefined, result); | ||
}; | ||
backend.cget('ACME.Blog.Entities.User', function(error, returnedResult) { | ||
expect(error).to.be.not.existing; | ||
expect(dispatched, 'driver did dispatch the request').to.be.true; | ||
expect(returnedResult, 'result').to.be.equal(result); | ||
}); | ||
}); | ||
}); |
@@ -164,2 +164,22 @@ var chai = require('chai'), | ||
}); | ||
it("can attach an object by key", function() { | ||
var ross = new UserModel({name: 'Ross', email: 'ross@ps-webforge.net', id: 7}); | ||
expect(em.find('ACME.Blog.Entities.User', 7)).to.not.exist; | ||
em.attach(ross); | ||
expect(em.find('ACME.Blog.Entities.User', 7), 'find user 7').to.be.eql(ross); | ||
}); | ||
it("does not attach the same object twice", function() { | ||
var ross = new UserModel({name: 'Ross', email: 'ross@ps-webforge.net', id: 7}); | ||
expect(em.findAll('ACME.Blog.Entities.User')).to.have.length(0); | ||
em.attach(ross); | ||
em.attach(ross); | ||
expect(em.findAll('ACME.Blog.Entities.User'), 'all users').to.have.length(1); | ||
}); | ||
}); |
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
34149
24
993