Skip to content

Commit e4a3fc2

Browse files
committed
fix(instances): Clash in options due to value change of options object
1 parent fdfa221 commit e4a3fc2

6 files changed

Lines changed: 67 additions & 19 deletions

File tree

demo/index.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ <h1>Loading embed.js in a block</h1>
4848
```
4949
Flavum, ferox http://open.spotify.com/track/4th1RQAelzqgY7wL53UGQt torquiss vix aperto de http://plnkr.co/edit/AK732k bi-color, http://www.iitr.ac.in/Main/assets/images/topold.jpg audax nixus.Pol, alter devatio!Crescere aliquando ducunt ad noster lixa. :smile: :P https://www.youtube.com/watch?v=bQRLVxZHKPs http://www.liveleak.com/view?i=42a_1426999994 https://gist.github.com/jeremiahlee/1748966 https://gist.github.com/thomasmb/7132686 https://twitter.com/ProductHunt/status/652826210666418176/photo/1 http://www.ted.com/talks/monica_lewinsky_the_price_of_shame http://www.dailymotion.com/video/x2k8un2_2015-wc-nz-vs-wi-holder-reacts-on-guptills-237_sport https://vine.co/v/bjHh0zHdgZT https://vimeo.com/119199079 http://www.w3schools.com/html/horse.mp3
5050
</div>
51+
<div id="block2">
52+
Cur rumor nocere?Emeritis adelphis satis perderes domina est.Gloss cadunt in bi-color brema!
53+
@(IIT Roorkee) Bi-color habitio virtualiter imitaris amicitia est.Teres tatas ducunt ad racana.Genetrixs
54+
prarere in
55+
rugensis civitas!
56+
</div>
5157
</div>
5258
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js"></script>
5359
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
@@ -67,7 +73,13 @@ <h1>Loading embed.js in a block</h1>
6773
var x = new EmbedJS({
6874
element: document.getElementById('block')
6975
});
70-
x.render();
76+
x.render()
77+
// x.render();
78+
var y = new EmbedJS({
79+
element: document.getElementById('block2')
80+
});
81+
y.render()
82+
7183
x.text(function(data){
7284
console.log(data);
7385
})

dist/embed.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/embed.js

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/embed.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/embed.es6

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,22 @@ const helper = require('./modules/video/helper.es6');
108108

109109
class EmbedJS {
110110
constructor(options, input) {
111+
let [defOpts,globOpts]=[utils.cloneObject(defaultOptions),utils.cloneObject(globalOptions)]
111112
//merge global options with the default options
112-
let globOptions = utils.deepExtend(defaultOptions, globalOptions)
113+
let globOptions = utils.deepExtend(defOpts, globOpts)
113114

114115
this.options = utils.deepExtend(globOptions, options);
115-
this.element = this.options.element || input;
116-
if (!this.element) {
117-
throw ReferenceError("You need to pass an element or the string that needs to be processed");
116+
if (!this.options.element && !input) {
117+
throw ReferenceError("You need to pass an element or the string that needs to be processed");
118+
}
119+
120+
if (this.options.element) {
121+
this.element = this.options.element;
122+
this.input = this.element.innerHTML;
123+
}else{
124+
this.input = input;
118125
}
119-
this.input = this.element.innerHTML;
126+
120127
}
121128

122129
/**
@@ -192,21 +199,21 @@ const helper = require('./modules/video/helper.es6');
192199
}
193200

194201
let ejs = {
195-
instances:[],
196-
elements :utils.getElementsByAttributeName('data-embed-js'),
197-
setOptions : function(options){
202+
instances: [],
203+
elements: utils.getElementsByAttributeName('data-embed-js'),
204+
setOptions: function(options) {
198205
globalOptions = utils.deepExtend(defaultOptions, options)
199206
},
200-
applyEmbedJS: function(){
207+
applyEmbedJS: function() {
201208
for (let i = 0; i < this.elements.length; i++) {
202209
let option = {
203-
element:this.elements[i]
210+
element: this.elements[i]
204211
}
205212
this.instances[i] = new EmbedJS(option)
206213
this.instances[i].render()
207214
}
208215
},
209-
destroyEmbedJS: function(){
216+
destroyEmbedJS: function() {
210217
for (let i = 0; i < this.elements.length; i++) {
211218
this.instances[i].destroy()
212219
}

src/js/modules/utils.es6

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ var utils = {
142142
}
143143
}
144144
return matchingElements;
145+
},
146+
147+
/**
148+
* Returns a cloned object
149+
* @param {object} obj
150+
* @return {object} cloned object
151+
*/
152+
cloneObject: function(obj) {
153+
if (obj === null || typeof obj !== 'object') {
154+
return obj;
155+
}
156+
157+
var temp = obj.constructor(); // give temp the original obj's constructor
158+
for (var key in obj) {
159+
temp[key] = this.cloneObject(obj[key]);
160+
}
161+
162+
return temp;
163+
145164
}
146165
};
147166

0 commit comments

Comments
 (0)