Skip to content

Commit d69c153

Browse files
author
Philipp
committed
Namespacing added
1 parent 156aa8f commit d69c153

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
<script>
2626
$(function () {
2727

28-
$('.code-snippet').codemirror();
28+
$('.code-snippet').codemirrorInit();
2929

30-
$('.code-snippet[data-test]').setValue('Any string');
30+
$('.code-snippet[data-test]').codemirror().setValue('Any string');
3131

32-
console.log($('.code-snippet[data-test]').getValue());
32+
console.log($('.code-snippet[data-test]').codemirror().getValue());
3333

3434
var elements = $('.code-snippet');
3535

@@ -38,13 +38,13 @@
3838
elements.each(function (key, element) {
3939

4040
$.data(element, 'codemirror').setValue('Any' + increment++);
41-
$(element).setValue('New' + increment++);
41+
$(element).codemirror().setValue('New' + increment++);
4242

4343
});
4444

45-
$('.code-snippet[data-no-lines]').setOption('lineNumbers', false);
45+
$('.code-snippet[data-no-lines]').codemirror().setOption('lineNumbers', false);
4646

47-
$('.code-snippet[data-no-lines]').setOptions({lineNumbers: true, readOnly: true});
47+
$('.code-snippet[data-no-lines]').codemirror().setOptions({lineNumbers: true, readOnly: true});
4848

4949
$.data($('.code-snippet[data-no-lines]')[0], 'codemirror').focus();
5050
});

philsweb.jquery.codemirror.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
(function ($) {
2-
$.fn.codemirror = function () {
2+
$.fn.codemirrorInit = function () {
33

44
$(this).init.prototype = $.extend({}, $(this).init.prototype, {
55

6-
setValue: function (value) {
7-
for (var i = 0; i < this.length; i++) {
8-
$.data(this[i], 'codemirror').setValue(value);
9-
}
10-
},
11-
getValue: function () {
12-
if (this.length > 1) {
13-
throw new Error("Only one DOM element can be selected");
14-
}
15-
return $.data(this[0], 'codemirror').getValue();
16-
},
17-
setOption: function (key, value) {
18-
for (var i = 0; i < this.length; i++) {
19-
$.data(this[i], 'codemirror').setOption(key, value);
20-
}
21-
},
22-
setOptions: function (optionsObject) {
23-
for (var i = 0; i < this.length; i++) {
24-
var $this = this[i];
25-
$.each(optionsObject, function (key, value) {
26-
$.data($this, 'codemirror').setOption(key, value);
27-
});
28-
}
6+
codemirror: function(){
7+
var $this = this;
8+
return {
9+
setValue: function (value) {
10+
for (var i = 0; i < $this.length; i++) {
11+
$.data($this[i], 'codemirror').setValue(value);
12+
}
13+
},
14+
getValue: function () {
15+
if (this.length > 1) {
16+
throw new Error("Only one DOM element can be selected");
17+
}
18+
return $.data($this[0], 'codemirror').getValue();
19+
},
20+
setOption: function (key, value) {
21+
for (var i = 0; i < $this.length; i++) {
22+
$.data($this[i], 'codemirror').setOption(key, value);
23+
}
24+
},
25+
setOptions: function (optionsObject) {
26+
for (var i = 0; i < this.length; i++) {
27+
var element = $this[i];
28+
$.each(optionsObject, function (key, value) {
29+
$.data(element, 'codemirror').setOption(key, value);
30+
});
31+
}
32+
}
33+
}
2934
}
30-
3135
});
3236

3337
return this.each(function () {

0 commit comments

Comments
 (0)