我正在尝试在实例
中附加el
内的html
var Article1 = Backbone.View.extend({
initialize: function(options) {
console.log('type 1', options)
this.flyIn(options);
},
flyIn: function(options) {
this.$el.find('.superDog').css({
display: 'block'
});
this.$el.find('.superDog').animate({
top: options.top,
left: options.left
}, 3000);
},
render: function() {
},
el: '<div><p class="superDog"></p></div>'
});
var Article1A = new Article1({
color: 'black',
top: '300',
left: '300',
html: 'hello world'
});
var Article1B = new Article1({
color: 'blue',
top: '800',
left: '800',
html: 'Hello Everyone'
});
var Article1C = new Article1({
color: 'blue',
top: '600',
left: '1000',
html: 'Hello No One'
});
我尝试过append.el
(或el.append
,不知道它走向哪个方向),options.html
等等。
是否有可能做我正在尝试做的事情,或者我必须使用别的东西吗?
答案 0 :(得分:0)
花些时间阅读Backbone文档:
通过缓存对象来优化对jQuery的使用,而不是一遍又一遍地选择它们。
// chain when possible
this.$('.superDog')
.append(options.html)
.css({ display: 'block' });
// cache the object
var $superDog = this.$('.superDog');
// and use it later
$superDog.append(options.html);
// ...more code and then...
$superDog.css({ display: 'block' });
制作一个适用于每篇文章的通用视图。
var ArticleView = Backbone.View.extend({
tagName: 'p',
className: 'superDog',
render: function() {
// apply the template
this.$el.html(this.model.get('html'))
.show()
.animate({
top: this.model.get('top'),
left: this.model.get('left')
}, 3000);
return this; // always return this in render.
},
});
然后在通用文章列表视图中使用此视图。
var ArticleList = Backbone.View.extend({
render: function() {
this.$el.empty();
this.collection.each(this.renderArticle, this);
return this;
},
renderArticle: function(model) {
var view = new ArticleView({
model: model
});
this.$el.append(view.render().el);
},
});
使用Backbone模型和集合来传达数据。
var list = new ArticleList({
collection: new Backbone.Collection([{
id: '1A',
color: 'black',
top: '300',
left: '300',
html: 'hello world'
},
{
id: '1B',
color: 'blue',
top: '800',
left: '800',
html: 'Hello Everyone'
},
{
id: '1C',
color: 'blue',
top: '600',
left: '1000',
html: 'Hello No One'
}
])
});
list.render();