Permalink
Browse files

added AMD/RequireJS compatible build, using grunt-rigger

  • Loading branch information...
1 parent fd50469 commit c2e252a8bba7469d8570f8d4981df8cd3d86591f @derickbailey committed May 18, 2012
Showing with 192 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +17 −3 grunt.js
  3. +146 −0 lib/amd/backbone.syphon.js
  4. +5 −0 lib/amd/backbone.syphon.min.js
  5. +8 −0 package.json
  6. +6 −0 readme.md
  7. +9 −0 src/amd.js
View
@@ -4,3 +4,4 @@
*.orig
tmp/
ext/
+node_modules
View
@@ -1,6 +1,8 @@
/*global module:false*/
module.exports = function(grunt) {
+ grunt.loadNpmTasks('grunt-rigger');
+
// Project configuration.
grunt.initConfig({
meta: {
@@ -11,18 +13,30 @@ module.exports = function(grunt) {
'// http://github.com/derickbailey/backbone.syphon'
},
lint: {
- files: ['src/**/*.js']
+ files: ['src/backbone.syphon.js']
},
concat: {
dist: {
src: ['<banner:meta.banner>', '<file_strip_banner:src/backbone.syphon.js>'],
dest: 'lib/backbone.syphon.js'
}
},
+
+ rig: {
+ amd: {
+ src: ['<banner:meta.banner>', 'src/amd.js'],
+ dest: 'lib/amd/backbone.syphon.js'
+ }
+ },
+
min: {
- dist: {
+ standard: {
src: ['<banner:meta.banner>', '<config:concat.dist.dest>'],
dest: 'lib/backbone.syphon.min.js'
+ },
+ amd: {
+ src: ['<banner:meta.banner>', '<config:rig.amd.dest>'],
+ dest: 'lib/amd/backbone.syphon.min.js'
}
},
jshint: {
@@ -49,6 +63,6 @@ module.exports = function(grunt) {
});
// Default task.
- grunt.registerTask('default', 'lint concat min');
+ grunt.registerTask('default', 'lint concat rig min');
};
View
@@ -0,0 +1,146 @@
+// Backbone.Syphon, v0.1.1
+// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
+// Distributed under MIT license
+// http://github.com/derickbailey/backbone.syphon
+(function (root, factory) {
+ if (typeof define === 'function' && define.amd) {
+ // AMD. Register as an anonymous module.
+ define(["_", "jQuery","Backbone"], factory);
+ }
+}(this, function (_, $, Backbone) {
+ Backbone.Syphon = (function(Backbone, $, _){
+ var Syphon = {};
+
+ // Ignore Element Types
+ // --------------------
+
+ // Tell Syphon to ignore all elements of these types
+ var ignoredTypes = ["button", "submit", "reset"];
+
+ // Syphon
+ // ------
+
+ // Get a JSON object that represents
+ // all of the form inputs, in this view
+ Syphon.serialize = function(view){
+ var data = {};
+
+ var elements = getInputElements(view, ignoredTypes);
+
+ _.each(elements, function(el){
+ var $el = $(el);
+ var type = getElementType($el);
+ var inputReader = Syphon.InputReaders.get(type);
+ var value = inputReader($el);
+
+ data[el.id] = value;
+ });
+
+ return data;
+ };
+
+ // Input Readers
+ // -------------
+
+ Syphon.InputReaders = {
+ readers: {},
+
+ // Retrieve the correct input reader based
+ // on the type of the element that is passed
+ // in as the `$el` parameter. If no reader is
+ // found for the specific input type, returns
+ // a default input reader.
+ get: function(type){
+ var reader = this.readers[type.toLowerCase()];
+
+ if (!reader){
+ reader = this.readers["default"];
+ }
+
+ return reader;
+ },
+
+ // Register a new input reader, based on
+ // an input element type. For example: "text",
+ // or "textarea". Note that the type can either
+ // be a `type` attribute (`type="text"`) or the
+ // elmement `tagName` (`<textarea>`).
+ register: function(type, reader){
+ this.readers[type.toLowerCase()] = reader;
+ },
+
+ // Registers the default input reader that will
+ // be returned when no input reader is found for
+ // the specific type requested.
+ registerDefault: function(reader){
+ this.readers["default"] = reader;
+ },
+
+ // Remove the Input Reader associated with this
+ // input type.
+ unregister: function(type){
+ delete this.readers[type];
+ }
+ };
+
+ // Built-in Input Readers
+ // ---------------------
+
+ // The default reader
+ Syphon.InputReaders.registerDefault(function($el){
+ return $el.val();
+ });
+
+ // Checkbox reader, returning a boolean value for
+ // whether or not the checkbox is checked.
+ Syphon.InputReaders.register("checkbox", function($el){
+ var checked = $el.prop("checked");
+ return checked;
+ });
+
+ // Helpers
+ // -------
+
+ // Retrieve all of the form inputs
+ // from the view
+ var getInputElements = function(view, ignoreTypes){
+ var form = view.$("form")[0];
+ var elements = form.elements;
+ elements = _.reject(elements, function(el){
+ var type = getElementType(el);
+ var found = _.include(ignoreTypes, type);
+ return found;
+ });
+ return elements;
+ };
+
+ // Determine what type of element this is. It
+ // will either return the `type` attribute of
+ // an `<input>` element, or the `tagName` of
+ // the element when the element is not an `<input>`.
+ var getElementType = function(el){
+ var typeAttr;
+ var $el = $(el);
+ var tagName = $el[0].tagName;
+ var type = tagName;
+
+ if (tagName.toLowerCase() === "input"){
+ typeAttr = $el.attr("type");
+ if (typeAttr){
+ type = typeAttr;
+ } else {
+ type = "text";
+ }
+ }
+
+ // Always return the type as lowercase
+ // so it can be matched to lowercase
+ // type registrations.
+ return type.toLowerCase();
+ };
+
+ return Syphon;
+ })(Backbone, jQuery, _);
+
+ return Backbone.Syphon;
+}));

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,8 @@
+{
+ "name": "Backbone.syphon",
+ "version": "0.1.1",
+ "dependencies": {
+ "grunt": "*",
+ "grunt-rigger": "*"
+ }
+}
View
@@ -32,6 +32,12 @@ Development: [backbone.syphon.js](https://raw.github.com/derickbailey/backbone.s
Production: [backbone.syphon.min.js](https://raw.github.com/derickbailey/backbone.syphon/master/lib/backbone.syphon.min.js)
+### AMD/RequireJS Builds
+
+Development: [backbone.syphon.js](https://raw.github.com/derickbailey/backbone.syphon/master/lib/amd/backbone.syphon.js)
+
+Production: [backbone.syphon.min.js](https://raw.github.com/derickbailey/backbone.syphon/master/lib/amd/backbone.syphon.min.js)
+
## Basic Usage
When the data from a form is needed, you can call the
View
@@ -0,0 +1,9 @@
+(function (root, factory) {
+ if (typeof define === 'function' && define.amd) {
+ // AMD. Register as an anonymous module.
+ define(["_", "jQuery","Backbone"], factory);
+ }
+}(this, function (_, $, Backbone) {
+ //= backbone.syphon.js
+ return Backbone.Syphon;
+}));

0 comments on commit c2e252a

Please sign in to comment.