Skip to content

Commit

Permalink
merge select:one change from dznz
Browse files Browse the repository at this point in the history
  • Loading branch information
Derick Bailey committed Jul 22, 2013
2 parents 7517ce6 + 3cfa864 commit 30f910b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
17 changes: 10 additions & 7 deletions readme.md
Expand Up @@ -185,11 +185,13 @@ The following events are triggered from Selectable models

#### "selected"

Triggers when a model is selected.
Triggers when a model is selected. Provides the selected model
as the first parameter.

#### "deselected"

Triggers when a model is deselected.
Triggers when a model is deselected. Provides the selected model
as the first parameter.

## Picky.SingleSelect

Expand Down Expand Up @@ -292,15 +294,15 @@ myCol.selected; //=> model

### SingleSelect Events

The following events are triggered by the MultiSelect based on changes
The following events are triggered by the SingleSelect based on changes
in selection:

#### "selected"
#### "select:one"

Triggered when a model has been selected. Provides the selected model
as the first parameter.

#### "deselected"
#### "deselect:one"

Triggered when a model has been deselected. Provides the deselected model
as the first parameter.
Expand Down Expand Up @@ -496,9 +498,10 @@ see all of the specs for Backbone.Picky

## Release Notes

### v0.1.1
### v0.2.0

* Pass model as argument in selected / deselected events
* Renamed `SingleSelect` events from "select" and "deselect" to "select:one" and "deselect:one"
* Pass model as argument in select:one / deselect:one events
* Updated the build to use latest grunt and related tools
* Removed reliance on ruby for any part of this project

Expand Down
25 changes: 15 additions & 10 deletions spec/javascripts/singleSelect.spec.js
Expand Up @@ -23,6 +23,7 @@ describe("single select collection", function(){
model = new Model();
collection = new Collection([model]);

spyOn(model, "trigger").andCallThrough();
spyOn(collection, "trigger").andCallThrough();

model.select();
Expand All @@ -33,7 +34,11 @@ describe("single select collection", function(){
});

it("should trigger a selected event", function(){
expect(collection.trigger).toHaveBeenCalledWith("selected", model);
expect(model.trigger).toHaveBeenCalledWith("selected", model);
});

it("should trigger a collection selected event", function(){
expect(collection.trigger).toHaveBeenCalledWith("select:one", model);
});
});

Expand All @@ -55,7 +60,7 @@ describe("single select collection", function(){
});

it("should trigger a selected event", function(){
expect(collection.trigger).toHaveBeenCalledWith("selected", model);
expect(collection.trigger).toHaveBeenCalledWith("select:one", model);
});

it("should tell the model to select itself", function(){
Expand All @@ -78,7 +83,7 @@ describe("single select collection", function(){
});

it("should not trigger a selected event", function(){
expect(collection.trigger).not.toHaveBeenCalledWith("selected", model);
expect(collection.trigger).not.toHaveBeenCalledWith("select:one", model);
});
});

Expand All @@ -102,15 +107,15 @@ describe("single select collection", function(){
});

it("should trigger a selected event", function(){
expect(collection.trigger).toHaveBeenCalledWith("selected", m2);
expect(collection.trigger).toHaveBeenCalledWith("select:one", m2);
});

it("should deselect the first model", function(){
expect(m1.deselect).toHaveBeenCalled();
});

it("should fire a deselect event for the first model", function(){
expect(collection.trigger).toHaveBeenCalledWith("deselected", m1);
expect(collection.trigger).toHaveBeenCalledWith("deselect:one", m1);
});
});

Expand Down Expand Up @@ -148,7 +153,7 @@ describe("single select collection", function(){
});

it("should trigger a deselected event", function(){
expect(collection.trigger).toHaveBeenCalledWith("deselected", model);
expect(collection.trigger).toHaveBeenCalledWith("deselect:one", model);
});
});

Expand Down Expand Up @@ -180,11 +185,11 @@ describe("single select collection", function(){
});

it("should not trigger a deselected event for the selected model", function(){
expect(collection.trigger).not.toHaveBeenCalledWith("deselected", m1);
expect(collection.trigger).not.toHaveBeenCalledWith("deselect:one", m1);
});

it("should not trigger a deselected event for the non-selected model", function(){
expect(collection.trigger).not.toHaveBeenCalledWith("deselected", m2);
expect(collection.trigger).not.toHaveBeenCalledWith("deselect:one", m2);
});
});

Expand Down Expand Up @@ -216,11 +221,11 @@ describe("single select collection", function(){
});

it("should not trigger a deselected event for the selected model", function(){
expect(collection.trigger).not.toHaveBeenCalledWith("deselected", m1);
expect(collection.trigger).not.toHaveBeenCalledWith("deselect:one", m1);
});

it("should not trigger a deselected event for the non-selected model", function(){
expect(collection.trigger).not.toHaveBeenCalledWith("deselected", m2);
expect(collection.trigger).not.toHaveBeenCalledWith("deselect:one", m2);
});
});

Expand Down
4 changes: 3 additions & 1 deletion src/backbone.picky.js
Expand Up @@ -15,14 +15,15 @@ Backbone.Picky = (function (Backbone, _) {
_.extend(Picky.SingleSelect.prototype, {

// Select a model, deselecting any previously
// select model
// selected model
select: function(model){
if (model && this.selected === model) { return; }

this.deselect();

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

// Deselect a model, resulting in no model
Expand All @@ -34,6 +35,7 @@ Backbone.Picky = (function (Backbone, _) {
if (this.selected !== model){ return; }

this.selected.deselect();
this.trigger("deselect:one", this.selected);
delete this.selected;
}

Expand Down

0 comments on commit 30f910b

Please sign in to comment.