Permalink
Browse files

First

  • Loading branch information...
0 parents commit baec9bd8b21b28ec57d23ac962ab724a724ffc37 @anthonyshort committed Sep 13, 2012
Showing with 308 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 .npmignore
  3. +22 −0 LICENSE-MIT
  4. +85 −0 README.md
  5. +24 −0 dist/backbone.include.js
  6. +4 −0 dist/backbone.include.min.js
  7. +69 −0 grunt.js
  8. +20 −0 lib/backbone.include.js
  9. +36 −0 package.json
  10. +11 −0 src/backbone.include.coffee
  11. +35 −0 test/backbone.include_test.js
1 .gitignore
@@ -0,0 +1 @@
+node_modules/*
1 .npmignore
@@ -0,0 +1 @@
+/node_modules/
22 LICENSE-MIT
@@ -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.
85 README.md
@@ -0,0 +1,85 @@
+# backbone.include
+
+Extremely simple mixin interface for Backbone borrowed from SpineJS. Many Backbone extensions require you to extend their model or view. It shouldn't be this way. We shouldn't be extending the prototype chain and we shouldn't have to do any manual work for something so common.
+
+This extension adds a simple `include` class method to Model, Collection and View.
+
+## Usage
+
+Download the [production version][min] or the [development version][max].
+
+[min]: https://raw.github.com/anthonyshort/backbone.include/master/dist/backbone.include.min.js
+[max]: https://raw.github.com/anthonyshort/backbone.include/master/dist/backbone.include.js
+
+In your web page:
+
+```html
+<script src="dist/backbone.include.min.js"></script>
+```
+
+Now you can mixin objects easily.
+
+```coffee
+class Task extends Backbone.Model
+ @include Validation
+
+class TaskView extends Backbone.View
+ @include EditableView
+ @include CompositeView
+ @include DraggableView
+```
+
+or in JS
+
+```js
+var Task = Backbone.Model.extend();
+Task.include(Validation);
+
+var TaskView = Backbone.View.extend();
+TaskView.include(EditableView);
+TaskView.include(CompositeView);
+TaskView.include(DraggableView);
+```
+
+Now we can break apart our code and easily re-use it across projects. We can mix and match the parts we
+need exactly where we need them.
+
+But extending the prototype isn't the only thing it does. It also fires of an `included` method on the mixin
+whenever it is included into an object and it is called in the context of the object it is being included into.
+
+For example:
+
+```coffee
+DraggableView =
+ included: ->
+ @::initialize = (options) ->
+ dragging = false
+ drag: ->
+ drop: ->
+
+class TaskView extends Backbone.View
+ @include DraggableView
+
+view = new TaskView
+view.dragging # false
+```
+
+It's a simple pattern but it's one that has let me break a lot of functionality out into reusable mixins extremely easily.
+
+## Todo
+
+* Look at using constructor functions instead of raw objects. On include extend the prototypes and call the constructor after
+the constructor of the mixee.
+* Add quicker ways of extending the `initialize` and `dispose` methods since they're used so frequently
+
+## 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 "src" subdirectory!_
+
+## Release History
+0.1.0 - First Release
+
+## License
+Copyright (c) 2012 Anthony Short
+Licensed under the MIT license.
24 dist/backbone.include.js
@@ -0,0 +1,24 @@
+/*! backbone.include - v0.1.0 - 2012-09-13
+* https://github.com/anthonyshort/backbone.include
+* 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.Model.include = Backbone.Collection.include = Backbone.View.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;
+ };
+
+}).call(this);
4 dist/backbone.include.min.js
@@ -0,0 +1,4 @@
+/*! backbone.include - v0.1.0 - 2012-09-13
+* https://github.com/anthonyshort/backbone.include
+* Copyright (c) 2012 Anthony Short; Licensed MIT */
+(function(){var e=[].indexOf||function(e){for(var t=0,n=this.length;t<n;t++)if(t in this&&this[t]===e)return t;return-1};Backbone.Model.include=Backbone.Collection.include=Backbone.View.include=function(t){var n,r,i;for(n in t)r=t[n],e.call(moduleKeywords,n)<0&&(this.prototype[n]||(this.prototype[n]=r));return(i=t.included)!=null&&i.apply(this),this}}).call(this);
69 grunt.js
@@ -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');
+
+};
20 lib/backbone.include.js
@@ -0,0 +1,20 @@
+(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.Model.include = Backbone.Collection.include = Backbone.View.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;
+ };
+
+}).call(this);
36 package.json
@@ -0,0 +1,36 @@
+{
+ "name": "backbone.include",
+ "description": "Simple interface for mixins with Backbone",
+ "version": "0.1.0",
+ "homepage": "https://github.com/anthonyshort/backbone.include",
+ "author": {
+ "name": "Anthony Short",
+ "email": "antshort@gmail.com",
+ "url": "http://anthonyshort.me"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/anthonyshort/backbone.include.git"
+ },
+ "bugs": {
+ "url": "https://github.com/anthonyshort/backbone.include/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/anthonyshort/backbone.include/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "lib/backbone.include",
+ "engines": {
+ "node": "*"
+ },
+ "scripts": {
+ "test": "grunt test"
+ },
+ "devDependencies": {
+ "grunt": "~0.3.15",
+ "grunt-coffee": "*"
+ },
+ "keywords": []
+}
11 src/backbone.include.coffee
@@ -0,0 +1,11 @@
+# Backbone.Include
+# https://github.com/anthonyshort/backbone.include
+#
+# Copyright (c) 2012 Anthony Short
+# Licensed under the MIT license.
+
+Backbone.Model.include = Backbone.Collection.include = Backbone.View.include = (obj) ->
+ for key, value of obj when key not in moduleKeywords
+ if not @::[key] then @::[key] = value
+ obj.included?.apply(this)
+ @
35 test/backbone.include_test.js
@@ -0,0 +1,35 @@
+/*global require:true */
+var backbone_include = require('../lib/backbone.include.js');
+
+/*
+ ======== A Handy Little Nodeunit Reference ========
+ https://github.com/caolan/nodeunit
+
+ Test methods:
+ test.expect(numAssertions)
+ test.done()
+ Test assertions:
+ test.ok(value, [message])
+ test.equal(actual, expected, [message])
+ test.notEqual(actual, expected, [message])
+ test.deepEqual(actual, expected, [message])
+ test.notDeepEqual(actual, expected, [message])
+ test.strictEqual(actual, expected, [message])
+ test.notStrictEqual(actual, expected, [message])
+ test.throws(block, [error], [message])
+ test.doesNotThrow(block, [error], [message])
+ test.ifError(value)
+*/
+
+exports['awesome'] = {
+ setUp: function(done) {
+ // setup here
+ done();
+ },
+ 'no args': function(test) {
+ test.expect(1);
+ // tests here
+ test.equal(backbone_include.awesome(), 'awesome', 'should be awesome.');
+ test.done();
+ }
+};

0 comments on commit baec9bd

Please sign in to comment.