[model][m]: working Webstore Backend (fixes #8).

This commit is contained in:
rgrp
2011-11-07 21:52:07 +00:00
parent 39ab2232a1
commit 5eb06b982e
2 changed files with 175 additions and 5 deletions

View File

@@ -20,6 +20,13 @@ recline.DocumentList = Backbone.Collection.extend({
model: recline.Document
})
// Backends section
// ================
recline.setBackend = function(backend) {
Backbone.sync = backend.sync;
};
// Backend which just caches in memory
//
// Does not need to be a backbone model but provides some conveience
@@ -73,7 +80,68 @@ recline.BackendMemory = Backbone.Model.extend({
}
});
recline.setBackend = function(backend) {
Backbone.sync = backend.sync;
};
// Webstore Backend for connecting to the Webstore
//
// Designed to only attached to only dataset and one dataset only ...
// Could generalize to support attaching to different datasets
recline.BackendWebstore = Backbone.Model.extend({
// require url attribute in initialization data
initialize: function() {
this.webstoreTableUrl = this.get('url');
},
getDataset: function(id) {
var dataset = new recline.Dataset({
id: id
});
dataset.backend = this;
return dataset;
},
sync: function(method, model, options) {
if (method === "read") {
// this switching on object type is rather horrible
// think may make more sense to do work in individual objects rather than in central Backbone.sync
if (this.__type__ == 'Dataset') {
// get the schema and return
var base = this.backend.get('url');
var schemaUrl = base + '/schema.json';
var jqxhr = $.ajax({
url: schemaUrl,
dataType: 'jsonp',
jsonp: '_callback'
});
var dfd = $.Deferred();
jqxhr.then(function(schema) {
headers = _.map(schema.data, function(item) {
return item.name;
});
dataset.set({
headers: headers
});
dataset.rowCount = schema.count;
dfd.resolve(dataset, jqxhr);
});
return dfd.promise();
}
}
},
getRows: function(datasetId, numRows, start) {
if (start === undefined) {
start = 0;
}
if (numRows === undefined) {
numRows = 10;
}
var base = this.get('url');
var jqxhr = $.ajax({
url: base + '.json',
dataType: 'jsonp',
jsonp: '_callback',
cache: true
});
var dfd = $.Deferred();
jqxhr.then(function(results) {
dfd.resolve(results.data);
});
return dfd.promise();
}
});