[refactor,model,view][s]: add explicit query start, done and fail events for dataset and use them in DataExplorer to generate notifications.

This commit is contained in:
Rufus Pollock
2012-02-24 09:11:17 +00:00
parent 3c9bd368da
commit aaa01b2191
2 changed files with 16 additions and 16 deletions

View File

@@ -39,6 +39,7 @@ my.Dataset = Backbone.Model.extend({
// Resulting DocumentList are used to reset this.currentDocuments and are // Resulting DocumentList are used to reset this.currentDocuments and are
// also returned. // also returned.
query: function(queryObj) { query: function(queryObj) {
this.trigger('query:start');
var self = this; var self = this;
this.queryState.set(queryObj, {silent: true}); this.queryState.set(queryObj, {silent: true});
var dfd = $.Deferred(); var dfd = $.Deferred();
@@ -50,9 +51,11 @@ my.Dataset = Backbone.Model.extend({
return _doc; return _doc;
}); });
self.currentDocuments.reset(docs); self.currentDocuments.reset(docs);
self.trigger('query:done');
dfd.resolve(self.currentDocuments); dfd.resolve(self.currentDocuments);
}) })
.fail(function(arguments) { .fail(function(arguments) {
self.trigger('query:fail', arguments);
dfd.reject(arguments); dfd.reject(arguments);
}); });
return dfd.promise(); return dfd.promise();

View File

@@ -101,33 +101,30 @@ my.DataExplorer = Backbone.View.extend({
this.router = new Backbone.Router(); this.router = new Backbone.Router();
this.setupRouting(); this.setupRouting();
this.model.bind('query:start', function(eventName) {
my.notify('Loading data', {loader: true});
});
this.model.bind('query:done', function(eventName) {
my.clearNotifications();
my.notify('Data loaded', {category: 'success'});
});
this.model.bind('query:fail', function(eventName, error) {
my.clearNotifications();
my.notify(error.message, {category: 'error', persist: true});
});
// retrieve basic data like fields etc // retrieve basic data like fields etc
// note this.model and dataset returned are the same // note this.model and dataset returned are the same
this.model.fetch() this.model.fetch()
.done(function(dataset) { .done(function(dataset) {
self.el.find('.doc-count').text(self.model.docCount || 'Unknown'); self.el.find('.doc-count').text(self.model.docCount || 'Unknown');
self.query(); self.model.query();
}) })
.fail(function(error) { .fail(function(error) {
my.notify(error.message, {category: 'error', persist: true}); my.notify(error.message, {category: 'error', persist: true});
}); });
}, },
// TODO: listen for being query and end query events on the dataset ...
// (This is no longer called by anything ...)
query: function() {
my.notify('Loading data', {loader: true});
this.model.query()
.done(function() {
my.clearNotifications();
my.notify('Data loaded', {category: 'success'});
})
.fail(function(error) {
my.clearNotifications();
my.notify(error.message, {category: 'error', persist: true});
});
},
setReadOnly: function() { setReadOnly: function() {
this.el.addClass('read-only'); this.el.addClass('read-only');
}, },