我有两个视图对象:
define(['jquery','underscore','backbone','collections/HipHopVideos','views/Video'],function($,_,Backbone){
GenreHipHop = Backbone.View.extend ({
el:"#hipHop",
collection: new HipHopVideosCollection,
initialize: function ()
{
context = this;
//fetche data for collection
this.collection.fetch({success:function ()
{
context.render ();
}
});
},
render: function ()
{
this.collection.each(function(video)
{
videoView = new VideoView({model:video});
this.$el.append(videoView.render().el);
},this);
}//end render function
});
define(['jquery','underscore','backbone','collections/PopVideos','views/Video'], function($,_,Backbone){
GenrePop = Backbone.View.extend({
el:"#pop",
collection: new PopVideosCollection(),
initialize: function ()
{
context = this;
//fetche data for collection
this.collection.fetch({success:function ()
{
context.render ();
}
});
},//end initialize function
render: function ()
{
this.collection.each(function(video)
{
videoView = new VideoView({model:video});
this.$el.append(videoView.render().el);
},this);
}//end render function
});
然后,这些对象应将内容附加到此 HTML:
<div class="sect">
<a href="" class="genre_title">Pop</a>
<img class="previousBtn" src="images/nav-left.png"/>
<ul class="video_item" id="pop" page="1"></ul>
<img class="nextBtn" src="images/nav-right.png"/>
<button class="btn btn-small view-all" type="button">view all</button>
</div>
<div class="sect">
<a href="" class="genre_title">Hip Hop</a>
<img class="previousBtn" src="images/nav-left.png"/>
<ul class="video_item" id="hipHop" page="1">
<script> //loadHipHop ()</script>
</ul>
<img class="nextBtn" src="images/nav-right.png"/>
<button class="btn btn-small view-all" type="button">view all</button>
</div>
然后我调用两个视图的实例来渲染到 dom:
pop = new GenrePop ();
hip = new GenreHipHop();
问题是视图元素被附加到带有 id #hip 模型的 ul 标记,而不是我在视图中概述的那样。我不明白是什么原因造成的以及如何解决它