简体   繁体   English

Backbone视图呈现中的getJSON问题

[英]Issue with getJSON within Backbone view rendering

I'm pretty new to Backbone.js, loving it so far, but I'm having a little trouble trying to get relational data to render. 我对Backbone.js很新,到目前为止还爱它,但我在尝试获取关系数据时遇到了一些麻烦。

Within my Backbone view (called ImagesView) I have the following code: 在我的Backbone视图(称为ImagesView)中,我有以下代码:

// Render it
render: function () {

  var self = this;

  // Empty the container first
  self.$el.html("")

  // Loop through images
  self.collection.each(function(img){

    // convert `img` to a JSON object
    img = img.toJSON()

    // Append each one
    self.$el.append(self.template(img))

  }, self)
}

There are 3 images in the collection, and they are templated correctly with the above code. 集合中有3个图像,使用上面的代码可以正确模板化。 Within the img object is a user attribute, containing the User ID. img对象中是一个user属性,包含用户ID。 I'm trying to return the user's details, and use these within the template instead of the ID. 我正在尝试返回用户的详细信息,并在模板中使用这些而不是ID。 I'm doing that using the code below: 我正在使用下面的代码:

// Render it
render: function () {

  var self = this;

  // Empty the container first
  self.$el.html("")

  // Loop through images
  self.collection.each(function(img){

    // convert `img` to a JSON object
    img = img.toJSON()

    /* New code START */

    // Each img has a `user` attribute containing the userID
    // We'll use this to get their details
    $.getJSON('/user/' + img.user, {}, function(json, textStatus) {
      img.photographer = {
        id: json.id,
        username: json.username,
        real_name: json.real_name
      }

      /* Moved 1 level deeper */
      // Append each one
      self.$el.append(self.template(img))
    });

    /* New code END */

  }, self)
}

When I do this, the user's details are returned correctly and inserted into the template, but I now get 3 of each image returned instead of 1 (ie 9 in total), in a completely random order. 当我这样做时,用户的详细信息会正确返回并插入到模板中,但我现在以完全随机的顺序返回每个图像中的3个而不是1个(即总共9个)。 What am I doing wrong? 我究竟做错了什么? I'm open to using Backbone methods instead of the getJSON if that will make it easier, I just couldn't get it to work myself. 我愿意使用Backbone方法而不是getJSON,如果这样可以让它变得更容易,我就无法让它自己工作。 I'm using underscore.js for the templating 我正在使用underscore.js进行模板化

The random order is likely caused by the requests being fired at very close intervals and responses returning out of the order they were fired in. I'm not sure why you're getting the multiple things, but if your template renders everything and you're calling that 3 times that could be it? 随机顺序很可能是由于请求以非常接近的间隔触发而且响应从它们被触发的顺序返回。我不知道为什么你会得到多个东西,但是如果你的模板呈现了所有内容而且你'重新称之为可能是它的3倍?

Anyway where I think you're going wrong is putting the responsibility of loading data into the render method. 无论如何,我认为你出错了就是把数据加载到render方法中。 You'd want this to be abstracted so you have a good separation between data concerns and template concerns. 您希望将其抽象化,以便在数据问题和模板问题之间实现良好分离。 As the ordering of the data is of interest, you'll want all 3 requests to have loaded before rendering. 由于数据的排序很有意义,因此您需要在渲染之前加载所有3个请求。 There's two different approaches you could take to this depending on if prior to loading this data you have sufficient data to render the view: 您可以采用两种不同的方法,具体取决于在加载此数据之前是否有足够的数据来呈现视图:

If you're waiting on all the data prior to rendering the view then you would want to render a different view (or template of this view) whilst the data is loaded and then replace that with a view showing all the data once it is loaded. 如果您在渲染视图之前等待所有数据,那么您将希望在加载数据时呈现不同的视图(或此视图的模板),然后将其替换为显示所有数据的视图。

If you have sufficient data to render the view and what you are loading is supplementary, you'd want to render the view with the data you have in render and then once the other data is loaded use a custom method to modify the rendered view to include your data. 如果您有足够的数据来渲染视图,并且您正在加载的内容是补充的,那么您需要使用渲染中的数据渲染视图,然后在加载其他数据后使用自定义方法将渲染视图修改为包括你的数据。

If you want to find out when all 3 requests are complete you can use http://api.jquery.com/jquery.when/ 如果您想了解所有3个请求何时完成,可以使用http://api.jquery.com/jquery.when/

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

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