New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

knockout-sync

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

knockout-sync - npm Package Compare versions

Comparing version 0.1.10 to 0.1.11

2

package.json
{
"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});

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc