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

Drop direct dependency on jquery #125

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ sudo: false
language: node_js
before_install: npm install -g grunt-cli
node_js:
- "0.10"
- "5"
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
"grunt-contrib-watch": "0.6.1",
"grunt-jscs": "^1.5.0",
"grunt-lintspaces": "0.7.0",
"grunt-mocha-test": "^0.11.0",
"grunt-mocha-test": "^0.12.0",
"grunt-preprocess": "^4.0.0",
"grunt-template": "^0.2.3",
"jsdom": "^0.11.0",
"jsdom": "^8.1.0",
"load-grunt-tasks": "^0.4.0",
"mocha": "^1.20.1",
"sinon": "^1.10.2",
Expand Down
2 changes: 2 additions & 0 deletions spec/javascripts/deserialize.nested.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ describe('deserializing nested key names', function() {

this.writers = new Backbone.Syphon.InputWriterSet();
this.writers.register('checkbox', function($el, value) {
$el = $($el);

if (_.include(value, $el.val())) {
$el.prop('checked', true);
}
Expand Down
2 changes: 2 additions & 0 deletions spec/javascripts/inputReaders.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ describe('input readers', function() {

this.readers = new Backbone.Syphon.InputReaderSet();
this.readers.registerDefault(function($el) {
$el = $($el);

return $el.data('stuff');
});

Expand Down
2 changes: 2 additions & 0 deletions spec/javascripts/inputWriters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ describe('input writers', function() {

this.writers = new Backbone.Syphon.InputWriterSet();
this.writers.registerDefault(function($el, value) {
$el = $($el);

$el.data('stuff', value);
});

Expand Down
2 changes: 2 additions & 0 deletions spec/javascripts/keyAssignmentValidators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('key assignment validators', function() {

this.validators = new Backbone.Syphon.KeyAssignmentValidatorSet();
this.validators.registerDefault(function($el) {
$el = $($el);

return $el.data('stuff') === 'bar';
});

Expand Down
2 changes: 2 additions & 0 deletions spec/javascripts/keyExtractors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ describe('key extractors', function() {

this.extractors = new Backbone.Syphon.KeyExtractorSet();
this.extractors.registerDefault(function($el) {
$el = $($el);

return $el.data('stuff');
});

Expand Down
1 change: 1 addition & 0 deletions spec/javascripts/serialize.nested.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('serializing nested key names', function() {

this.inputReaders = new Backbone.Syphon.InputReaderSet();
this.inputReaders.register('checkbox', function($el) {
$el = $($el);
return $el.val();
});

Expand Down
4 changes: 2 additions & 2 deletions spec/javascripts/setup/node.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
if (!global.document || !global.window) {
var jsdom = require('jsdom').jsdom;

global.document = jsdom('<html><head><script></script></head><body></body></html>', null, {
global.document = jsdom('<html><head><script></script></head><body></body></html>', {
FetchExternalResources: ['script'],
ProcessExternalResources: ['script'],
MutationEvents: '2.0',
QuerySelector: false
});

global.window = document.createWindow();
global.window = document.defaultView;
global.navigator = global.window.navigator;

global.window.Node.prototype.contains = function(node) {
Expand Down
8 changes: 4 additions & 4 deletions src/backbone.syphon.inputreaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ var InputReaders = Syphon.InputReaders = new InputReaderSet();

// The default input reader, which uses an input
// element's "value"
InputReaders.registerDefault(function($el) {
return $el.val();
InputReaders.registerDefault(function(el) {
return el.value;
});

// Checkbox reader, returning a boolean value for
// whether or not the checkbox is checked.
InputReaders.register('checkbox', function($el) {
return ($el.prop('indeterminate')) ? null : $el.prop('checked');
InputReaders.register('checkbox', function(el) {
return el.indeterminate ? null : el.checked;
});
14 changes: 7 additions & 7 deletions src/backbone.syphon.inputwriters.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ var InputWriters = Syphon.InputWriters = new InputWriterSet();

// The default input writer, which sets an input
// element's "value"
InputWriters.registerDefault(function($el, value) {
$el.val(value);
InputWriters.registerDefault(function(el, value) {
el.value = value;
});

// Checkbox writer, set whether or not the checkbox is checked
// depending on the boolean value.
InputWriters.register('checkbox', function($el, value) {
InputWriters.register('checkbox', function(el, value) {
if (value === null) {
$el.prop('indeterminate', true);
el.indeterminate = true;
} else {
$el.prop('checked', value);
el.checked = value;
}
});

// Radio button writer, set whether or not the radio button is
// 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());
InputWriters.register('radio', function(el, value) {
el.checked = (el.value === value.toString());
});
34 changes: 18 additions & 16 deletions src/backbone.syphon.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,20 @@ Syphon.serialize = function(view, options) {

// Process all of the elements
_.each(elements, function(el) {
var $el = $(el);
var type = getElementType($el);
var type = getElementType(el);

// Get the key for the input
var keyExtractor = config.keyExtractors.get(type);
var key = keyExtractor($el);
var key = keyExtractor(el);

// Get the value for the input
var inputReader = config.inputReaders.get(type);
var value = inputReader($el);
var value = inputReader(el);

// Get the key assignment validator and make sure
// it's valid before assigning the value to the key
var validKeyAssignment = config.keyAssignmentValidators.get(type);
if (validKeyAssignment($el, key, value)) {
if (validKeyAssignment(el, key, value)) {
var keychain = config.keySplitter(key);
data = assignKeyValue(data, keychain, value);
}
Expand All @@ -65,19 +64,18 @@ Syphon.deserialize = function(view, data, options) {

// Process all of the elements
_.each(elements, function(el) {
var $el = $(el);
var type = getElementType($el);
var type = getElementType(el);

// Get the key for the input
var keyExtractor = config.keyExtractors.get(type);
var key = keyExtractor($el);
var key = keyExtractor(el);

// Get the input writer and the value to write
var inputWriter = config.inputWriters.get(type);
var value = flattenedData[key];

// Write the value to the input
inputWriter($el, value);
inputWriter(el, value);
});
};

Expand All @@ -93,10 +91,11 @@ var getInputElements = function(view, config) {
var reject;
var myType = getElementType(el);
var extractor = config.keyExtractors.get(myType);
var identifier = extractor($(el));
var identifier = extractor(el);

var foundInIgnored = _.find(config.ignoredTypes, function(ignoredTypeOrSelector) {
return (ignoredTypeOrSelector === myType) || $(el).is(ignoredTypeOrSelector);
// IE8 will need to polyfill matches
return (ignoredTypeOrSelector === myType) || el.matches(ignoredTypeOrSelector);
});

var foundInInclude = _.include(config.include, identifier);
Expand Down Expand Up @@ -124,12 +123,11 @@ var getInputElements = function(view, config) {
// the element when the element is not an `<input>`.
var getElementType = function(el) {
var typeAttr;
var $el = $(el);
var tagName = $el[0].tagName;
var tagName = el.tagName;
var type = tagName;

if (tagName.toLowerCase() === 'input') {
typeAttr = $el.attr('type');
typeAttr = el.getAttribute('type');
if (typeAttr) {
type = typeAttr;
} else {
Expand All @@ -146,10 +144,14 @@ var getElementType = function(el) {
// If a dom element is given, just return the form fields.
// Otherwise, get the form fields from the view.
var getForm = function(viewOrForm) {
// Do not need to worry about this case because there are no space + selectors
//http://ejohn.org/blog/thoughts-on-queryselectorall/
var inputMatcher = 'input,select,textarea,button';

if (_.isUndefined(viewOrForm.$el)) {
return $(viewOrForm).find(':input');
return viewOrForm.querySelectorAll(inputMatcher);
} else {
return viewOrForm.$(':input');
return viewOrForm.el.querySelectorAll(inputMatcher);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/backbone.syphon.keyassignmentvalidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ KeyAssignmentValidators.registerDefault(function() {

// But only the "checked" radio button for a given
// radio button group is valid
KeyAssignmentValidators.register('radio', function($el, key, value) {
return $el.prop('checked');
KeyAssignmentValidators.register('radio', function(el, key, value) {
return el.checked;
});
4 changes: 2 additions & 2 deletions src/backbone.syphon.keyextractors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ var KeyExtractors = Syphon.KeyExtractors = new KeyExtractorSet();

// The default key extractor, which uses the
// input element's "name" attribute
KeyExtractors.registerDefault(function($el) {
return $el.prop('name') || '';
KeyExtractors.registerDefault(function(el) {
return el.getAttribute('name') || '';
});