Backbone/Vimeo API - 查看日志记录,但不渲染
Backbone/Vimeo API - View logging but not rendering
我目前正在学习Backbone并尝试构建我的第一个应用程序。作为一种学习工具,我正在尝试按用户 ID 渲染 Vimeo 画廊。
我已将所有内容放在一起,并且我的视图正在正确记录,但它不会呈现到页面。几个小时以来,我一直在尝试解决这个问题,但我不确定我哪里出错了。任何见解都非常感谢。我的方法正确吗?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Backbone App</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
</head>
<body>
<div id="video-container">
<script type="text/template" id="video_template">
<h1><%= video_title %></h1>
</script>
</div>
<script>
(function($){
var vimeoUser = 9836759;
var Video = Backbone.Model.extend({});
var VideoCollection = Backbone.Collection.extend({
model: Video
});
var VideoView = Backbone.View.extend({
tagName: 'li',
initialize: function(){
_.bindAll(this, 'render');
this.render();
},
render: function(){
var variables = {video_title: this.model.attributes.title};
var template = _.template($('#video_template').html(), variables);
// Logging element works
console.log(template);
// Rendering does not work
this.$el.html( template );
}
});
var GalleryView = Backbone.View.extend({
tagName: 'ul',
initialize: function(){
this.render();
},
render: function(){
this.collection.each(function(video){
var videoView = new VideoView({ model: video});
}, this);
}
});
// Create instance of VideoCollection
var VideoGallery = new VideoCollection;
$.ajax({
url: 'http://vimeo.com/api/v2/' + vimeoUser + '/videos.json',
dataType: 'jsonp',
success: function(response) {
// map api results to our collection
var videos = _.map(response, function(video) {
return {
title: video.title,
details: video.description,
thumbnail_large: video.thumbnail_large,
video: 'http://player.vimeo.com/video/' + video.id + '?api=1&player_id=vimeo-player&autoplay=1'
}
});
// add vimeo videos to collection
VideoGallery.add(videos);
var galleryView = new GalleryView({ el: $('#video-container'), collection: VideoGallery });
}
});
})(jQuery);
</script>
</body>
</html>
您在VideoView
上缺少一个元素,因此它只是渲染到未附加到 DOM 的li
。 我会建议这样的事情:
var self = this;
this.collection.each(function(video){
var container = $("<li>");
self.$el.append(container);
var videoView = new VideoView({ el: container, model: video});
}, this);
小提琴
传统上,render
只是填充视图的el
,但不接触 DOM。通常的模式是这样的:
var v = new View(...)
v.render();
$(container).append(v.el);
通常render
返回this
因此您经常会看到:
$(container).append(v.render().el);
首先调整所有render
方法,以 return this;
结尾。然后调整内容以更新 DOM:
var VideoView = Backbone.View.extend({
//...
render: function() {
//...
return this; // <---------- Add this
}
});
var GalleryView = Backbone.View.extend({
//...
render: function() {
this.collection.each(function(video){
var videoView = new VideoView({ model: video });
this.$el.append(videoView.el); // <---------- Add this
});
return this; // <---------- Add this
}
});
您的GalleryView
设置为创建<ul>
作为其el
你会想说:
var galleryView = new GalleryView({ collection: VideoGallery });
$('#video-container').append(galleryView.el);
把一切都搞大。
相关文章:
- UI的日志记录框架
- 将Function-Nothing日志记录返回到控制台
- Ember.js——如何对模型进行控制台日志记录
- 日志记录功能/对象名称's在函数堆栈内的javascript中
- 扩展 console.log() 的 javascript Web 应用程序的客户端日志记录
- 日志记录时避免 Promise 构造函数反模式
- 在使用 Html/WinJS 开发的 Metro 应用程序中实现日志记录
- 客户端日志记录
- OrientDB通过触发器自动记录更改日志
- 如何在 mvc3 Web 应用程序的生产版本中删除 JS 日志记录调用
- 在 $.ajaxSetup 转换器中获取 ajax 请求 URL,以便进行日志记录
- Web 服务的快速顺序日志记录
- AngularJS:客户端错误的服务器端日志记录
- 如果用户未登录,如何使用 Jquery AJAX 加载内部日志记录页
- 自动日志记录 JavaScript
- 是否有JavaScript API可以将日志发送到云日志记录服务(如papertrail)
- 如何使用快乐的js进行每个用户群的日志记录
- 如果用户禁用了JS, Apache可以记录日志吗?
- 修改对象的原型,以便在每次请求变量时向控制台记录日志
- 为什么我的测试失败,并且lodash方法中的console.log()语句没有记录日志