Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial release

  • Loading branch information...
commit d6745623db95993b761a20b46b45aa29ac235ead 0 parents
@derickbailey authored
Showing with 367 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. +208 −0 readme.md
  3. +152 −0 src/backbone.picky.js
7 .gitignore
@@ -0,0 +1,7 @@
+.DS_Store
+*.swp
+*.swo
+*.orig
+tmp/
+ext/
+node_modules
208 readme.md
@@ -0,0 +1,208 @@
+# Backbone.Picky
+
+Selectable entities as mixins for Backbone.Model and Backbone.Collection!
+
+#### NOTE: THIS CODE IS UNTESTED
+
+I hacked this together over the last hour, while building out some functionality
+for a client project. I have not written any automated tests for this
+project specifically. I've tested this through my client project only,
+which means
+
+**THIS CODE IS UNTESTED. USE AT YOUR OWN RISK**
+
+## Picky.Selectable
+
+Creates selectable capabilities for a model, including tracking whether or
+not the model is selected, and raising events when selection changes.
+
+```js
+var selectable = new Backbone.Picky.Selectable(myModel);
+```
+
+### Basic Usage
+
+Extend your model with the `Selectable` instance to make your model
+selectable directly.
+
+```js
+SelectableModel = Backbone.Model.extend({
+ initialize: function(){
+ var selectable = new Backbone.Picky.Selectable(this);
+ _.extend(this, selectable);
+ }
+});
+```
+
+### Selectable Methods
+
+The following methods are included in the `Selectable` object
+
+#### Selectable#select
+
+Select a model, setting the model's `selected` attribute to true and
+triggering a "selected" event.
+
+```js
+var myModel = new SelectableModel();
+
+myModel.on("select", function(){
+ console.log("I'm selected!");
+});
+
+myModel.select(); //=> logs "I'm selected!"
+myModel.selected; //=> true
+```
+#### Selectable#deselect
+
+Select a model, setting the model's `selected` attribute to true and
+triggering a "selected" event.
+
+```js
+var myModel = new SelectableModel();
+
+myModel.on("deselect", function(){
+ console.log("I'm no longer selected!");
+});
+
+// must select it before it can be deselected
+myModel.select();
+
+myModel.deselect(); //=> logs "I'm no longer selected!";
+myModel.selected; //=> true
+```
+
+#### Selectable#toggleSelected
+
+Toggles the selection state between selected and deselected by calling
+the `select` or `deselect` method appropriately.
+
+```js
+var myModel = new SelectableModel();
+
+myModel.on("select", function(){
+ console.log("I'm selected!");
+});
+
+myModel.on("deselect", function(){
+ console.log("I'm no longer selected!");
+});
+
+// toggle selection
+myModel.toggleSelected(); //=> "I'm selected!"
+myModel.toggleSelected(); //=> "I'm no longer selected!"
+```
+
+## Picky.MultiSelect
+
+Creates multi-select capabilities for a `Backbone.Collection`, including
+select all, select none and select some features.
+
+```js
+var multiSelect = new Backbone.Picky.MultiSelect(myCollection) ;
+```
+
+### Model Requirements
+
+Your model for the collection must implement the following API to be
+usable by a `MultiSelect` collection:
+
+* `select: function(){...}`
+* `deselect: function(){...}`
+
+The easiest way to do this is to have your model extend `Selectable` as
+shown above.
+
+### Model and Collection Interactions
+
+If you implement a `Selectable` model, the methods on the models and the
+`MultiSelect` collection will keep each other in sync. That is, if you
+call `model.select()` on a model, the collection will be notified of the
+model being selected and it will correctly update the `selectedLength` and
+fire the correct events.
+
+### Basic Usage
+
+Extend your collection with the `MultiSleect` instance to make your
+collection support multiple selections directly.
+
+```js
+
+SelectableModel = Backbone.Model.extend({
+ initialize: function(){
+ var selectable = new Backbone.Picky.Selectable(this);
+ _.extend(this, selectable);
+ }
+});
+
+MultiCollection = Backbone.Collection.extend({
+
+ model: SelectableModel,
+
+ initialize: function(){
+ var multiSelect = new Backbone.Picky.MultiSelect(this);
+ _.extend(this, multiSelect);
+ }
+});
+```
+### MultiSelect Methods
+
+The following methods are provided by the `MultiSelect` object
+
+#### select(model)
+
+#### deselect(model)
+
+#### selectAll
+
+#### deselectAll
+
+#### toggleSelectAll
+
+### MultiSelect Attributes
+
+The following attribute is set by the multi-select automatically
+
+### selected
+
+Returns a hash of selected models, keyed from the model `cid`.
+
+#### selectedLength
+
+Returns the number of items in the collection that are selected.
+
+### MultiSelect Events
+
+The following events are triggered by the MultiSelect based on changes
+in selection:
+
+#### "select:all"
+
+Triggered when all models have been selected
+
+#### "select:none"
+
+Triggered when all models have been deselected
+
+#### "select:some"
+
+Triggered when at least 1 model is selected, but less than all models have
+been selected
+
+## Release Notes
+
+### v0.0.1
+
+* Initial release of untested code
+* Basic "Selectable" mixin for models
+* Basic "MultiSelect" mixin for collections
+
+## Legal Mumbo Jumbo (MIT License)
+
+Copyright (c) 2012 Derick Bailey, Muted Solutions, LLC
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
152 src/backbone.picky.js
@@ -0,0 +1,152 @@
+// Backbone.Picky - v0.0.1
+// Copyright (C)2012 Derick Bailey, Muted Solutions, LLC. All Rights Reserved.
+//
+// Distributed under MIT license.
+// http://github.com/derickbailey/backbone.picky
+
+Backbone.Picky = (function (Backbone, _) {
+ var Picky = {};
+
+ // Picky.SingleSelect
+ // ------------------
+
+ // Coming Soon!
+
+ // Picky.MultiSelect
+ // -----------------
+ // A mult-select mixin for Backbone.Collection, allowing a collection to
+ // have multiple items selected, including `selectAll` and `selectNone`
+ // capabilities.
+
+ Picky.MultiSelect = function (collection) {
+ this.collection = collection;
+ this.selected = {};
+ }
+
+ _.extend(Picky.MultiSelect.prototype, {
+
+ // Select a specified model, make sure the
+ // model knows it's selected, and hold on to
+ // the selected model.
+ select: function (model) {
+ if (this.selected[model.cid]) { return; }
+
+ this.selected[model.cid] = model;
+ model.select();
+ calculateSelectedLength(this);
+ },
+
+ // Deselect a specified model, make sure the
+ // model knows it has been deselected, and remove
+ // the model from the selected list.
+ deselect: function (model) {
+ if (!this.selected[model.cid]) { return; }
+
+ delete this.selected[model.cid];
+ model.deselect();
+ calculateSelectedLength(this);
+ },
+
+ // Select all models in this collection
+ selectAll: function () {
+ this.each(function (model) { model.select(); });
+ calculateSelectedLength(this);
+ },
+
+ // Deselect all models in this collection
+ selectNone: function () {
+ if (this.selectedLength === 0) { return; }
+ this.each(function (model) { model.deselect(); });
+ calculateSelectedLength(this);
+ },
+
+ // Toggle select all / none. If some are selected, it
+ // will select all. If all are selected, it will select
+ // none. If none are selected, it will select all.
+ toggleSelectAll: function () {
+ if (this.selectedLength === this.length) {
+ this.selectNone();
+ } else {
+ this.selectAll();
+ }
+ }
+ });
+
+ // Picky.Selectable
+ // ----------------
+ // A selectable mixin for Backbone.Model, allowing a model to be selected,
+ // enabling it to work with Picky.MultiSelect or on it's own
+
+ Picky.Selectable = function (model) {
+ this.model = model;
+ }
+
+ _.extend(Picky.Selectable.prototype, {
+
+ // Select this model, and tell our
+ // collection that we're selected
+ select: function () {
+ if (this.selected) { return; }
+
+ this.selected = true;
+ this.trigger("selected");
+
+ if (this.collection) {
+ this.collection.select(this);
+ }
+ },
+
+ // Deselect this model, and tell our
+ // collection that we're deselected
+ deselect: function () {
+ if (!this.selected) { return; }
+
+ this.selected = false;
+ this.trigger("deselected");
+
+ if (this.collection) {
+ this.collection.deselect(this);
+ }
+ },
+
+ // Change selected to the opposite of what
+ // it currently is
+ toggleSelected: function () {
+ if (this.selected) {
+ this.deselect();
+ } else {
+ this.select();
+ }
+ }
+ });
+
+ // Helper Methods
+ // --------------
+
+ // Calculate the number of selected items in a collection
+ // and update the collection with that length. Trigger events
+ // from the collection based on the number of selected items.
+ var calculateSelectedLength = function (collection) {
+ collection.selectedLength = _.size(collection.selected);
+
+ var selectedLength = collection.selectedLength;
+ var length = collection.length;
+
+ if (selectedLength === length) {
+ collection.trigger("select:all", collection);
+ return;
+ }
+
+ if (selectedLength === 0) {
+ collection.trigger("select:none", collection);
+ return;
+ }
+
+ if (selectedLength > 0 && selectedLength < length) {
+ collection.trigger("select:some", collection);
+ return;
+ }
+ };
+
+ return Picky;
+})(Backbone, _);
Please sign in to comment.
Something went wrong with that request. Please try again.