View没有得到渲染的backbonejs

时间:2016-04-20 07:00:51

标签: javascript html model-view-controller backbone.js

我有以下的backbonejs代码,它从我的API中获取集合中的数据。

app.js

var app = app || {};

app.Book = Backbone.Model.extend({
    defaults: {
    photo: 'https://tpc.googlesyndication.com/simgad/10102369525962162085'
  }

});

app.Library = Backbone.Collection.extend({
    model: app.Book,
    url: 'http://localhost:8000/api/v1/products_user',
    parse: function (response) {
       return response.data;
    }
});

app.BookView = Backbone.View.extend({
    tagName: 'article',
    className: 'white-panel',
    template: _.template( $( '#product_grid_template' ).html() ),

    render: function() {
        //this.el is what we defined in tagName. use $el to get access to jQuery html() function
        this.$el.html( this.template( this.model.attributes ) );

        return this;
    }
});


app.LibraryView = Backbone.View.extend({
    el: '#pinBoot',

    initialize: function() {                    // UPDATED
        this.collection = new app.Library();    // UPDATED
        this.collection.fetch({
            reset:true});

        this.listenTo( this.collection, 'add', this.renderBook );
        this.listenTo( this.collection, 'reset', this.render ); // NEW

    },

    // render library by rendering each book in its collection
    render: function() {
        console.log('render');
        this.collection.each(function( item ) {
            this.renderBook( item );
        }, this );
    },

    // render a book by creating a BookView and appending the
    // element it renders to the library's element
    renderBook: function( item ) {
        var bookView = new app.BookView({
            model: item
        });
        this.$el.append( bookView.render().el );
    }
});

new app.LibraryView();

以上代码呈现以下HTML

<article class="white-panel r1 c0" style=
"width: 316.667px; left: 0px; top: 0px;">
    <div class="widget portfolio graphics homepage">
        <div class="entry-container span4">
            <!-- Portfolio Image -->
            <div class="entry-image">
                <a href="product.html"><img alt="" src=
                "https://tpc.googlesyndication.com/simgad/10102369525962162085"></a>
            </div>
            <div class="entry drop-shadow curved">
                <!-- Portfolio Heading -->
                <div class="heading">
                    <a href="#">My product</a> HII
                </div>
                <div class="camera" data-target="#prod0-modal" data-toggle=
                "modal">
                    3
                </div>
                <div class="price">
                    ₹ 10 <span>+ Delivery Charges ₹ 11</span>
                </div>
            </div>
            <div class="clearfix"></div>
        </div>
    </div>
</article>

整个HTML和数据被渲染到HTML中,我发现不可见的原因是因为,样式参数&#39; style =&#34; opacity:1;&#34;&#39 ;没有添加到<div class="entry-container span4">

上面的app.js,我已修改为工作,没有API如下

app.LibraryView = Backbone.View.extend({
    el: '#pinBoot',

    initialize: function(initialBooks) {                    
        // this.collection = new app.Library();   
        // this.collection.fetch({
        //     reset:true});

        // this.listenTo( this.collection, 'add', this.renderBook );
        // this.listenTo( this.collection, 'reset', this.render ); // NEW
        this.collection = new app.Library( initialBooks );
        this.render();

    },

    // render library by rendering each book in its collection
    render: function() {
        console.log('render');
        this.collection.each(function( item ) {
            this.renderBook( item );
        }, this );
    },

    // render a book by creating a BookView and appending the
    // element it renders to the library's element
    renderBook: function( item ) {
        var bookView = new app.BookView({
            model: item
        });
        this.$el.append( bookView.render().el );
    }
});

var books = [
        { product_name: 'My product',delivery_charge: '10.00',amount:'10.22' },
           ];
new app.LibraryView(books);

现在一切都很完美,风格被添加到div没有任何问题。使用淡入淡出效果javascript添加样式,假设为网格提供淡入淡出效果,但是当我使用API​​获取数据和渲染时,div不会被样式元素添加。

为什么会这样?它与页面加载渲染或异步数据加载有关吗?

我是backbonejs的新手,当我打电话给API时,有人可以帮我解决这个问题吗?

更新

我有这个脚本,它将不透明度元素添加到div的visiblity

<script type="text/javascript">
         //<![CDATA[
            $(window).load(function() { // makes sure the whole site is loaded
                 $("#status").fadeOut(); // will first fade out the loading animation
                 $("#preloader").delay(350).fadeOut("slow"); // will fade out the white DIV that covers the website.
                 $("#preloader-container").css("position","static");
                 $(".widget .entry-container").each(function(index) {
                     $(this).delay(400*index).animate({'opacity': 1},300);

                 });  
             });
         //]]>
      </script>

此脚本是否与我的主干数据呈现冲突?

1 个答案:

答案 0 :(得分:0)

在绑定事件以进行处理之前,您将获取数据。如果获取速度足够快,则可能会导致竞争条件。

initialize: function() {                    // UPDATED
    this.collection = new app.Library();    // UPDATED
    this.collection.fetch({
        reset:true});

    this.listenTo( this.collection, 'add', this.renderBook );
    this.listenTo( this.collection, 'reset', this.render ); // NEW

}

设置视图集合后,绑定所有必要的事件,然后对其执行任何操作:

initialize: function() {                    // UPDATED
    this.collection = new app.Library();    // UPDATED
    this.listenTo( this.collection, 'add', this.renderBook );
    this.listenTo( this.collection, 'reset', this.render ); // NEW

    this.collection.fetch({
        reset:true});
}