-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.execute-object.js
More file actions
59 lines (57 loc) · 1.71 KB
/
jquery.execute-object.js
File metadata and controls
59 lines (57 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* File: jquery.execute-object.js
* Version: 1.0.0
* Author: Iļja Ketris
*
* Copyright 2012 Iļja Ketris
*
* This source file is free software, under either the GPL v2 license or a
* BSD style license, as supplied with this software.
*
* This source file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
* This file contains implementation of the jQuery function that
* executes jQuery methods on objects, retrieved in JSON compliant string or object.
*
*
* {"#my_id": ["addClass", "error"]} $('#my_id').addClass("error")
*
*/
jQuery.extend({
executeObject: function(data) {
var matchCount = 0
var isOnlyStrings = function (s) {
if (typeof s == 'string') return true
var filtered = $.grep(s, function(x){
return typeof x == 'object'
})
return ! filtered.length
}
var callMethod = function (selector, array) {
var method, args
selector = $(selector)
if (!selector.length) return
if (!array) return
if (!array.length) return
method = array[0]
args = array.slice(1)
if (selector[method] instanceof Function) { // if callable
selector[method].apply(selector, args)
matchCount += 1
}
}
if (!arguments.length) return
try { if (typeof data == 'string') data = JSON.parse(data) }
catch(e) { return 0 }
for (selector in data) {
var value = $.makeArray(data[selector])
if (isOnlyStrings(value)) callMethod(selector, value)
else $.each(value, function(i, y) {
callMethod(selector, $.makeArray(y))
})
}
return matchCount
}
})