简体   繁体   English

骨干模板数据显示在console.log中,但不在页面上

[英]Backbone template data shows up in console.log but not on page

I'm new to Backbone and I've gotten as far as to grab a set of data from Instagram and output it onto a page by simply appending it to a container (commented out in the code below). 我是Backbone的新手,我已经从Instagram上获取了一组数据,只需将其附加到容器即可(将其注释在下面的代码中),并将其输出到页面上。 However I'd like to utilize the templating system instead to handle the output. 但是我想利用模板系统来处理输出。 This is where it falls apart and I'm now beginning to think I don't even have this set up properly. 这是它崩溃的地方,我现在开始认为我什至没有适当地设置它。 I have a feeling to the fetchData method in my view should and the loop in my render method should be methods that belong to the collection instead. 我有一种感觉,到fetchData我认为方法应该在我的循环render方法应该是属于集合,而不是方法。

Proper practices aside, the main issue I'm having is that when I attempt to pass data over to the template it comes out blank. 除了正确的做法,我遇到的主要问题是,当我尝试将数据传递到模板时,它变成空白。 However, I can see all of the necessary information when I console.log it from the template. 但是,当我从模板中进行console.log记录时,我可以看到所有必要的信息。

Here's my JS file 这是我的JS文件

var social = {}

/*
*
* MODELS
*
*/
social.Instagram = Backbone.Model.extend();

/*
 *
 * COLLECTIONS
 *
 */
social.InstagramFeed = Backbone.Collection.extend({
  model: social.Instagram,
  url: 'https://api.instagram.com/v1/users/<USER_ID>/media/recent/?client_id=<CLIENT_ID>',
  parse: function(response) {
    return response;
  },
  sync: function(method, model, options) {
    var params = _.extend({
        type: 'GET',
        dataType: 'jsonp',
        url: this.url,
        processData: false
    }, options);
    return $.ajax(params);
  }
});

/*
 *
 * VIEWS
 *
 */
social.InstagramView = Backbone.View.extend({
  el: '#social',
  feed: {},
  initialize: function() {
    this.collection = new social.InstagramFeed();

    this.collection.on('sync', this.render, this);

    this.fetchData();
  },
  render: function() {
    var images = {};
    // var images = '';

    for(var i = 0; i < this.feed.length; i++) {
        var photo = this.feed[i].images.standard_resolution.url;
        var caption = this.feed[i].caption == null ? 'no caption' : this.feed[i].caption.text;
        var likes = this.feed[i].likes.count;
        var id = this.feed[i].id;

        // images += '<img src="'+photo+'" data-caption="'+caption+'" data-likes="'+likes+'" data-id="'+id+'" alt="">';
        images[i] = {'photo': photo, 'caption': caption, 'likes': likes, 'id': id};
    }

    // this.model = images;
    // $('#social').append(images);

    var template = _.template($('#instagram-template').html());
    this.$el.html(template({ collection: images }));

  },
  fetchData: function() {
    var self = this;
    this.collection.fetch({
        success: function(collection, response) {
            self.feed = response.data;
        },
        error: function() {
            console.log("failed to find instagram feed...");
        }
    });
  }
});

social.instagramview = new social.InstagramView;

Template File 模板文件

<script type="text/template" id="instagram-template">
  <% _.each(collection, function(item) { %>
    <img src="<%= item.photo %>"> // this doesn't work
    <% console.log(item.photo) %> // this works
  <% }); %>
</script>

Am I on the right track? 我在正确的轨道上吗? Should I look into restructuring the logic between my controller and view? 我应该研究重组控制器和视图之间的逻辑吗?

So here's the solution by user2989731: 所以这是用户2989731的解决方案:

The problem was occurring from loading the scripts before the template script. 该问题是由于在模板脚本之前加载脚本而发生的。 It started working when the scripts were loaded into the bottom of the page 当脚本加载到页面底部时,它开始工作

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

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