Conflict of two javascript files

I’m playing with html5 LeanBack Player and it works well when on the page only its javascritpts. But if I add these javascript files to rails 3 project then I get error in LeanBack Player's javascript file:

Uncaught TypeError: Object function each(iterator, context) {
  var index = 0;
  try {
    this._each(function(value) {
      iterator.call(context, value, index++);
    });
  } catch (e) {
    if (e != $break) throw e;
  }
  return this;
} has no method ‘split’

in function LBPlayer.prototype.resolveTextPlainSubs.

I guessed that’s because of conflict with native Prototype in rails but I don’t know how to resolve it. Thanks

Answers


Indeed it's a conflict with Prototypes extending of Array.prototype:

// leanbackPlayer.js @941
srt = srt.split('\n\n');
var i = 0; var isSub = false;
this.vars.subs[lang] = {};
this.vars.subs[lang].label = {};
this.vars.subs[lang].label = label;
this.vars.subs[lang].track = {};

// Error: using for in over an array is just stupid
//        not even using hasOwnProperty is outright pitiful
for(var s in srt) { 

    // this will also yield `each` but that's a function which has no .split() method
    var st = srt[s].split('\n');
    var time; var j;
    if(st.length >= 2) {
        var t = "";

In order to fix it you should replace the for(var s in srt) with a simple for loop:

for(var e = 0, el = srt.length; e < el; e++) {
    var st = srt[e].split('\n');
    var time; var j;
    if(st.length >= 2) {
        var t = "";
        ...
}

Need Your Help

overriding filterresults in android autocompletetextview?

android filter autocompletetextview

i've been pulling my hair over this a few days, I'm trying to setup an autocompletetextview in android where the user inputs a key and the autocomplete suggestions are the values, however I've trie...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.