[#162,model,be/memory][m]: refactor started with the new API on Dataset, Record and Memory backend.
* Tests are passing except for a dataproxy one.
This commit is contained in:
39
src/model.js
39
src/model.js
@@ -44,11 +44,22 @@ my.Dataset = Backbone.Model.extend({
|
||||
initialize: function(model, backend) {
|
||||
_.bindAll(this, 'query');
|
||||
this.backend = backend;
|
||||
if (typeof backend === 'undefined') {
|
||||
// guess backend ...
|
||||
if (this.get('records')) {
|
||||
this.backend = recline.Backend.Memory;
|
||||
}
|
||||
}
|
||||
if (typeof(backend) === 'string') {
|
||||
this.backend = this._backendFromString(backend);
|
||||
}
|
||||
this.fields = new my.FieldList();
|
||||
this.currentRecords = new my.RecordList();
|
||||
this._changes = {
|
||||
deletes: [],
|
||||
updates: [],
|
||||
creates: []
|
||||
};
|
||||
this.facets = new my.FacetList();
|
||||
this.docCount = null;
|
||||
this.queryState = new my.Query();
|
||||
@@ -56,6 +67,17 @@ my.Dataset = Backbone.Model.extend({
|
||||
this.queryState.bind('facet:add', this.query);
|
||||
},
|
||||
|
||||
// ### fetch
|
||||
//
|
||||
// Retrieve dataset and (some) records from the backend.
|
||||
fetch: function() {
|
||||
return this.backend.fetch(this);
|
||||
},
|
||||
|
||||
save: function() {
|
||||
return this.backend.save(this, this._changes);
|
||||
},
|
||||
|
||||
// ### query
|
||||
//
|
||||
// AJAX method with promise API to get records from the backend.
|
||||
@@ -76,6 +98,12 @@ my.Dataset = Backbone.Model.extend({
|
||||
var _doc = new my.Record(hit._source);
|
||||
_doc.backend = self.backend;
|
||||
_doc.dataset = self;
|
||||
_doc.bind('change', function(doc) {
|
||||
self._changes.updates.push(doc.toJSON());
|
||||
});
|
||||
_doc.bind('destroy', function(doc) {
|
||||
self._changes.deletes.push(doc.toJSON());
|
||||
});
|
||||
return _doc;
|
||||
});
|
||||
self.currentRecords.reset(docs);
|
||||
@@ -96,6 +124,7 @@ my.Dataset = Backbone.Model.extend({
|
||||
return dfd.promise();
|
||||
},
|
||||
|
||||
|
||||
_prepareQuery: function(newQueryObj) {
|
||||
if (newQueryObj) {
|
||||
this.queryState.set(newQueryObj);
|
||||
@@ -242,7 +271,15 @@ my.Record = Backbone.Model.extend({
|
||||
}
|
||||
}
|
||||
return html;
|
||||
}
|
||||
},
|
||||
|
||||
// Override Backbone save, fetch and destroy so they do nothing
|
||||
// Instead, Dataset object that created this Record should take care of
|
||||
// handling these changes (discovery will occur via event notifications)
|
||||
// WARNING: these will not persist *unless* you call save on Dataset
|
||||
fetch: function() {},
|
||||
save: function() {},
|
||||
destroy: function() { this.trigger('destroy', this); }
|
||||
});
|
||||
|
||||
// ## A Backbone collection of Records
|
||||
|
||||
Reference in New Issue
Block a user