-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 242ea92
Showing
11 changed files
with
849 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2012 Anthony Short | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# backbone.collectionsubset | ||
|
||
Create sub-collections of other collections and keep them in sync | ||
|
||
## Getting Started | ||
### On the server | ||
Install the module with: `npm install backbone.collectionsubset` | ||
|
||
```javascript | ||
var backbone_collectionsubset = require('backbone.collectionsubset'); | ||
backbone_collectionsubset.awesome(); // "awesome" | ||
``` | ||
|
||
### In the browser | ||
Download the [production version][min] or the [development version][max]. | ||
|
||
[min]: https://raw.github.com/anthonyshort/backbone.collectionsubset/master/dist/backbone.collectionsubset.min.js | ||
[max]: https://raw.github.com/anthonyshort/backbone.collectionsubset/master/dist/backbone.collectionsubset.js | ||
|
||
In your web page: | ||
|
||
```html | ||
<script src="dist/backbone.collectionsubset.min.js"></script> | ||
<script> | ||
awesome(); // "awesome" | ||
</script> | ||
``` | ||
|
||
In your code, you can attach backbone.collectionsubset's methods to any object. | ||
|
||
```html | ||
<script> | ||
this.exports = Bocoup.utils; | ||
</script> | ||
<script src="dist/backbone.collectionsubset.min.js"></script> | ||
<script> | ||
Bocoup.utils.awesome(); // "awesome" | ||
</script> | ||
``` | ||
|
||
## Documentation | ||
_(Coming soon)_ | ||
|
||
## Examples | ||
_(Coming soon)_ | ||
|
||
## Contributing | ||
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](https://github.com/cowboy/grunt). | ||
|
||
_Also, please don't edit files in the "dist" subdirectory as they are generated via grunt. You'll find source code in the "lib" subdirectory!_ | ||
|
||
## Release History | ||
_(Nothing yet)_ | ||
|
||
## License | ||
Copyright (c) 2012 Anthony Short | ||
Licensed under the MIT license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/*! backbone.collectionsubset - v0.1.0 - 2012-09-13 | ||
* https://github.com/anthonyshort/backbone.collectionsubset | ||
* Copyright (c) 2012 Anthony Short; Licensed MIT */ | ||
|
||
(function() { | ||
var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
|
||
Backbone.CollectionSubset = (function() { | ||
|
||
CollectionSubset.extend = Backbone.Model.extend; | ||
|
||
CollectionSubset.include = function(obj) { | ||
var key, value, _ref; | ||
for (key in obj) { | ||
value = obj[key]; | ||
if (__indexOf.call(moduleKeywords, key) < 0) { | ||
if (!this.prototype[key]) { | ||
this.prototype[key] = value; | ||
} | ||
} | ||
} | ||
if ((_ref = obj.included) != null) { | ||
_ref.apply(this); | ||
} | ||
return this; | ||
}; | ||
|
||
CollectionSubset.include(Backbone.Events); | ||
|
||
function CollectionSubset(options) { | ||
if (options == null) { | ||
options = {}; | ||
} | ||
options = _.defaults(options, { | ||
refresh: true, | ||
triggers: null, | ||
filter: function() { | ||
return true; | ||
}, | ||
name: null, | ||
child: null, | ||
parent: null | ||
}); | ||
this.triggers = options.triggers ? options.triggers.split(' ') : []; | ||
if (!options.child) { | ||
options.child = new options.parent.constructor; | ||
} | ||
this.setParent(options.parent); | ||
this.setChild(options.child); | ||
this.setFilter(options.filter); | ||
if (options.model) { | ||
this.child.model = options.model; | ||
} | ||
if (options.refresh) { | ||
this.refresh(); | ||
} | ||
this.name = options.name; | ||
} | ||
|
||
CollectionSubset.prototype.setParent = function(collection) { | ||
var _ref, | ||
_this = this; | ||
if ((_ref = this.parent) != null) { | ||
_ref.off(null, null, this); | ||
} | ||
this.parent = collection; | ||
this.parent.on('add', this._onParentAdd, this); | ||
this.parent.on('remove', this._onParentRemove, this); | ||
this.parent.on('reset', this._onParentReset, this); | ||
this.parent.on('change', this._onParentChange, this); | ||
this.parent.on('dispose', this.dispose, this); | ||
this.parent.on('loading', (function() { | ||
return _this.child.trigger('loading'); | ||
}), this); | ||
return this.parent.on('ready', (function() { | ||
return _this.child.trigger('ready'); | ||
}), this); | ||
}; | ||
|
||
CollectionSubset.prototype.setChild = function(collection) { | ||
var _ref; | ||
if ((_ref = this.child) != null) { | ||
_ref.off(null, null, this); | ||
} | ||
this.child = collection; | ||
this.child.on('add', this._onChildAdd, this); | ||
this.child.on('reset', this._onChildReset, this); | ||
this.child.on('dispose', this.dispose, this); | ||
this.child.superset = this.parent; | ||
this.child.filterer = this; | ||
this.child.url = this.parent.url; | ||
return this.child.model = this.parent.model; | ||
}; | ||
|
||
CollectionSubset.prototype.setFilter = function(fn) { | ||
var filter; | ||
filter = function(model) { | ||
var matchesFilter, matchesParentFilter; | ||
matchesFilter = fn.call(this, model); | ||
matchesParentFilter = this.parent.filterer ? this.parent.filterer.filter(model) : true; | ||
return matchesFilter && matchesParentFilter; | ||
}; | ||
return this.filter = filter.bind(this); | ||
}; | ||
|
||
CollectionSubset.prototype.refresh = function(options) { | ||
var models; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
models = this.parent.filter(this.filter); | ||
this.child.reset(models, { | ||
subset: this | ||
}); | ||
return this.child.trigger('refresh'); | ||
}; | ||
|
||
CollectionSubset.prototype._replaceChildModel = function(parentModel) { | ||
var childModel, index; | ||
childModel = this.child.getByCid(parentModel.cid); | ||
if (childModel === parentModel) { | ||
return; | ||
} | ||
if (_.isUndefined(childModel)) { | ||
return this.child.add(parentModel, { | ||
subset: this | ||
}); | ||
} else { | ||
index = this.child.indexOf(childModel); | ||
this.child.remove(childModel); | ||
return this.child.add(parentModel, { | ||
at: index, | ||
subset: this | ||
}); | ||
} | ||
}; | ||
|
||
CollectionSubset.prototype._onParentAdd = function(model, collection, options) { | ||
if (options.subset === this) { | ||
return; | ||
} | ||
if (this.filter(model)) { | ||
return this._replaceChildModel(model); | ||
} | ||
}; | ||
|
||
CollectionSubset.prototype._onParentRemove = function(model, collection, options) { | ||
return this.child.remove(model, options); | ||
}; | ||
|
||
CollectionSubset.prototype._onParentReset = function(collection, options) { | ||
return this.refresh(); | ||
}; | ||
|
||
CollectionSubset.prototype._onParentChange = function(model, changes) { | ||
if (!this.triggerMatched(model)) { | ||
return; | ||
} | ||
if (this.filter(model)) { | ||
return this.child.add(model); | ||
} else { | ||
return this.child.remove(model); | ||
} | ||
}; | ||
|
||
CollectionSubset.prototype._onChildAdd = function(model, collection, options) { | ||
var parentModel; | ||
if (options.subset === this) { | ||
return; | ||
} | ||
this.parent.add(model); | ||
parentModel = this.parent.getByCid(model.cid); | ||
if (!parentModel) { | ||
return; | ||
} | ||
if (this.filter(parentModel)) { | ||
return this._replaceChildModel(parentModel); | ||
} else { | ||
return this.child.remove(model); | ||
} | ||
}; | ||
|
||
CollectionSubset.prototype._onChildReset = function(collection, options) { | ||
if (options.subset === this) { | ||
return; | ||
} | ||
this.parent.add(this.child.models); | ||
return this.refresh(); | ||
}; | ||
|
||
CollectionSubset.prototype.triggerMatched = function(model) { | ||
var changedAttrs; | ||
if (this.triggers.length === 0) { | ||
return true; | ||
} | ||
if (!model.hasChanged()) { | ||
return false; | ||
} | ||
changedAttrs = _.keys(model.changedAttributes()); | ||
return _.intersection(this.triggers, changedAttrs).length > 0; | ||
}; | ||
|
||
CollectionSubset.prototype.dispose = function() { | ||
var prop, _i, _len, _ref; | ||
if (this.disposed) { | ||
return; | ||
} | ||
this.trigger('dispose', this); | ||
this.parent.off(null, null, this); | ||
this.child.off(null, null, this); | ||
this.child.dispose(); | ||
this.off(); | ||
_ref = ['parent', 'child', 'options']; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
prop = _ref[_i]; | ||
delete this[prop]; | ||
} | ||
return this.disposed = true; | ||
}; | ||
|
||
return CollectionSubset; | ||
|
||
})(); | ||
|
||
}).call(this); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module.exports = function(grunt) { | ||
|
||
// Project configuration. | ||
grunt.initConfig({ | ||
pkg: '<json:package.json>', | ||
meta: { | ||
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' + | ||
'<%= grunt.template.today("yyyy-mm-dd") %>\n' + | ||
'<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' + | ||
'* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' + | ||
' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */' | ||
}, | ||
concat: { | ||
dist: { | ||
src: ['<banner:meta.banner>', '<file_strip_banner:lib/<%= pkg.name %>.js>'], | ||
dest: 'dist/<%= pkg.name %>.js' | ||
} | ||
}, | ||
min: { | ||
dist: { | ||
src: ['<banner:meta.banner>', '<config:concat.dist.dest>'], | ||
dest: 'dist/<%= pkg.name %>.min.js' | ||
} | ||
}, | ||
test: { | ||
files: ['test/**/*.js'] | ||
}, | ||
lint: { | ||
files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js'] | ||
}, | ||
watch: { | ||
files: '<config:lint.files>', | ||
tasks: 'lint test' | ||
}, | ||
jshint: { | ||
options: { | ||
curly: true, | ||
eqeqeq: true, | ||
immed: true, | ||
latedef: true, | ||
newcap: true, | ||
noarg: true, | ||
sub: true, | ||
undef: true, | ||
boss: true, | ||
eqnull: true | ||
}, | ||
globals: { | ||
exports: true, | ||
module: false | ||
} | ||
}, | ||
coffee: { | ||
app: { | ||
src: ['src/*.coffee'], | ||
dest: 'lib', | ||
options: { | ||
bare: false | ||
} | ||
} | ||
}, | ||
uglify: {} | ||
}); | ||
|
||
// Default task. | ||
grunt.registerTask('default', 'coffee concat min'); | ||
grunt.loadNpmTasks('grunt-coffee'); | ||
|
||
}; |
Oops, something went wrong.