• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    JavaScript
  • Created over 12 years ago
  • Updated about 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

selectable entities as mixins for Backbone.Model and Backbone.Collection

Backbone.Picky

Selectable entities as mixins for Backbone.Model and Backbone.Collection!

DEPRECATION NOTICE

This project is no longer maintained and no recommended for production use.

If you need selectable Backbone models, check out Backbone.select.

Source Code And Downloads

You can download the raw source code from the "src" folder above, or grab one of the builds from the "lib" folder.

To get the latest stable release, use these links which point to the 'master' branch's builds:

Standard Builds

Development: backbone.picky.js

Production: backbone.picky.min.js

AMD/RequireJS Builds

Development: backbone.picky.js

Production: backbone.picky.min.js

Documentation

This readme file contains basic usage examples and details on the full API, including methods, attributes and events.

Annotated Source Code

Picky has annotated source code using the Docco tool to turn comments in to documentation. This provides an in-depth look at what each section of is doing.

View The Annotated Source Code

Method Name Overrides

IMPORTANT NOTE ABOUT METHOD NAME "select"

The Picky collections override the method select on collections. At this point, I can't think of a better name for specifying a model has been selected. Once I find a better name, the API will change. But for now, you will not be able to use the standard select method on any collection that has a Picky collection mixin.

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.

Therefore, the following are functionally the same:

model = new MyModel();
col = new MyCollection([model]);

model.select();
model = new MyModel();
col = new MyCollection([model]);

col.select(model);

Model Requirements For Picky Collections

Your model for a Picky collection must implement the following API to be usable by the selection methods and functionality:

  • select: function(){...}
  • deselect: function(){...}

The easiest way to do this is to have your model extend Selectable. You can, however, implement your own version of these methods.

Backbone.Picky's Components:

  • Picky.Selectable: Creates select / deselect capabilities for a model
  • Picky.MultiSelect: Allows a collection to know about the selection of multiple models, including select all / deselect all
  • Picky.SingleSelect: Allow a collection to have an exclusively selected model

Picky.Selectable

Creates selectable capabilities for a model, including tracking whether or not the model is selected, and raising events when selection changes.

var selectable = new Backbone.Picky.Selectable(myModel);

Basic Usage

Extend your model with the Selectable instance to make your model selectable directly.

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 "select" event.

var myModel = new SelectableModel();

myModel.on("select", function(){
  console.log("I'm selected!");
});

myModel.select(); //=> logs "I'm selected!"
myModel.selected; //=> true

Selectable#deselect

Deselect a model, setting the model's selected attribute to false and triggering a "deselected" event.

var myModel = new SelectableModel();

myModel.on("deselected", 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; //=> false

Selectable#toggleSelected

Toggles the selection state between selected and deselected by calling the select or deselect method appropriately.

var myModel = new SelectableModel();

myModel.on("select", function(){
  console.log("I'm selected!");
});

myModel.on("deselected", function(){
  console.log("I'm no longer selected!");
});

// toggle selection
myModel.toggleSelected(); //=> "I'm selected!"
myModel.toggleSelected(); //=> "I'm no longer selected!"

Selectable Attributes

The following attributes are manipulated by the Selectable object

Selectable#selected

Returns a boolean value indicating whether or not the model is currently selected.

Selectable Events

The following events are triggered from Selectable models

"selected"

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

"deselected"

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

Picky.SingleSelect

Creates single-select capabilities for a Backbone.Collection, allowing a single model to be exclusively selected within the colllection. Selecting another model will cause the first one to be deselected.

var singleSelect = new Backbone.Picky.SingleSelect(myCollection) ;

Basic Usage

Extend your collection with the SingleSelect instance to make your collection support exclusive selections directly.

SelectableModel = Backbone.Model.extend({
  initialize: function(){
    var selectable = new Backbone.Picky.Selectable(this);
    _.extend(this, selectable);
  }
});

SingleCollection = Backbone.Collection.extend({
  model: SelectableModel,

  initialize: function(){
    var singleSelect = new Backbone.Picky.SingleSelect(this);
    _.extend(this, singleSelect);
  }
});

SingleSelect Methods

The following methods are provided by the SingleSelect object.

SingleSelect#select(model)

Select a model. This method will store the selected model in the collection's selected attribute, and call the model's select method to ensure the model knows it has been selected.

myModel = new SelectableModel();
myCol = new MultiCollection();
myCol.select(myModel);

Or

myModel = new SelectableModel();
myCol = new MultiCollection([myModel]);
myModel.select();

If the model is already selected, this is a no-op. If a previous model is already selected, the previous model will be deselected.

SingleSelect#deselect(model)

Deselect the currently selected model. This method will remove the model from the collection's selected attribute, and call the model's deselect method to ensure the model knows it has been deselected.

myModel = new SelectableModel();
myCol = new MultiCollection();
myCol.deselect(myModel);

Or

myModel = new SelectableModel();
myCol = new MultiCollection();
myModel.deselect();

If the model is not currently selected, this is a no-op. If you try to deselect a model that is not the currently selected model, the actual selected model will not be deselected.

SingleSelect Attributes

The following attribute is set by the multi-select automatically

SingleSelect#selected

Returns the one selected model for this collection

myCol = new MultiCollection();
myCol.select(model);

myCol.selected; //=> model

SingleSelect Events

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

"select:one"

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

"deselect:one"

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

This fires whether deselect has been called explicitly, or the selection is being replace through another call to select.

Picky.MultiSelect

Creates multi-select capabilities for a Backbone.Collection, including select all, select none and select some features.

var multiSelect = new Backbone.Picky.MultiSelect(myCollection) ;

Basic Usage

Extend your collection with the MultiSleect instance to make your collection support multiple selections directly.

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

MultiSelect#select(model)

Select a model. This method will store the selected model in the collection's selected list, and call the model's select method to ensure the model knows it has been selected.

myCol = new MultiCollection();

myCol.select(myModel);

If the model is already selected, this is a no-op.

MultiSelect#deselect(model)

Deselect a model. This method will remove the model from the collection's selected list, and call the model's deselect method to ensure the model knows it has been deselected.

myCol = new MultiCollection();

myCol.deselect(myModel);

If the model is not currently selected, this is a no-op.

MultiSelect#selectAll

Select all models in the collection.

myCol = new MultiCollection();

myCol.selectAll();

Models that are already selected will not be re-selected. Models that are not currently selected will be selected. The end result will be all models in the collection are selected.

MultiSelect#deselectAll

Deselect all models in the collection.

myCol = new MultiCollection();

myCol.deselectAll();

Models that are selected will be deselected. Models that are not selected will not be deselected again. The end result will be no models in the collection are selected.

MultiSelect#toggleSelectAll

Toggle selection of all models in the collection:

myCol = new MultiCollection();

myCol.toggleSelectAll(); // select all models in the collection

myCol.toggleSelectAll(); // de-select all models in the collection

The following rules are used when toggling:

  • If no models are selected, select them all
  • If 1 or more models, but less than all models are selected, select them all
  • If all models are selected, deselect them all

MultiSelect Attributes

The following attribute is set by the multi-select automatically

MultiSelect#selected

Returns a hash of selected models, keyed from the model cid.

myCol = new MultiCollection();
myCol.select(model);

myCol.selected;

//=> produces
// {
//   "c1": (model object here)
// }

MultiSelect#selectedLength

Returns the number of items in the collection that are selected.

myCol = new MultiCollection();
myCol.select(model);

myCol.selectedLength; //=> 1

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

Building Backbone.Picky

If you wish to build Backbone.Picky on your system, you will need Ruby to run the Jasmine specs, and NodeJS to run the grunt build.

To Run The Jasmine Specs

  1. Be sure you have Bundler installed in your Ruby Gems. Then run bundle install from the project folder

  2. Once this is done, you can run rake jasmine to run the Jasmine server

  3. Point your browser at http://localhost:8888 and you will see all of the specs for Backbone.Picky

To Build The Packages

  1. Be sure you have NodeJS and NPM installed on your system

  2. Run npm install -g grunt to install the grunt build system

  3. From the project folder, run grunt to produce a build

Release Notes

v0.2.0

  • 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

v0.1.0

  • Added Picky.SingleSelect
  • Fleshed out the specs more

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.

More Repositories

1

backbone.modelbinding

awesome model binding for Backbone.js
JavaScript
700
star
2

backbone.memento

store and restore your model's state
JavaScript
367
star
3

mustbe

Authorization plumbing for NodeJS/ExpressJS/ConnectJS apps
JavaScript
320
star
4

Albacore

Dolphin-Safe Rake Tasks For .NET Systems
Ruby
240
star
5

jasmine.async

Make Jasmine's asynchronous testing suck less.
JavaScript
136
star
6

rabbus

A micro-service bus with built-in messaging patterns, for NodeJS and RabbitMQ
JavaScript
116
star
7

express-sub-app-demo

Demonstrates the ability to mount multiple Express apps into a single Express host app
JavaScript
111
star
8

solid-javascript

SOLID JavaScript In A Wobbly World (Wide Web)
JavaScript
70
star
9

nanit

Node Application Initializers
JavaScript
56
star
10

migroose

MongoDB database / data-structure migrations, for MongooseJS models and schemas
JavaScript
55
star
11

appcontroller

An example Application Controller implementation for C# WinForms applications
C#
35
star
12

emberclonemail

A sample application written with EmberJS
Ruby
32
star
13

backbone.compute

Computed fields for Backbone.Model
JavaScript
31
star
14

presentations-and-training

Material used for presentations and training classes
C#
26
star
15

backbone.fwd

forward events from one backbone object, through another
JavaScript
20
star
16

bowie

An experiment in beautiful models with ES6 elegance
JavaScript
19
star
17

hands-on-backbone

The sample code to go along with the "Hands-on Backbone.js" screencast series from PragProg.com
JavaScript
18
star
18

iam

Simple authentication plumbing and middleware for Node/Express apps
JavaScript
15
star
19

bada55-node-dev

How to build your own #BADA55 NodeJS development environment
JavaScript
14
star
20

UnitOfWork

A C# UnitOfWork Implementation For NHibernate. Supports WinForms and ASP.NET.
C#
12
star
21

epa

simple environment configuration for nodejs apps, using json files
JavaScript
11
star
22

speccy

simple javascript specification pattern implementation for nodejs / browserify
JavaScript
9
star
23

appcontroller.cf

Application Controller example code for the .NET Compact Framework
C#
8
star
24

jquery-to-backbone-marionette

code demo for my "jQuery To Backbone + Marionette" talk
JavaScript
8
star
25

boebotjs

Make Your Bot GO! With JavaScript!
JavaScript
8
star
26

Security

A small, role based security module for .NET apps
C#
7
star
27

vimbacore

a playground to try out c# coding in vim and figure out what albacore's csc task needs
Ruby
7
star
28

docker4js

Docker for JavaScript Developers - 2 day, hands-on training course from Derick Bailey
JavaScript
5
star
29

migroose-cli

command line tooling for mongrate, the mongodb/mongoosejs migration framework
JavaScript
5
star
30

5-tips-to-improve-js-with-es6

presentation given at Crater Remote Conf, Feb 10th, 2016
JavaScript
5
star
31

backbone-sinatra-boilerplate

My boilerplate cruft for working with Backbone.js in a Sinatra-backed app
JavaScript
5
star
32

MyFirstMVCSeleniumTest

How To Get Started With Selenium Core And ASP.NET MVC
JavaScript
4
star
33

classyobjects

A class-y inheritance example for JavaScript
JavaScript
3
star
34

backboneplugins

website for backboneplugins.com
CSS
3
star
35

ninject.rhinomocks.cf

Automocking container for RhinoMocks, running on Compact Framework
C#
3
star
36

5-stages-of-developer-grief

Presented at SpaceCityJS 2015
3
star
37

vimfiles.osx

my osx .vim folder and .vimrc
Vim Script
3
star
38

5-stages-of-entrepreneurial-grief

presentation for PrarieDevCon 2015
3
star
39

ninject.rhinomocks

Automocking container for RhinoMocks
C#
3
star
40

growing-express-architecture

Growing Express.js Architecture - a talk given at JSRemoteConf on Jan 15, 2016.
3
star
41

node-oracledb-cpu-leak

app to demonstrate node-oracledb cpu leak / spike
JavaScript
2
star
42

gitup

Automate the git update dance
Shell
2
star
43

execubot.js

A sample WebTask.io project: Read and execute code from a gist, post it in slack channel.
JavaScript
2
star
44

backbone.presentation

My slide deck for a Backbone.js presentation
JavaScript
2
star
45

cheesewiz

Correctly package localized resource files in .NET Comptact Framework .cab deployment projects
C#
2
star
46

jsfuncalc

An exercise in creating a functional javascript calculator
Ruby
2
star
47

ocarina

Simplified API for Oracle, built on top of official orabledb library
JavaScript
2
star
48

apologypro

apology.pro: because you're an amateur
JavaScript
2
star
49

boxing

terrible dropbox api and express middleware
JavaScript
2
star
50

mutedsolutions

website for my company
CSS
2
star
51

alt-tekpub

An open rewrite of Tekpub using Node, MongoDB and other buzzwords
JavaScript
1
star
52

objects

1
star
53

vimfiles.windows

my vim files and _vimrc for windows
Vim Script
1
star
54

derickbailey.github.com

My Github Homepage!
1
star
55

rabbus-sequence

process messages in sequential order with Rabbus and RabbitMQ
JavaScript
1
star
56

puzzleblocks

a command line game inspired by the Nintaii game that I play on my Droid
Ruby
1
star
57

traffic-limiter

Limit the number of tasks being run, based on task key/type
JavaScript
1
star
58

express-depot

mount Express sub-apps and middleware from a directory listing
JavaScript
1
star
59

react-todo-example

a basic example of organizing code in a basic react/redux app
JavaScript
1
star
60

wacotechlunch

Waco Tech Lunch
1
star
61

dotfiles

my dotfiles
Vim Script
1
star
62

node-jasmine-async

Making Jasmine's async suck less (for Jasmine v1.3)
JavaScript
1
star