Skip to content

Commit

Permalink
sg-dataprovider: Return Promise from get()
Browse files Browse the repository at this point in the history
closes #3
  • Loading branch information
Vladimir Varankin committed Dec 6, 2015
1 parent 3c9db49 commit 0675cfb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
8 changes: 4 additions & 4 deletions blocks/sg-datalist/sg-datalist.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ provide(BemDom.decl(this.name, {
throw new Error('dataprovider can\'t be set twice');
}
this._dataprovider = dataprovider.on({
'items' : this._onProviderGotItems,
'data' : this._onProviderGotData,
'error' : this._onProviderGotError
}, this);
},

requestData : function(params) {
this.getDataProvider().get(params);
this.getDataProvider().get(params).done();
return this;
},

Expand Down Expand Up @@ -110,10 +110,10 @@ provide(BemDom.decl(this.name, {
this.emit('item-click', data);
},

_onProviderGotItems : function(e, data) {
_onProviderGotData : function(e, data) {
this
.emit('items', data)
._updateMenu(data.items);
._updateMenu(data.result);
},

_onProviderGotError : function(e, data) {
Expand Down
2 changes: 1 addition & 1 deletion blocks/sg-dataprovider/sg-dataprovider.deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
shouldDeps : [
'inherit',
'events',
'next-tick'
'vow'
]
})
32 changes: 13 additions & 19 deletions blocks/sg-dataprovider/sg-dataprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,33 @@

modules.define(
'sg-dataprovider',
['inherit', 'events', 'next-tick'],
function(provide, inherit, events, nextTick) {
['inherit', 'events', 'vow'],
function(provide, inherit, events, vow) {

provide(/** @exports */ inherit(events.Emitter, {
__constructor : function() {
this.__base.apply(this, arguments);
this._onGotData = this._onGotData.bind(this);
},

/**
* @param {Object} params
* @returns {Object} this
* @returns {vow.Promise}
*/
get : function(params) {
// TODO: return Promise
this._getData(params, this._onGotData);
return this;
return vow.fulfill(this._getData(params)).always(this._onGotData, this);
},

/**
* @param {Object} params
* @param {Function} callback
* @returns {vow.Promise}
* @abstract
*/
_getData : function(params, callback) {
callback(new Error('not implemented'));
_getData : function(params) {
return vow.reject(new Error('not implemented'));
},

_onGotData : function(err, data) {
var _this = this;
nextTick(function() {
err ? _this.emit('error', err) : _this.emit('items', { items : data });
})
_onGotData : function(promise) {
var res = promise.valueOf();
promise.isRejected()?
this.emit('error', res) :
this.emit('data', { result : res });
return promise;
}
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
shouldDeps : [
{ elem : 'storage' },
'inherit',
'vow',
'sg-dataprovider'
]
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
modules.define(
'timezone-provider',
['inherit', 'sg-dataprovider', 'timezone-provider__storage'],
function(provide, inherit, DataProvider, TzStorage) {
['inherit', 'vow', 'sg-dataprovider', 'timezone-provider__storage'],
function(provide, inherit, vow, DataProvider, TzStorage) {

provide(inherit(DataProvider, {
__constructor : function(data) {
Expand All @@ -10,13 +10,19 @@ provide(inherit(DataProvider, {
},

/** @override */
_getData : function(params, callback) {
var _this = this,
query = this._buildQuery(params.val);
_getData : function(params) {
var query = this._buildQuery(params.val),
deferred = vow.defer();

if(query) {
this._storage.find(query, callback);
this._storage.find(query, function(err, data) {
err? deferred.reject(err) : deferred.resolve(data);
});
} else {
deferred.resolve([]);
}

return deferred.promise();
},

_buildQuery : function(val) {
Expand Down

0 comments on commit 0675cfb

Please sign in to comment.