jquery-serialize-json-sprockets 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f62f51cd83e40320487c0bc480ff3425207d4cf1
|
4
|
+
data.tar.gz: b5372cbae34604c647deb52d4b012e46662efe27
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eb93131cac6d034bc09968af0961ba9df8e301170a4e15753641feb0bd43c3302d912eb97e8396d44da64e8a119248c62c9f4df00d451204aa04b4638d12d891
|
7
|
+
data.tar.gz: c8503dd20dfdd87ef288941c5b7d2a00fa1784c28ad705a3f7671f925e83a6d7669b11f4688b1518b25cadfd1703d6979e9e817e5fc998be40f15fc7824e9908
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module JQuery
|
2
|
+
module Serialize
|
3
|
+
module JSON
|
4
|
+
module Sprockets
|
5
|
+
|
6
|
+
def self.path
|
7
|
+
File.expand_path(File.join("..", "vendor", "assets", "javascripts"), File.dirname(__FILE__))
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.load!
|
11
|
+
|
12
|
+
if defined?(::Rails)
|
13
|
+
require "jquery/serialize/json/sprockets/engine"
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
JQuery::Serialize::JSON::Sprockets.load!
|
@@ -0,0 +1,166 @@
|
|
1
|
+
/*!
|
2
|
+
SerializeJSON jQuery plugin.
|
3
|
+
https://github.com/marioizquierdo/jquery.serializeJSON
|
4
|
+
version 2.1.0 (May, 2014)
|
5
|
+
|
6
|
+
Copyright (c) 2014 Mario Izquierdo
|
7
|
+
Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
|
8
|
+
and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
|
9
|
+
*/
|
10
|
+
(function ($) {
|
11
|
+
"use strict";
|
12
|
+
|
13
|
+
// jQuery('form').serializeJSON()
|
14
|
+
$.fn.serializeJSON = function (options) {
|
15
|
+
var serializedObject, formAsArray, keys, value, f, opts;
|
16
|
+
f = $.serializeJSON;
|
17
|
+
formAsArray = this.serializeArray(); // array of objects {name, value}
|
18
|
+
opts = f.optsWithDefaults(options); // calculate values for options {parseNumbers, parseBoolens, parseNulls}
|
19
|
+
|
20
|
+
serializedObject = {};
|
21
|
+
$.each(formAsArray, function (i, input) {
|
22
|
+
keys = f.splitInputNameIntoKeysArray(input.name); // "some[deep][key]" => ['some', 'deep', 'key']
|
23
|
+
value = f.parseValue(input.value, opts); // string, number, boolean or null
|
24
|
+
if (opts.parseWithFunction) value = opts.parseWithFunction(value); // allow for custom parsing
|
25
|
+
f.deepSet(serializedObject, keys, value, opts);
|
26
|
+
});
|
27
|
+
return serializedObject;
|
28
|
+
};
|
29
|
+
|
30
|
+
// Use $.serializeJSON as namespace for the auxiliar functions
|
31
|
+
// and to define defaults
|
32
|
+
$.serializeJSON = {
|
33
|
+
|
34
|
+
defaultOptions: {
|
35
|
+
parseNumbers: false, // convert values like "1", "-2.33" to 1, -2.33
|
36
|
+
parseBooleans: false, // convert "true", "false" to true, false
|
37
|
+
parseNulls: false, // convert "null" to null
|
38
|
+
parseAll: false, // all of the above
|
39
|
+
parseWithFunction: null, // to use custom parser, use a function like: function (val) => parsed_val
|
40
|
+
useIntKeysAsArrayIndex: false // name="foo[2]" value="v" => {foo: [null, null, "v"]}, instead of {foo: ["2": "v"]}
|
41
|
+
},
|
42
|
+
|
43
|
+
// Merge options with defaults to get {parseNumbers, parseBoolens, parseNulls, useIntKeysAsArrayIndex}
|
44
|
+
optsWithDefaults: function(options) {
|
45
|
+
var f, parseAll;
|
46
|
+
if (options == null) options = {}; // arg default value = {}
|
47
|
+
f = $.serializeJSON;
|
48
|
+
parseAll = f.optWithDefaults('parseAll', options);
|
49
|
+
return {
|
50
|
+
parseNumbers: parseAll || f.optWithDefaults('parseNumbers', options),
|
51
|
+
parseBooleans: parseAll || f.optWithDefaults('parseBooleans', options),
|
52
|
+
parseNulls: parseAll || f.optWithDefaults('parseNulls', options),
|
53
|
+
parseWithFunction: f.optWithDefaults('parseWithFunction', options),
|
54
|
+
useIntKeysAsArrayIndex: f.optWithDefaults('useIntKeysAsArrayIndex', options)
|
55
|
+
}
|
56
|
+
},
|
57
|
+
|
58
|
+
optWithDefaults: function(key, options) {
|
59
|
+
return (options[key] !== false) && (options[key] || $.serializeJSON.defaultOptions[key]);
|
60
|
+
},
|
61
|
+
|
62
|
+
// Convert the string to a number, boolean or null, depending on the enable option and the string format.
|
63
|
+
parseValue: function(str, opts) {
|
64
|
+
var value, f;
|
65
|
+
f = $.serializeJSON;
|
66
|
+
if (opts.parseNumbers && f.isNumeric(str)) return Number(str); // number
|
67
|
+
if (opts.parseBooleans && (str === "true" || str === "false")) return str === "true"; // boolean
|
68
|
+
if (opts.parseNulls && str == "null") return null; // null
|
69
|
+
return str; // otherwise, keep same string
|
70
|
+
},
|
71
|
+
|
72
|
+
isObject: function(obj) { return obj === Object(obj); }, // is this variable an object?
|
73
|
+
isUndefined: function(obj) { return obj === void 0; }, // safe check for undefined values
|
74
|
+
isValidArrayIndex: function(val) { return /^[0-9]+$/.test(String(val)); }, // 1,2,3,4 ... are valid array indexes
|
75
|
+
isNumeric: function(obj) { return obj - parseFloat(obj) >= 0; }, // taken from jQuery.isNumeric implementation. Not using jQuery.isNumeric to support old jQuery and Zepto versions
|
76
|
+
|
77
|
+
// Split the input name in programatically readable keys
|
78
|
+
// "foo" => ['foo']
|
79
|
+
// "[foo]" => ['foo']
|
80
|
+
// "foo[inn][bar]" => ['foo', 'inn', 'bar']
|
81
|
+
// "foo[inn][arr][0]" => ['foo', 'inn', 'arr', '0']
|
82
|
+
// "arr[][val]" => ['arr', '', 'val']
|
83
|
+
splitInputNameIntoKeysArray: function (name) {
|
84
|
+
var keys, last, f;
|
85
|
+
f = $.serializeJSON;
|
86
|
+
if (f.isUndefined(name)) { throw new Error("ArgumentError: param 'name' expected to be a string, found undefined"); }
|
87
|
+
keys = $.map(name.split('['), function (key) {
|
88
|
+
last = key[key.length - 1];
|
89
|
+
return last === ']' ? key.substring(0, key.length - 1) : key;
|
90
|
+
});
|
91
|
+
if (keys[0] === '') { keys.shift(); } // "[foo][inn]" should be same as "foo[inn]"
|
92
|
+
return keys;
|
93
|
+
},
|
94
|
+
|
95
|
+
// Set a value in an object or array, using multiple keys to set in a nested object or array:
|
96
|
+
//
|
97
|
+
// deepSet(obj, ['foo'], v) // obj['foo'] = v
|
98
|
+
// deepSet(obj, ['foo', 'inn'], v) // obj['foo']['inn'] = v // Create the inner obj['foo'] object, if needed
|
99
|
+
// deepSet(obj, ['foo', 'inn', '123'], v) // obj['foo']['arr']['123'] = v //
|
100
|
+
//
|
101
|
+
// deepSet(obj, ['0'], v) // obj['0'] = v
|
102
|
+
// deepSet(arr, ['0'], v, {useIntKeysAsArrayIndex: true}) // arr[0] = v
|
103
|
+
// deepSet(arr, [''], v) // arr.push(v)
|
104
|
+
// deepSet(obj, ['arr', ''], v) // obj['arr'].push(v)
|
105
|
+
//
|
106
|
+
// arr = [];
|
107
|
+
// deepSet(arr, ['', v] // arr => [v]
|
108
|
+
// deepSet(arr, ['', 'foo'], v) // arr => [v, {foo: v}]
|
109
|
+
// deepSet(arr, ['', 'bar'], v) // arr => [v, {foo: v, bar: v}]
|
110
|
+
// deepSet(arr, ['', 'bar'], v) // arr => [v, {foo: v, bar: v}, {bar: v}]
|
111
|
+
//
|
112
|
+
deepSet: function (o, keys, value, opts) {
|
113
|
+
var key, nextKey, tail, lastIdx, lastVal, f;
|
114
|
+
if (opts == null) opts = {};
|
115
|
+
f = $.serializeJSON;
|
116
|
+
if (f.isUndefined(o)) { throw new Error("ArgumentError: param 'o' expected to be an object or array, found undefined"); }
|
117
|
+
if (!keys || keys.length === 0) { throw new Error("ArgumentError: param 'keys' expected to be an array with least one element"); }
|
118
|
+
|
119
|
+
key = keys[0];
|
120
|
+
|
121
|
+
// Only one key, then it's not a deepSet, just assign the value.
|
122
|
+
if (keys.length === 1) {
|
123
|
+
if (key === '') {
|
124
|
+
o.push(value); // '' is used to push values into the array (assume o is an array)
|
125
|
+
} else {
|
126
|
+
o[key] = value; // other keys can be used as object keys or array indexes
|
127
|
+
}
|
128
|
+
|
129
|
+
// With more keys is a deepSet. Apply recursively.
|
130
|
+
} else {
|
131
|
+
|
132
|
+
nextKey = keys[1];
|
133
|
+
|
134
|
+
// '' is used to push values into the array,
|
135
|
+
// with nextKey, set the value into the same object, in object[nextKey].
|
136
|
+
// Covers the case of ['', 'foo'] and ['', 'var'] to push the object {foo, var}, and the case of nested arrays.
|
137
|
+
if (key === '') {
|
138
|
+
lastIdx = o.length - 1; // asume o is array
|
139
|
+
lastVal = o[lastIdx];
|
140
|
+
if (f.isObject(lastVal) && (f.isUndefined(lastVal[nextKey]) || keys.length > 2)) { // if nextKey is not present in the last object element, or there are more keys to deep set
|
141
|
+
key = lastIdx; // then set the new value in the same object element
|
142
|
+
} else {
|
143
|
+
key = lastIdx + 1; // otherwise, point to set the next index in the array
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
// o[key] defaults to object or array, depending if nextKey is an array index (int or '') or an object key (string)
|
148
|
+
if (f.isUndefined(o[key])) {
|
149
|
+
if (nextKey === '') { // '' is used to push values into the array.
|
150
|
+
o[key] = [];
|
151
|
+
} else if (opts.useIntKeysAsArrayIndex && f.isValidArrayIndex(nextKey)) { // if 1, 2, 3 ... then use an array, where nextKey is the index
|
152
|
+
o[key] = [];
|
153
|
+
} else { // for anything else, use an object, where nextKey is going to be the attribute name
|
154
|
+
o[key] = {};
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
// Recursively set the inner object
|
159
|
+
tail = keys.slice(1);
|
160
|
+
f.deepSet(o[key], tail, value, opts);
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
};
|
165
|
+
|
166
|
+
}(window.jQuery || window.Zepto || window.$));
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jquery-serialize-json-sprockets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- shelling
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sprockets
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.11.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.11.0
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- navyblueshellingford@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/jquery-serialize-json-sprockets.rb
|
35
|
+
- lib/jquery/serialize/json/sprockets/engine.rb
|
36
|
+
- vendor/assets/javascripts/jquery.serializejson.js
|
37
|
+
homepage: ''
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.2.2
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: the ladda button effect assets
|
61
|
+
test_files: []
|