Skip to content

Commit

Permalink
fix: not sending the correct value to before:event handlers in Storag…
Browse files Browse the repository at this point in the history
…e class
  • Loading branch information
emaphp committed Mar 21, 2015
1 parent 31ca3f9 commit 8e870bd
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 23 deletions.
35 changes: 14 additions & 21 deletions dist/backbone.async.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,39 +135,35 @@
if (!this.Collection)
throw new Error('No Collection class defined');

options = options || {};
var self = this;
var mustTrigger = (typeof(options.silent) === 'undefined') || !options.silent;

return new Promise(function(resolve, reject) {
self.collection = new self.Collection();
if (mustTrigger)
self.trigger('before:fetch', self.collection, options);
self.collection.fetch(options || {})
if (!self.collection) self.collection = new self.Collection();
if (mustTrigger) self.trigger('before:fetch', { collection: self.collection, options: options});
self.collection.fetch(options)
.then(function(data) {
if (mustTrigger)
self.trigger('after:fetch', data, true);
if (mustTrigger) self.trigger('after:fetch', data, true);
self.isLoaded = true;
resolve(data);
})
.catch(function(data) {
if (mustTrigger)
self.trigger('after:fetch', data, false);
if (mustTrigger) self.trigger('after:fetch', data, false);
reject(data);
});
});
},

get: function(id, options) {
if (this.isLoaded) {
if (this.collection) {
var value = this.collection.get(id);
var data = {
model: value,
options: options
};

if (!value)
return Promise.reject(data);

if (!value) return Promise.reject(data);
return Promise.resolve(data);
}

Expand All @@ -177,26 +173,23 @@
if (!this.Collection)
throw new Error('No Collection class defined');

options = options || {};
var self = this;
var mustTrigger = (typeof(options.silent) === 'undefined') || !options.silent;

return new Promise(function(resolve, reject) {
var model = new self.Model();
model.set(self.Model.idAttribute || 'id', id);
if (mustTrigger)
self.trigger('before:get', model, options);
model.fetch(options || {})
if (mustTrigger) self.trigger('before:get', { model: model, options: options });
model.fetch(options)
.then(function(data) {
if (mustTrigger)
self.trigger('after:get', data, true);
if (!self.collection)
self.collection = new self.Collection();
if (mustTrigger) self.trigger('after:get', data, true);
if (!self.collection) self.collection = new self.Collection();
self.collection.push(model);
resolve(data);
})
.catch(function(data) {
if (mustTrigger)
self.trigger('after:get', data, false);
if (mustTrigger) self.trigger('after:get', data, false);
reject(data);
});
});
Expand Down
2 changes: 1 addition & 1 deletion dist/backbone.async.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

178 changes: 177 additions & 1 deletion test/specs/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("ASync.Storage tests", function() {
server.restore();
});

describe('Fetch test', function() {
describe('Fetch by ID tests', function() {
it('must fetch by id', function(done) {
var value = { id: 1, name: "Emmanuel", surname: "Antico"};
server.respondWith(
Expand All @@ -25,6 +25,7 @@ describe("ASync.Storage tests", function() {
.then(function(data) {
expect(data).to.be.a('object');
expect(data).to.have.property('model');
expect(data).to.have.property('response');
expect(data).to.have.property('options');

expect(data.model.attributes).to.deep.equal(value);
Expand All @@ -34,6 +35,8 @@ describe("ASync.Storage tests", function() {
expect(model.attributes).to.deep.equal(value);
expect(storage.isLoaded).to.be.false;

expect(data.response).to.deep.equal(value);

expect(data.options).to.be.a('object');
expect(data.options).to.have.property('test');
expect(data.options).to.have.property('silent');
Expand All @@ -48,6 +51,179 @@ describe("ASync.Storage tests", function() {
server.respond();
});

it('must invoke callbacks in order', function(done) {
var value = { id: 2, name: "Emmanuel", surname: "Antico"};
server.respondWith(
'GET',
'/contacts/2',
[
200,
{"Content-Type": "application/json"},
JSON.stringify(value)
]
);

var callback1 = sinon.spy();
var callback2 = sinon.spy();

var storage = new FIXTURES.ContactsStorage();
storage.get(2)
.then(callback1)
.then(callback2)
.then(function(data) {
expect(callback1.called).to.be.true;
expect(callback1.calledOnce).to.be.true;
expect(callback2.called).to.be.true;
expect(callback2.calledOnce).to.be.true;
expect(callback1.calledBefore(callback2)).to.be.true;

done();
})
.catch(function(err) { done(err); });

server.respond();
});

it('must call event handlers', function(done) {
var value = { id: 3, name: "Emmanuel", surname: "Antico"};
server.respondWith(
'GET',
'/contacts/3',
[
200,
{"Content-Type": "application/json"},
JSON.stringify(value)
]
);

var storage = new FIXTURES.ContactsStorage();
var obj = _.extend({}, Backbone.Events);
var beforeCallback = sinon.spy();
var afterCallback = sinon.spy();
obj.listenTo(storage, 'before:get', beforeCallback);
obj.listenTo(storage, 'after:get', afterCallback);

storage.get(3, {test: true, silent:false})
.then(function(data) {
expect(beforeCallback.called).to.be.true;
expect(afterCallback.called).to.be.true;
expect(beforeCallback.calledBefore(afterCallback)).to.be.true;

var dataArg = beforeCallback.args[0][0];
expect(dataArg).to.be.a('object');
expect(dataArg).to.have.property('model');
expect(dataArg).to.have.property('options');
expect(dataArg.options).to.have.property('test');
expect(dataArg.options).to.have.property('silent');
expect(dataArg.options.test).to.be.true;
expect(dataArg.options.silent).to.be.false;

dataArg = afterCallback.args[0][0];
expect(dataArg).to.be.a('object');
expect(dataArg).to.have.property('model');
expect(dataArg).to.have.property('options');
expect(dataArg).to.have.property('response');
expect(dataArg.response).to.deep.equal(value);
expect(dataArg.model.attributes).to.be.deep.equal(value);

expect(dataArg.options).to.have.property('test');
expect(dataArg.options).to.have.property('silent');
expect(dataArg.options.test).to.be.true;
expect(dataArg.options.silent).to.be.false;

var success = afterCallback.args[0][1];
expect(success).to.be.true;
done();
})
.catch(function(err) { done(err); });

server.respond();
});

it('must not call event handlers', function(done) {
var value = { id: 4, name: "Emmanuel", surname: "Antico"};
server.respondWith(
'GET',
'/contacts/4',
[
200,
{"Content-Type": "application/json"},
JSON.stringify(value)
]
);

var storage = new FIXTURES.ContactsStorage();
var obj = _.extend({}, Backbone.Events);
var beforeCallback = sinon.spy();
var afterCallback = sinon.spy();
obj.listenTo(storage, 'before:get', beforeCallback);
obj.listenTo(storage, 'after:get', afterCallback);

storage.get(4, {test: false, silent: true})
.then(function(data) {
expect(beforeCallback.called).to.be.false;
expect(afterCallback.called).to.be.false;
done();
})
.catch(function(err) { done(err); });

server.respond();
});

it('must return previous object', function(done) {
var value = { id: 5, name: "Emmanuel", surname: "Antico"};
server.respondWith(
'GET',
'/contacts/5',
[
200,
{"Content-Type": "application/json"},
JSON.stringify(value)
]
);

var storage = new FIXTURES.ContactsStorage();
storage.get(5)
.then(function(data) {
var obj = _.extend({}, Backbone.Events);
var beforeCallback = sinon.spy();
var afterCallback = sinon.spy();
obj.listenTo(storage, 'before:get', beforeCallback);
obj.listenTo(storage, 'after:get', afterCallback);

storage.get(5, {test: true, silent: false})
.then(function(data) {
expect(data).to.be.a('object');
expect(data).to.have.property('model');
expect(data).to.not.have.property('response');
expect(data).to.have.property('options');

expect(beforeCallback.called).to.be.false;
expect(afterCallback.called).to.be.false;

expect(storage.collection.models.length).to.equal(1);
var model = storage.collection.get(5);
expect(model.attributes).to.deep.equal(value);
expect(storage.isLoaded).to.be.false;

expect(data.options).to.be.a('object');
expect(data.options).to.have.property('test');
expect(data.options).to.have.property('silent');

expect(data.options.test).to.be.true;
expect(data.options.silent).to.be.false;

done();
})
.catch(function(err) { done(err); });
})
.catch(function(err) { done(err); });

server.respond();
});
});

describe('Fetch all tests', function() {
it('must fetch collection', function(done) {
var values = [
{id: 1, name: 'Curly'},
Expand Down

0 comments on commit 8e870bd

Please sign in to comment.