knockout-sync
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -1,2 +0,2 @@ | ||
/*global module:false, __dirname: true */ | ||
/*global module:false */ | ||
module.exports = function(grunt) { | ||
@@ -107,3 +107,3 @@ | ||
grunt.registerTask('test', ['simplemocha']); | ||
grunt.registerTask('default', ['jshint']); | ||
grunt.registerTask('default', ['jshint', 'test']); | ||
}; |
{ | ||
"name": "knockout-sync", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"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', 'Amplify', 'lodash', 'JSON'], function(koMapping, EntityModel, amplify, _, undefinedJSON) { | ||
define(['knockout-mapping', './EntityModel', 'Amplify', 'lodash', 'JSON'], function(koMapping, EntityModel, amplify, _) { | ||
@@ -21,3 +21,4 @@ /** | ||
prefixUrl: '/api/', | ||
putSingular: true | ||
putSingular: true, | ||
removeSingular: true | ||
}, | ||
@@ -147,2 +148,28 @@ options | ||
/** | ||
* Removes an existing entity | ||
* | ||
* if the entity.id() is not set the behaviour is undefined(yet) | ||
* | ||
* callback = function(failure) if the server returns a bad response the response failure is set otherwise its undefined | ||
*/ | ||
this.remove = function(entity, callback) { | ||
if (entity.id() > 0) { | ||
var method, urlPart, successCodes; | ||
var entityMeta = that.model.getEntityMeta(entity.fqn); | ||
method = 'delete'; | ||
urlPart = (that.options.removeSingular ? entityMeta.singular : entityMeta.plural)+'/'+entity.id(); | ||
successCodes = [200, 204]; // or 204 no content | ||
that.dispatchRequest(method, urlPart, undefined, successCodes, function(failure) { | ||
if (!failure) { | ||
amplify.publish('knockout-sync.entity-removed', entity, entityMeta); | ||
} | ||
callback(failure); | ||
}); | ||
} | ||
}; | ||
/** | ||
* Dispatches an request to a server response with the driver | ||
@@ -149,0 +176,0 @@ * |
@@ -1,2 +0,2 @@ | ||
define(['./Exception', './EntityModel', 'lodash', 'knockout', 'knockout-mapping', 'Amplify'], function(Exception, EntityModel, _, ko, koMapping, amplify) { | ||
define(['./Exception', './EntityModel', 'lodash', 'knockout', 'knockout-mapping'], function(Exception, EntityModel, _, ko, koMapping) { | ||
@@ -247,4 +247,11 @@ /** | ||
this.detach = function(entity) { | ||
var entityMeta = that.getEntityMeta(entity.fqn); | ||
var mappedArray = that.entities[entityMeta.plural]; | ||
mappedArray.mappedRemove(entity); | ||
}; | ||
this.init(); | ||
}; | ||
}); |
@@ -1,2 +0,2 @@ | ||
define(['jquery', 'JSON'], function($, undef) { | ||
define(['jquery', 'JSON'], function($) { | ||
@@ -24,3 +24,3 @@ /* | ||
}, | ||
error: function(jqXHR, textStatus, errorThrown) { | ||
error: function(jqXHR) { | ||
// are here errors that do not have a jqXHR server response? | ||
@@ -27,0 +27,0 @@ var response = that.responseFromXHR(jqXHR); |
@@ -265,2 +265,31 @@ /* jshint expr: true */ | ||
}); | ||
it("removes an existing entity", function(done) { | ||
var user = new UserModel({ | ||
name: 'Ross', | ||
email: 'ross@ps-webforge.net', | ||
id: 7 | ||
}); | ||
expectDispatch({ | ||
method: 'delete', | ||
url: '/api/user/7', | ||
data: undefined, | ||
response: response(undefined, 204) | ||
}); | ||
expectAmplify( | ||
'knockout-sync.entity-removed', | ||
function(eventEntity, eventEntityMeta) { | ||
expect(eventEntity).to.be.eql(user); | ||
expect(eventEntityMeta.fqn).to.be.eql(user.fqn); | ||
} | ||
); | ||
backend.remove(user, function(error) { | ||
expect(error).to.not.exist; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -267,0 +296,0 @@ |
@@ -203,2 +203,11 @@ var chai = require('chai'), | ||
}); | ||
it("can detach an object by key", function() { | ||
var ross = new UserModel({name: 'Ross', email: 'ross@ps-webforge.net', id: 7}); | ||
em.attach(ross); | ||
expect(em.find('ACME.Blog.Entities.User', 7), 'find user 7').to.be.eql(ross); | ||
em.detach(ross); | ||
expect(em.find('ACME.Blog.Entities.User', 7), 'should not find user 7').to.be.null; | ||
}); | ||
}); |
49137
1412