-
Notifications
You must be signed in to change notification settings - Fork 0
/
binddata.js
42 lines (39 loc) · 1.1 KB
/
binddata.js
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
/*! binddata.js v0.0.1 https://github.com/valtido/binddata 2014 @Valtid Caushi*/
;(function($){
'use strict';
function binddata(){
$('binddata')
.not('[binddata=true]')
.each(function(i,element){
var selector = $(element).attr('selector') || $(element).next()
, data = $(element).attr('src') || $(element).html()
, type = typeof $(element).attr('src') == "undefined"? "inline" : "external"
;
if(typeof selector == "undefined")
throw "Databind: A Selector is required, to bind this data to.";
else
selector = $(selector);
if(typeof $(element).attr('binddata') == "undefined"){
$(element).attr('binddata',true);
if(type === "external") {
var url = data;
$.getJSON(url, function(response){
data = response
$(selector).data('binddata',data);
$(selector).trigger('binddata');
})
}else{
data = JSON.parse($.trim(data));
$(selector).data('binddata',data);
}
}else{
try { console.warn("Binddata: only binds once.")} catch(e){ }
}
})
}
window.binddata = binddata;
binddata();
$(function(){
binddata();
})
})(jQuery);