forked from evoluteur/evolutility-ui-jquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathone-json.js
More file actions
executable file
·81 lines (67 loc) · 2.09 KB
/
Copy pathone-json.js
File metadata and controls
executable file
·81 lines (67 loc) · 2.09 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*! ***************************************************************************
*
* evolutility :: one-json.js
*
* View "one json" to edit one backbone model in JSON.
*
* https://github.com/evoluteur/evolutility
* Copyright (c) 2015, Olivier Giulieri
*
*************************************************************************** */
Evol.ViewOne.JSON = Evol.ViewOne.extend({
events: {
'click > .evol-buttons > button': 'click_button'
},
viewName: 'json',
render: function () {
var h = [],
eUI=Evol.UI;
if(this.model){
var jsonStr=JSON.stringify(this.model, null, 2);
h.push(
eUI.label('uimjson', 'JSON'),
eUI.input.textMJSON('uimjson', jsonStr, 16));
this._renderButtons(h, 'json');
}else{
h.push(eUI.HTMLMsg(Evol.i18n.nodata, '', 'info'));
}
this.$el.html(h.join(''));
this.setData(this.model);
//this.custOn=false;
return this;
},
validate: function () {
var isValid=true,
data=this.getData(),
$fp=this._getDOMField().parent();
//this.clearMessages();
isValid=!Evol.UI.addRemClass($fp, data===null, 'has-error');
this.$el.trigger('action', 'validate', {valid:isValid});
return isValid?[]:[Evol.i18n.validation.invalid];
},
getData: function () {
var jsonStr=this._getDOMField().val(),
obj;
try{
obj=$.parseJSON(jsonStr);
}catch(err){
obj=null;
}
return obj;
},
setData: function (m) {
this.clearError()._getDOMField().val(JSON.stringify(m, null, 2));
return this.setTitle();
},
clear: function () {
this._getDOMField().val('');
return this;
},
clearError: function(){
this._getDOMField().parent().removeClass('has-error');
return this;
},
_getDOMField: function(){
return this.$el.children('textarea');
}
});