Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contenteditable #113

Open
wants to merge 11 commits into
base: minor
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
sudo: false
language: node_js
before_install: npm install -g grunt-cli
node_js:
Expand Down
27 changes: 22 additions & 5 deletions apidoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ The document is the primary source for the API that Syphon
exposes, and provides information on how to correctly override
and configure the behaviors of Syphon.

## Syphon.InputFetcher

Determines the input elements present in the form (or view).
The default implementation will return elements of type `input`
as well as elements with `contenteditable="true"`.

## Syphon.KeyExtractorSet (Key Extractors)

When a form is serialized, all of the input elements are
Expand All @@ -15,7 +21,7 @@ to `.serialize`.
### Default Key Extractor Set

Syphon comes with a default key extractor set in the
`Backbone.Syhpon.KeyExtractors` object. This extractor set
`Backbone.Syphon.KeyExtractors` object. This extractor set
has one default extractor built in to it (see below).

You can replace the entire extractor set by creating a new
Expand All @@ -37,14 +43,18 @@ register new extractors as needed (see below).
### Default Key Extractor: element "name"

The default key extractor uses the `name` attribute of the form's
input element as the key.
input element as the key. If the `name` property is undefined, it
will fall back to using the `data-name` property (which should
therefore be defined in order to use Syphon with non-input elements,
e.g. a `div` with `contenteditable="true"`).

For example, an HTML form that looks like this:

```html
<form>
<input name="foo" value="bar">
<input type="checkbox" name="chk" checked>
<div data-name="editor" contenteditable="true">my text</div>
</form>
```

Expand All @@ -53,7 +63,8 @@ will produce this result, when serialized:
```js
{
foo: "bar",
chk: true
chk: true,
editor: "my text"
}
```

Expand Down Expand Up @@ -138,10 +149,13 @@ jQuery's `val()` method. The checkbox reader, however, looks
for whether or not the checkbox is checked and returns a
boolean value.

In addition, the input reader will handle elements that are
`contenteditable="true"` using jQuery's `html()` method.

### Default Input Reader Set

Syphon comes with a default input reader set in the
`Backbone.Syhpon.InputReaders` object. This input reader set
`Backbone.Syphon.InputReaders` object. This input reader set
has a few default input readers built in (see below).

You can replace the entire input reader set by creating a new
Expand Down Expand Up @@ -201,10 +215,13 @@ every form of input using jQuery's `val()` method. The checkbox reader,
sets whether or not the checkbox is checked, and the radio writer will
select the correct radio button in a radio button group.

In addition, the input writer will handle elements that are
`contenteditable="true"` using jQuery's `html()` method.

### Default Input Writer Set

Syphon comes with a default input writer set in the
`Backbone.Syhpon.InputWriters` object. This input writer set
`Backbone.Syphon.InputWriters` object. This input writer set
has a few default input writers built in (see above).

You can replace the entire input writer set by creating a new
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backbone.syphon",
"version": "0.5.1",
"version": "0.6.0",
"description": "Serialize a Backbone.View in to a JavaScript object.",
"main": "./lib/backbone.syphon.js",
"homepage": "https://github.com/marionettejs/backbone.syphon",
Expand Down
24 changes: 23 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
## v0.5.1 [view commit logs](https://github.com/marionettejs/backbone.syphon/compare/v0.5.0...v5.0.1)
## v0.6.0 [view commit logs](https://github.com/marionettejs/backbone.syphon/compare/v0.5.1...v0.6.1)

### Fixes

* When no elements are found, Backbone.Syphon will not throw an exception

### Docs

* Added note about using Backbone.Relational and circular references
* Typo fixes

### Improvements

* Travis now uses Docker images to speed up testing
* Uses `:input` instead of `form` to discover fields

### Features

* You can now ignore fields by selector
* When using indeterminate checkboxes, they will be a null value


## v0.5.1 [view commit logs](https://github.com/marionettejs/backbone.syphon/compare/v0.5.0...v0.5.1)

### Fixes

Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backbone.syphon",
"version": "0.5.1",
"version": "0.6.0",
"description": "Serialize a Backbone.View in to a JavaScript object.",
"main": "lib/backbone.syphon.js",
"scripts": ["lib/backbone.syphon.js"],
Expand Down
38 changes: 22 additions & 16 deletions lib/backbone.syphon.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Backbone.Syphon, v0.5.1
// Backbone.Syphon, v0.6.0
// ----------------------------------
//
// Copyright (c) 2015 Derick Bailey, Muted Solutions, LLC.
Expand Down Expand Up @@ -27,7 +27,7 @@

var Syphon = Backbone.Syphon = {};

Syphon.VERSION = '0.5.1';
Syphon.VERSION = '0.6.0';

Syphon.noConflict = function() {
Backbone.Syphon = previousSyphon;
Expand Down Expand Up @@ -123,16 +123,18 @@
// Retrieve all of the form inputs
// from the form
var getInputElements = function(view, config) {
var form = getForm(view);
var elements = form.elements;
var formInputs = getForm(view);

elements = _.reject(elements, function(el) {
formInputs = _.reject(formInputs, function(el) {
var reject;
var type = getElementType(el);
var extractor = config.keyExtractors.get(type);
var myType = getElementType(el);
var extractor = config.keyExtractors.get(myType);
var identifier = extractor($(el));

var foundInIgnored = _.include(config.ignoredTypes, type);
var foundInIgnored = _.find(config.ignoredTypes, function(ignoredTypeOrSelector) {
return (ignoredTypeOrSelector === myType) || $(el).is(ignoredTypeOrSelector);
});

var foundInInclude = _.include(config.include, identifier);
var foundInExclude = _.include(config.exclude, identifier);

Expand All @@ -149,7 +151,7 @@
return reject;
});

return elements;
return formInputs;
};

// Determine what type of element this is. It
Expand Down Expand Up @@ -177,13 +179,13 @@
return type.toLowerCase();
};

// If a form element is given, just return it.
// Otherwise, get the form element from the view.
// If a dom element is given, just return the form fields.
// Otherwise, get the form fields from the view.
var getForm = function(viewOrForm) {
if (_.isUndefined(viewOrForm.$el) && viewOrForm.tagName.toLowerCase() === 'form') {
return viewOrForm;
if (_.isUndefined(viewOrForm.$el)) {
return $(viewOrForm).children(':input');
} else {
return viewOrForm.$el.is('form') ? viewOrForm.el : viewOrForm.$('form')[0];
return viewOrForm.$(':input');
}
};

Expand Down Expand Up @@ -391,7 +393,7 @@
// Checkbox reader, returning a boolean value for
// whether or not the checkbox is checked.
InputReaders.register('checkbox', function($el) {
return $el.prop('checked');
return ($el.prop('indeterminate')) ? null : $el.prop('checked');
});

// Input Writers
Expand All @@ -413,7 +415,11 @@
// Checkbox writer, set whether or not the checkbox is checked
// depending on the boolean value.
InputWriters.register('checkbox', function($el, value) {
$el.prop('checked', value);
if (value === null) {
$el.prop('indeterminate', true);
} else {
$el.prop('checked', value);
}
});

// Radio button writer, set whether or not the radio button is
Expand Down
4 changes: 2 additions & 2 deletions lib/backbone.syphon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading