Skip to content

Instantly share code, notes, and snippets.

@sebastienblanc
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastienblanc/9652216 to your computer and use it in GitHub Desktop.
Save sebastienblanc/9652216 to your computer and use it in GitHub Desktop.
(function(global, define) {
define(function (require) {
var when;
when = require('when');
require('aerogear');
var ms = require('mathsync');
function AeroGearCore(client, namespace, config, model, className) {
var self = this;
this._rest = AeroGear.Pipeline({
baseURL: client
}).add(namespace).pipes[namespace];
this._syncrest = AeroGear.Pipeline({
baseURL: client
}).add('sync').pipes['sync'];
var dataManager = AeroGear.DataManager();
if (config != null) {
config.name = namespace
}
this._model = model;
this._className = className;
var contactList = new ContactFindOptions();
contactList.filter="Bobby";
contactList.multiple=true;
var fields = ["id","displayName", "phoneNumbers"];
config ? dataManager.add(config) : dataManager.add(namespace);
this._dataManager = dataManager.stores[namespace];
if(config.type === "IndexedDB" || config.type === "WebSQL" ) {
// TODO Is there something to do in fact ?
this._dataManager.open({
success: function(e) {
console.log("DB is open" + e);
var a =1;
},
error: function(e) {
console.log("open is getting an exception" + e);
var a =1;
}
});
}
this._online = navigator.onLine;
this._offlineAction = [];
window.addEventListener('online', function() {
self._online = true;
self._push();
//self._sync();
});
window.addEventListener('offline', function(){
self._online = false;
});
}
AeroGearCore.prototype = {
provide: false,
load: function() {
var self = this;
function getContacts(contacts){
  self._dataManager.remove();
self._dataManager.save(contacts);
return contacts;
}
navigator.contacts.find(fields, getContacts, function(error){alert(error);},contactList );
//return self._contacts;
},
add: function(item) {
console.log("add in AeroGearCore");
var self = this;
if (this._online) {
return this._rest.save(item, {
success: function(data) {
data.id = parseInt(data.id);
self._dataManager.save(data);
return data;
},
error: function(error) {
console.log("Error in add send");
return error;
}
});
} else {
this._offlineAction.push({action: 'add', item: item});
}
},
remove: function(item) {
console.log("remove in AeroGearCore");
item.id = parseInt(item.id);
var self = this;
if (this._online) {
return this._rest.remove(item, {
success: function() {
self._dataManager.remove(item);
return item;
},
error: function(error) {
console.log("Error in remove send");
return error;
}
});
} else {
this._offlineAction.push({action: 'remove', item: item});
}
},
update: function(item) {
console.log("update in AeroGearCore");
item.id = parseInt(item.id);
var self = this;
if (this._online) {
return this._rest.save(item, {
success: function(data) {
self._dataManager.save(data);
return data;
},
error: function(error) {
console.log("Error in remove send");
return error;
}
});
} else {
this._offlineAction.push({action: 'update', item: item});
}
},
clear: function() {
console.log("clear in AeroGearCore");
},
_push: function() {
var self = this;
this._offlineAction.forEach(function(data, index) {
self[data.action].apply(self, [data.item]);
});
},
_sync: function() {
var local = ms.summarizer.fromItems(this._dataManager.read(), ms.serialize.fromString());
function fetchSummary(level) {
return this._syncrest.read({id: level}, {
success: function(summary) {
return JSON.parse(summary);
},
error: function(error) {
console.log('Unable to sync');
}
});
}
var remote = ms.summarizer.fromJSON(fetchSummary);
var resolveDiff = ms.resolver.fromSummarizers(local, remote, ms.serialize.toString());
resolveDiff().then(function (difference) {
if(difference.added.length != 0 || difference.removed.length != 0) {
difference.updated = [];
var i,j;
for (i = 0; i < difference.added; i+=1) {
for (j = 0; j < difference.removed.length; ++j) {
if(difference.added[j].id === difference.removed[j].id) {
difference.removed[j].splice(j, 1);
difference.updated.push(difference.added[j].splice(i, 1));
i--; j--;
break;
}
}
}
difference.added.forEach(function(data){
data.id = parseInt(data.id);
self._model.add(data);
});
difference.removed.forEach(function(data){
data.id = parseInt(data.id);
self._model.remove(data);
});
difference.updated.forEach(function(data){
data.id = parseInt(data.id);
self._model.update(data);
});
}
});
}
};
return AeroGearCore;
});
})(this.window || global,
typeof define == 'function'
? define
: function(factory) { module.exports = factory(require); }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment