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 support #143

Merged
merged 18 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions apidoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,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 +57,8 @@ will produce this result, when serialized:
```js
{
foo: "bar",
chk: true
chk: true,
editor: "my text"
}
```

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.7.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump this to 0.8.0

"version": "0.7.1",
"description": "Serialize a Backbone.View in to a JavaScript object.",
"main": "./lib/backbone.syphon.js",
"homepage": "https://github.com/marionettejs/backbone.syphon",
Expand Down
29 changes: 23 additions & 6 deletions lib/backbone.syphon.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Backbone.Syphon, v1.0.0
// Backbone.Syphon, v0.7.1
// ----------------------------------
//
// Copyright (c) 2016 Derick Bailey, Muted Solutions, LLC.
// Copyright (c) 2018 Derick Bailey, Muted Solutions, LLC.
// Distributed under MIT license
//
// http://github.com/marionettejs/backbone.syphon
Expand All @@ -27,7 +27,7 @@

var Syphon = Backbone.Syphon = {};

Syphon.VERSION = '1.0.0';
Syphon.VERSION = '0.7.1';

Syphon.noConflict = function() {
Backbone.Syphon = previousSyphon;
Expand Down Expand Up @@ -173,6 +173,10 @@
}
}

if ($el.attr('contenteditable')) {
type = 'contenteditable';
}

// Always return the type as lowercase
// so it can be matched to lowercase
// type registrations.
Expand All @@ -185,7 +189,7 @@
if (_.isUndefined(viewOrForm.$el)) {
return $(viewOrForm).find(':input');
} else {
return viewOrForm.$(':input');
return viewOrForm.$(':input, [contenteditable]');
}
};

Expand Down Expand Up @@ -371,7 +375,7 @@
// The default key extractor, which uses the
// input element's "name" attribute
KeyExtractors.registerDefault(function($el) {
return $el.prop('name') || '';
return $el.prop('name') || $el.data('name') || '';
});

// Input Readers
Expand All @@ -390,6 +394,12 @@
return $el.val();
});

// contenteditable reader, which returns the HTML
// contained by the element
InputReaders.register('contenteditable', function($el) {
return $el.html();
});

// Checkbox reader, returning a boolean value for
// whether or not the checkbox is checked.
InputReaders.register('checkbox', function($el) {
Expand All @@ -412,6 +422,12 @@
$el.val(value);
});

// contenteditable writer, which changes the HTML contained by
// the element
InputWriters.register('contenteditable', function($el, value) {
$el.html(value);
});

// Checkbox writer, set whether or not the checkbox is checked
// depending on the boolean value.
InputWriters.register('checkbox', function($el, value) {
Expand All @@ -426,7 +442,8 @@
// checked. The button should only be checked if it's value
// equals the given value.
InputWriters.register('radio', function($el, value) {
$el.prop('checked', $el.val() === value.toString());
var checked = _.isUndefined(value) ? false : $el.val() === value.toString();
$el.prop('checked', checked);
});

// Key Assignment Validators
Expand Down
6 changes: 3 additions & 3 deletions lib/backbone.syphon.min.js

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

Loading