Skip to content

Commit

Permalink
Changed event names so that the SingleSelect and the Selectable event…
Browse files Browse the repository at this point in the history
…s would not have the same name.

Reordered the flow of selection and deselection so that the events come in a logical order.
  • Loading branch information
BorisKozo committed Jan 29, 2013
1 parent e4addef commit 1a952ce
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 199 deletions.
35 changes: 20 additions & 15 deletions lib/amd/backbone.picky.js
Expand Up @@ -92,20 +92,23 @@

this.selected = model;
this.selected.select();
this.trigger("selected", model);
this.trigger("collection:selected", model);
},

// Deselect a model, resulting in no model
// being selected
deselect: function (model) {
var selected;

if (!this.selected) { return; }

model = model || this.selected;
if (this.selected !== model) { return; }

this.selected.deselect();
this.trigger("deselected", this.selected);
selected = this.selected;
delete this.selected;
this.trigger("collection:deselected", selected);
selected.deselect();
},

//Finds the first selected model in the collection and selects it,
Expand Down Expand Up @@ -243,31 +246,33 @@
select: function (options) {
if (this.selected) { return; }

if (this.collection && _.isFunction(this.collection.select)) {
this.collection.select(this);
}

this.selected = true;

if (options && options.silent) { return; }

this.trigger("selected");
this.trigger("model:selected");

if (this.collection) {
this.collection.select(this);
}
},

// Deselect this model, and tell our
// collection that we're deselected
deselect: function (options) {
if (!this.selected) { return; }

if (this.collection && _.isFunction(this.collection.deselect)) {
this.collection.deselect(this);
}

this.selected = false;

if (options && options.silent) { return; }

this.trigger("deselected");
this.trigger("model:deselected");

if (this.collection) {
this.collection.deselect(this);
}
},

// Change selected to the opposite of what
Expand Down Expand Up @@ -304,17 +309,17 @@
length = collection.length;

if (selectedLength === length) {
collection.trigger("selected:all", collection);
collection.trigger("collection:selected:all", collection);
return;
}

if (selectedLength === 0) {
collection.trigger("selected:none", collection);
collection.trigger("collection:selected:none", collection);
return;
}

if (selectedLength > 0 && selectedLength < length) {
collection.trigger("selected:some", collection);
collection.trigger("collection:selected:some", collection);
return;
}
},
Expand All @@ -333,7 +338,7 @@
}
});
if (collection.selected) {
collection.trigger("selected", collection.selected);
collection.trigger("collection:selected", collection.selected);
}
},

Expand Down
2 changes: 1 addition & 1 deletion lib/amd/backbone.picky.min.js

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

35 changes: 20 additions & 15 deletions lib/backbone.picky.js
Expand Up @@ -86,20 +86,23 @@ Backbone.Picky = (function (Backbone, _) {

this.selected = model;
this.selected.select();
this.trigger("selected", model);
this.trigger("collection:selected", model);
},

// Deselect a model, resulting in no model
// being selected
deselect: function (model) {
var selected;

if (!this.selected) { return; }

model = model || this.selected;
if (this.selected !== model) { return; }

this.selected.deselect();
this.trigger("deselected", this.selected);
selected = this.selected;
delete this.selected;
this.trigger("collection:deselected", selected);
selected.deselect();
},

//Finds the first selected model in the collection and selects it,
Expand Down Expand Up @@ -237,31 +240,33 @@ Backbone.Picky = (function (Backbone, _) {
select: function (options) {
if (this.selected) { return; }

if (this.collection && _.isFunction(this.collection.select)) {
this.collection.select(this);
}

this.selected = true;

if (options && options.silent) { return; }

this.trigger("selected");
this.trigger("model:selected");

if (this.collection) {
this.collection.select(this);
}
},

// Deselect this model, and tell our
// collection that we're deselected
deselect: function (options) {
if (!this.selected) { return; }

if (this.collection && _.isFunction(this.collection.deselect)) {
this.collection.deselect(this);
}

this.selected = false;

if (options && options.silent) { return; }

this.trigger("deselected");
this.trigger("model:deselected");

if (this.collection) {
this.collection.deselect(this);
}
},

// Change selected to the opposite of what
Expand Down Expand Up @@ -298,17 +303,17 @@ Backbone.Picky = (function (Backbone, _) {
length = collection.length;

if (selectedLength === length) {
collection.trigger("selected:all", collection);
collection.trigger("collection:selected:all", collection);
return;
}

if (selectedLength === 0) {
collection.trigger("selected:none", collection);
collection.trigger("collection:selected:none", collection);
return;
}

if (selectedLength > 0 && selectedLength < length) {
collection.trigger("selected:some", collection);
collection.trigger("collection:selected:some", collection);
return;
}
},
Expand All @@ -327,7 +332,7 @@ Backbone.Picky = (function (Backbone, _) {
}
});
if (collection.selected) {
collection.trigger("selected", collection.selected);
collection.trigger("collection:selected", collection.selected);
}
},

Expand Down
2 changes: 1 addition & 1 deletion lib/backbone.picky.min.js

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

0 comments on commit 1a952ce

Please sign in to comment.