gpt4 book ai didi

javascript - 主干 : how to pass data to a render view

转载 作者:行者123 更新时间:2023-11-28 07:37:19 27 4
gpt4 key购买 nike

有没有办法将数据传递给渲染

以下代码是我当前正在使用的代码:

    render:function() {
var background =
var highlight = this.model.get('highlight');
$(this.el).find('div').each(function(index,element) {
if(index < highlight) {
$(element).css({'background': 'url("assets/img/alphabet/ok/ok.png"), url('+background+')'});
$(element).attr('id', 'ok');
} else {
$(element).removeAttr('id');
}
})
}

也许喜欢这张照片:

enter image description here

最佳答案

...你可以这样做:

this.background : [],
//...
initialize : function(){
//...
this.background.push(string.charAt(i));
//...
},
render : function(){
//...
var background = this.background[index];
//...
}

但是,也许 initialize 中的 for 循环也可以保留在 render 中,因为它渲染 View (使用 .append ())。

编辑说明:

我再说一遍,在渲染函数的初始化函数中循环移动的可能性。

我尝试更改您的主干 View 结构:

var WordView = Backbone.View.extend({

initialize : function(){
this.listenTo(this.model,"remove",this.remove);
},

render : function(){
var $Div,
string = this.model.get('string'), //use _string (string can cause some issue in IE)
highlight = this.model.get("highlight"),

letter_width = 36,
letter_heigth = 35,
word_width = string.length * letter_width;

if(this.model.get("x") + word_width > $(window).width()) this.model.set({x:$(window).width() - word_width});

$(this.el).css({
position:"absolute",
top : this.model.get("y"),
left : this.model.get("x")
});

var background,char;
for(var i; i < string.length; i++){
char = string.charAt(i);
background = "url('"+char+"') no-repeat";
$Div.removeAttr("id"); //seems useless

if(i < highlight){
background = "url('assets/img/alphabet/ok/ok.png'),url('"+char+"')";
$Div.id("ok"); //maybe is better $Div.class("ok");
}

$Div = $("<div>").clone();
$Div.css({
width : letter_width,
heigth : letter_height,
float: "left",
"background" : background
}).appendTo($(this.el));

}
}
});

关于javascript - 主干 : how to pass data to a render view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28449362/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com