Skip to content
Browse files

- Fixed removeAllListeners (described in #103)

- Fixed bug when several models are removed via delete-event
  • Loading branch information...
1 parent 888dda2 commit 617de8bb5b5563cccb37aa5204b49b7cd6cdc197 @jhnns jhnns committed Oct 3, 2012
Showing with 23 additions and 46 deletions.
  1. +7 −5 lib/shared/ModelCollection.class.js
  2. +16 −41 test/shared/ModelCollection.class.test.js
View
12 lib/shared/ModelCollection.class.js
@@ -193,15 +193,17 @@ var ModelCollection = new Class("ModelCollection", {
*/
__addDeleteListener: function (modelToDelete) {
- var Super = this.Super,
- models = Super._getElements(),
- filteredModels,
- modelToDeleteIndex;
+ var Super = this.Super;
function onDeleteListener(err) {
+ var filteredModels,
+ models,
+ modelToDeleteIndex;
+
if(!err) {
+ models = Super._getElements();
filteredModels = _(models).filter(function filterModels(model, index) {
if (modelToDelete === model) {
modelToDeleteIndex = index;
@@ -219,7 +221,7 @@ var ModelCollection = new Class("ModelCollection", {
}
//Clean in any case the listener for delete.
- modelToDelete.removeAllListeners("delete", onDeleteListener);
@topa
Peerigon member
topa added a note Oct 4, 2012

@jhnns thx for the fix. I think it was an IDE completion fail of mine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ modelToDelete.removeListener("delete", onDeleteListener);
}
View
57 test/shared/ModelCollection.class.test.js
@@ -341,60 +341,35 @@ describe("ModelCollection", function () {
describe("on Model.delete()", function () {
- var octocatService;
-
- // monkey-patch environment
- before(function () {
-
- environment.isServer = function() { return false; };
- environment.isClient = function() { return true; };
-
- });
-
beforeEach(function () {
-
- octocatService = {
- delete: function (remote, ids, onDelete) {
- onDelete( { status: "success" } );
- }
- };
-
- octocatModel.setService(octocatService);
-
- modelCollection.push(octocatModels);
- });
-
- // revert monkey-patch
- after(function () {
-
- environment.isServer = function() { return true; };
- environment.isClient = function() { return false; };
-
+ modelCollection.push(octocatModels);
});
it("should emit 'remove'-Event if a Model was deleted and pass the Model", function (done) {
- modelCollection.on("remove", function checkPassedParam(models) {
+ modelCollection.once("remove", function checkPassedParam(models) {
expect(octocatModel).to.equal(models[0]);
- done();
});
- octocatModel.delete(function onDelete(err) {
- if (err) throw err;
+ octocatModel.emit("delete"); // faking delete, we just emit the event.
+
+ modelCollection.once("remove", function checkPassedParam(models) {
+ expect(octocatModels[1]).to.equal(models[0]);
+ done();
});
- });
+ octocatModels[1].emit("delete"); // faking delete, we just emit the event.
- it("it should remove the Model from Collection if it was deleted", function (done) {
+ });
- var previousCollectionLength = modelCollection.size(),
- expectedCollectionLength = previousCollectionLength - 1;
+ it("it should remove the Model from Collection if it was deleted", function () {
- octocatModel.delete(function onDelete(err) {
- if (err) throw err;
- expect(modelCollection.size()).to.equal(expectedCollectionLength);
- done();
- });
+ octocatModels[0].emit("delete"); // faking delete, we just emit the event.
+ expect(modelCollection.toArray()).to.eql([octocatModels[1], octocatModels[2]]);
+ octocatModels[1].emit("delete");
+ expect(modelCollection.toArray()).to.eql([octocatModels[2]]);
+ octocatModels[2].emit("delete");
+ expect(modelCollection.toArray()).to.eql([]);
});

0 comments on commit 617de8b

Please sign in to comment.
Something went wrong with that request. Please try again.