为下一次实例化设置的主干视图属性
Backbone View Attribute set for next Instantiation?
我有一个具有tooltip
属性的视图。我想在initialize
或render
上动态设置该属性。但是,当我设置它时,它出现在该视图的下一个实例而不是当前实例上:
var WorkoutSectionSlide = Parse.View.extend( {
tag : 'div',
className : 'sectionPreview',
attributes : {},
template : _.template(workoutSectionPreviewElement),
initialize : function() {
// this.setDetailsTooltip(); // doesn't work if run here either
},
setDetailsTooltip : function() {
// build details
...
// set tooltip
this.attributes['tooltip'] = details.join(', ');
},
render: function() {
this.setDetailsTooltip(); // applies to next WorkoutViewSlide
// build firstExercises images
var firstExercisesHTML = '';
for(key in this.model.workoutExerciseList.models) {
// stop after 3
if(key == 3)
break;
else
firstExercisesHTML += '<img src="' +
(this.model.workoutExerciseList.models[key].get("finalThumbnail") ?
this.model.workoutExerciseList.models[key].get("finalThumbnail").url : Exercise.SRC_NOIMAGE) + '" />';
}
// render the section slide
$(this.el).html(this.template({
workoutSection : this.model,
firstExercisesHTML : firstExercisesHTML,
WorkoutSection : WorkoutSection,
Exercise : Exercise
}));
return this;
}
});
以下是我初始化视图的方式:
// section preview
$('#sectionPreviews').append(
(new WorkoutSectionPreview({
model: that.workoutSections[that._renderWorkoutSectionIndex]
})).render().el
);
如何在当前视图上动态设置attribute
(工具提示),为什么它会影响下一个视图?
谢谢
您可以将attribute
属性定义为返回对象作为结果的函数。因此,您可以动态设置属性。
var MyView = Backbone.View.extend({
model: MyModel,
tagName: 'article',
className: 'someClass',
attributes: function(){
return {
id: 'model-'+this.model.id,
someAttr: Math.random()
}
}
})
我希望它能成功。
我认为
你的问题就在这里:
var WorkoutSectionSlide = Parse.View.extend( {
tag : 'div',
className : 'sectionPreview',
attributes : {} // <----------------- This doesn't do what you think it does
您放入.extend({...})
中的所有内容最终都会WorkoutSectionSlide.prototype
,它们不会复制到实例中,而是通过原型由所有实例共享。在您的情况下,结果是你有一个由所有WorkoutSectionSlide
共享的attributes
对象。
此外,视图的attributes
仅在构造对象时使用:
var View = Backbone.View = function(options) {
this.cid = _.uniqueId('view');
this._configure(options || {});
this._ensureElement();
this.initialize.apply(this, arguments);
this.delegateEvents();
};
_ensureElement
调用是使用 attributes
的东西,您会注意到它在调用initialize
之前出现。该顺序与原型行为相结合,这就是属性显示在视图的下一个实例上的原因。attributes
实际上是针对静态属性的,您的this.$el.attr('tooltip', ...)
解决方案是处理动态属性的好方法。
相关文章:
- Javascript使用变量设置属性
- 如何在自动完成时设置属性标题
- 未捕获的类型错误:无法设置属性'innerHTML'如果为null,则将脚本移动到正文不会;不起作用
- 未捕获的类型错误:无法设置属性'背景'的未定义
- 无法设置属性'innerHTML'在javascript中为null
- Haxe Javascript:在不使用Reflect的情况下按名称获取和设置属性
- Jquery为每个元素设置属性
- 从骨干集合筛选模型,然后为这些模型设置属性
- 我们可以设置属性'id'到不同的'按钮'通过使用javascript或jquery的循环
- 如何在javascript对象中设置属性的类型,就像mongoose模式设计一样
- emscripten+sdl=引发异常:TypeError:无法设置属性'widthNative'的未定
- 未捕获的类型错误:无法设置属性'onclick'为null.已尝试window.onload
- Backbone JS Promises在模型上设置属性之前解析
- Highcharts无法设置属性'的值;要点':对象为null或未定义
- 无法设置属性'round'的未定义
- 通过 Javascript 设置 C# 属性值
- 在 app.locals 上设置属性和调用 app.set() 有什么区别?
- 不引人注目地设置属性后函数失败
- j查询在设置属性后在后续单击时不读取数据属性
- IE错误:无法设置属性'的值;样式':对象为null或未定义