spiral-project/daybed.js — pull request #18 by leplatrem (2014-09-07 12:47:09+00:00)
Title: (WIP) import Kept code

If Kept ought to be our reference app, its integration with Daybed should be smooth, consistent and dead simple!

We can't pretend to be sexy and expect developpers to write complex code like in https://github.com/n1k0/kept/pull/26

That's why I took the code from Kept daybed storage and propose to include it in daybed.js. If we needed this for Kept, others will for their app.

Daybed storage in Kept becomes :

```
DaybedStore.prototype = {
setUp: function() {
var self = this;
return Daybed.startSession(self._options.host, {
token: self._options.token
})
.then(function(session) {
self._session = session;
self._session.prefix = PREFIX + session.token.slice(0, 5);
self._options.onSession(session);

return self._session.addModels(self._types);
});
},

load: function() {
var self = this;
var data = [];
return self._session.getRecords(self._types)
.then(function (allRecords) {
for(var model in allRecords) {
var records = allRecords[model];
records.forEach(function (record) {
record.type = model;
})
data = data.concat(records);
}
return data;
});
},

save: function(data) {
var byModelId = {};
data.forEach(function (record) {
if (byModelId[record.type] === undefined)
byModelId[record.type] = [];
byModelId[record.type].push(record);
});

var synchronizeModels = byModelId.map(function (modelId) {
var records = byModelId[modelId];
return this._session.synchronizeRecords(modelId, records);
});

return Promise.all(synchronizeModels);
}
};
```

* [x] Import code
* [ ] Object equals util
* [ ] Unit tests
* [ ] Decide wether we keep the pattern of storing ``modelId`` in records to distinguish them