2014-01-11 29 views
0

我正在重写一些Backbone代码,并得到一个奇怪的错误。这里是我的代码:

App.Views.ExploreCard.user = App.Views.ExploreCard.Parent.extend({ 
    name: 'user', 
    allowUntyped: true, 
    classes: 'explore-person-box shadowed', 
    onclick: function() { 
    window.location.assign(this.data.profilePath); 
    }, 
    postRender: function() { 
    App.HCS.redrawHCS(this.$el.find('.score'), this.$el.find('.user-card-avatar-round')); 
    } 
}); 

App.HCS = { 
    redrawHCS: function(elements) { 
    elements.each(function(index, value, border) { 
     var score = $(value).html(); 
     console.log(value); 
     console.log(border); 
     if (score <= 33 && score >= 1) { 
     $(value).css("background-color", "#ff4013"); 
     $(border).css("border", "3px solid #ff4013;"); 
     } 
     else if (score <= 66 && score >= 34) { 
     $(value).css("background-color", "rgb(92, 154, 0)"); 
     $(border).css("border", "3px solid rgb(92, 154, 0)"); 
     } 
     else if (score <= 99 && score >= 67) { 
     $(value).css("background-color", "#fb9f00"); 
     $(border).css("border", "3px solid #fb9f00;"); 
     } 
    }); 
    } 
}; 

当我这样做的价值和边境console.log,我总是不确定的边境。我曾作为一个全面的检查切换参数在这条线:

来自:

App.HCS.redrawHCS(this.$el.find('.score'), this.$el.find('.user-card-avatar-round')); 

到:

App.HCS.redrawHCS(this.$el.find('.user-card-avatar-round'), this.$el.find('.score')); 

不管第二个参数的边界始终是不确定的顺序。

我在Backbone $el上做了一些Google搜索,但没有找到任何修复。

回答

2

你的代码有点困惑。你打电话App.HCS.redrawHCS有两个参数:

App.HCS.redrawHCS(this.$el.find('.score'), this.$el.find('.user-card-avatar-round')); 

redrawHCS只看一次的说法:

redrawHCS: function(elements) { 

然后尝试用each遍历elements

elements.each(function(index, value, border) { 

但将jQuery的each和调用回调函数为:

function(index, dom_element) 

所以value将是一个DOM元素,borderundefined,并且所述第二参数redrawHCS将被完全忽略。

如果.user-card-avatar-round应该是border的价值.score那么你想要的东西更是这样的:

redrawHCS: function(values, borders) { 
    var i, value, border; 
    for(i = 0; i < values.length; ++i) { 
     value = values[i]; 
     border = borders[i]; 
     //... 
    } 
} 

我敢肯定有更好的方法来解决这个问题,但我会需要知道DOM结构才能给你一个。我猜想它在each函数里看起来像redrawHCS(this.$el.find('.score'))border = $(value).closest('.user-card-avatar-round'),但这是非常推测的。