简体   繁体   English

骨干视图无法呈现正确的dom元素

[英]Backbone view not rendering correct dom element

I have two view objects: 我有两个视图对象:

 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

}); 

These Object should then append there content to this HTML: 然后,这些对象应该将内容附加到此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>

I then call instances of both views to render to dom: 然后我调用两个视图的实例来渲染到dom:

   pop = new GenrePop ();
  hip = new GenreHipHop();

The problem is the view elements are append to the ul tag with id #hip model and not as I outlined in the view. 问题是视图元素附加到带有#hip模型的ul标记,而不是我在视图中概述的。 I don't understand what is causing this and how to fix it 我不明白是什么导致了这个以及如何解决它

It's better to control the mapping of views to DOM elements from outside the view. 最好从视图外部控制视图到DOM元素的映射。 You don't want the view to require information about the structure of the page around it. 您不希望视图需要有关其周围页面结构的信息。 Having the view bound to a specific element also prevents reuse in other areas of the site/page. 将视图绑定到特定元素也会阻止在站点/页面的其他区域中重用。

1 - remove the el property from your views 1 - 从您的视图中删除el属性

2 - bind the views to DOM elements like this: 2 - 将视图绑定到DOM元素,如下所示:

var pop = new GenrePop({el: $('#pop')});
var hip = new GenreHipHop({el: $('#hipHop')});

See this article for more information on binding views to elements. 有关将元素绑定到视图的更多信息,请参阅此文章

In your define statements, you are passing in 5 arguments into the first array, but only setting the first 3 to parameters to the function. 在您的define语句中,您将5个参数传入第一个数组,但只将前3个参数设置为函数的参数。 For example, try changing the first line in your file to: 例如,尝试将文件中的第一行更改为:

define(['jquery',
        'underscore',
        'backbone',
        'collections/HipHopVideos',
        'views/Video'],
        function($,_,Backbone, ActHipHopVideoCollection, ActVideoView){
...
}

And then on the line where you create the collection , do this: 然后在创建collection的行上执行以下操作:

collection: new ActHipHopVideoCollection(),

And when you create each videoView , do this: 当您创建每个videoView ,请执行以下操作:

videoView = new ActVideoView({model:video});

That way you are using the collection and view that requirejs is getting for you and passing to this function. 这样你就可以使用requirejs为你获取并传递给这个函数的collectionview

You will need to make this change for the other file as well. 您还需要对其他文件进行此更改。 This way, you are not using some global variables to create objects. 这样,您就不会使用某些全局变量来创建对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM